Exforsys.com
 
Home Tutorials PHP
 

PHP Strings

 
Author: Harsha M V
Category: PHP
Comments (0)

Table of Contents

 PHP Strings
 String Functions

String Functions

Page 2 of 2


String Functions:

print()

print function outputs a string



Example:


int print (string $arg)


print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.


Various Method of Using print:


Sample Code
  1. <?php
  2. print("Hello World")
  3. print "print() also works without parentheses."
  4. print "This spans
  5. multiple lines. The newlines will be
  6. output as well"
  7. print "This spans multiple lines. The newlines will be n output as well."
  8. print "escaping characters is done "Like this"."
  9.  
  10. print <<<END
  11. This uses the "here document" syntax to output
  12. multiple lines with $variable interpolation. Note
  13. that the here document terminator must appear on a
  14. line with just a semicolon no extra whitespace!
  15. END //(USING HEREDOC)
  16.  
  17. print $foo //(Printing Variables)
  18. ?>
Copyright exforsys.com


echo

echo — Output one or more strings


Example:


void echo ( string $arg1 $arg2 );


Outputs all parameters.


echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.


Various Method of Using echo:


Sample Code
  1. <?php
  2. echo "Hello World"
  3. echo "This spans
  4. multiple lines. The newlines will be
  5. output as well"
  6. echo "This spans multiple lines. The newlines will be n output as well."
  7. echo "Escaping characters is done "Like this"."
  8. echo $foo
  9. echo $foo,$bar
  10. echo <<<END
  11. This uses the "here document" syntax to output
  12. multiple lines with $variable interpolation. Note
  13. that the here document terminator must appear on a
  14. line with just a semicolon. no extra whitespace!
  15. END
  16. echo $some_var ? 'true': 'false'
  17. ?>
Copyright exforsys.com


sprintf

sprintf returns a string produced according to the formatting string format.  Type Specifier that says what type the argument data should be treated as. The conversion specifiers are useful to format or transform the values. Each conversion specifier starts with a single percent symbol%) and ends with a conversion character (one ofb, c,d,f,o,s, u,x, orX ). Please refer http://in2.php.net/manual/en/function.sprintf.php for complete list of specifiers along with descriptions.


Example


 


Sample Code
  1. <?php
  2. $str = "Hello"
  3. $number = 123
  4. $txt = sprintf("%s world. Day number %u",$str,$number)
  5. echo $txt
  6. ?>
Copyright exforsys.com


Output


Hello world. Day number 123


print_r:

Prints human-readable information about a variable


print_r (mixed $expression [, bool $return])


print_r() displays information about a variable in a way that's readable by humans. print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown.  Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.  Main practical use of print_r() function is to output array values.


Example:


Sample Code
  1. <?php
  2. $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'))
  3. print_r ($a)
  4. ?>
Copyright exforsys.com


Output:


Array
(
    [a] => apple
    [b] => banana
    [c] => Array
    (
        [0] => x
        [1] => y
        [2] => z
    )
)


var_dump:

Var_dump is used to get information about a variable.   This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure. In PHP 5 all public, private and protected properties of objects will be returned in the output.


Example:


Sample Code
  1. <?php
  2. $a = array(1, 2, array("a", "b", "c"))
  3. var_dump($a)
  4. ?>
Copyright exforsys.com



Output:


array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
        array(3) {
            [0]=>
            string(1) "a"
            [1]=>
            string(1) "b"
            [2]=>
            string(1) "c"
        }
}


The echo and print functions are similar is all aspects. The only difference between them is that when items are separated using a comma, the echo function performs faster.




First Page: PHP Strings


Read Next: How to Use Cookies in PHP



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape