Print functions in PHP

The important Print functions in PHP are..

echo()
    • The echo() function outputs one or more strings.
  • Syntax
    • echo(strings)
  • Note:
    • The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.
    • The echo() function is slightly faster than print().
print():
    • The print() function outputs one or more strings.
  • Syntax
    • print(strings)
  • Note:
    • The print() function is not actually a function, so you are not required to use parentheses with it.
    • The print() function is slightly slower than echo().
    • We can't print multiple statements using the print() function.
printf()
    • The printf() function outputs a formatted string.
  • Syntax
    • printf(format,arg1,arg2,arg++)
  • The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.
  • Eample
    • <?php
      $fName = "Suneel";
      $number = 20;
      printf(""Hi %s, your number %u",$fName,$number);
      ?>
  • Output:
    • "Hi Suneel, your number 123
  • sprintf()
    • The sprintf() function writes a formatted string to a variable.
Syntax
    • sprintf(format,arg1,arg2,arg++)
  • The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.
  • Note:
  • If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$".
  • Example 1
    • <?php
      $fName = "Suneel";
      $number = 20;
      $txt = sprintf("Hi %s, your number %u",$fName,$number);
      echo $txt;
      ?>
  • Output
    • "Hi Suneel, your number 123

0 comments :

Post a Comment

> Related Posts with Thumbnails
 

Copyright © 2012. GS dot net - All Rights Reserved - Design by BTDesigner - Proudly powered by Blogger