Monday, 29 September 2014

printf in PHP

PHP printf() function is utilized to print formatted string from one or more arguments.

Syntax:

printf(format,arg1,arg2,arg++)
format : Required. Defines the string and how to format the variables in it..

arg1 : Required. The argument to be added as the first %-sign.

arg2 : Optional. The argument to be added at the second %-sign

arg++ : Optional. The argument to be added at the third, fourth etc. %-sign

Note : If there are more % signs than arguments, you must use placeholders.
Note : The only difference between printf() and sprintf() is that printf() sends the resulting string directly to the output whereas sprintf() returns the result string as its value.

Tip : Related functions: fprintf(), sprintf(), vfprintf(), vprintf() and vsprintf().

Example:

<?php
$str_name = "Morning!";
$my_name = "Jack.";
$day_num = 100;
printf("Good %s My name is %s It is day number %u",$str_name,$my_name,$day_num);
?>
Output will be:
Good Morning! My name is Jack. It is day number 100

0 comments:

Post a Comment