PHP print, print_r, printf



print is similiar to echo, it print out string on the screen. Similarly, when double quotes are used, variable's value will be print out, while single quotes will print out the variable name only.

<?PHP
	print "PHP Tutorial"; //PHP Tutorial
	print 'PHP Tutorial'; //PHP Tutorial
	$a="Joni";
	print "$a PHP Tutorial"; //Joni PHP Tutorial
	print '$a PHP Tutorial'; //$a PHP Tutorial
?>

Special characters include double quote, single quote should be escaped using \ symbol. Strings can be concatenated using symbol while printing.
<?PHP
	print "John's daughter";  //John's daughter
	print 'John\'s daughter';  //John's daughter
	print 'John"s daughter';  //John"s daughter
	print "John\"s daughter";  //John"s daughter
	$arr = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
	for($i=1;$i<=count($arr);$i++)
	{
	   print $i . " is: " . $arr[$i-1] . ".";
	}
	1 is: Monday.
	2 is: Tuesday.
	3 is: Wednesday.
	4 is: Thursday.
	5 is: Friday.
	6 is: Saturday.
	7 is: Sunday.
?>

print can not use heredoc symbol <<<. It always return value 1.

print_r() function provides a convenient way to print out a varaible's value, including string, number, array, objects etc.
<?PHP
	$a="4";
	print_r($a);  //4
	$b="This is a string";  
	print_r($b);  //This is a string
	$arr = array("Monday","Tuesday","Wednesday","Thusday","Friday","Saturday","Sunday");
	print_r($arr);
	//Array ( [0] => Monday [1] => Tuesday [2] => Wednesday [3] => Thusday [4] => Friday [5] => Saturday [6] => Sunday ) 
	unset($arr[3]);
	print_r($arr);
	//Array ( [0] => Monday [1] => Tuesday [2] => Wednesday [4] => Friday [5] => Saturday [6] => Sunday ) 
?>

var_dump() function is similiar to print_r().

printf() function formats string and then prints out on the screen. sprintf() function formats string and returns the string.
<?PHP
	$a=33400;
	printf("%e",$a); //3.340000e+4
	printf("%b",$a); //1000001001111000
	printf("%b in scientific format is %e",$a,$a); //33400 in scientific fomrat is 3.340000e+4
	$s=sprintf("%b in scientific format is %e",$a,$a);
    echo "$s"; //33400 in scientific fomrat is 3.340000e+4
?>

printf() format specifiers:
%bbinary
%cASCII character
%dsigned decimal number
%escientific number
%uunsigned decimal number
%ffloat with local settings
%Ffloat without local settings
%ooctal number
%xlowercase hexadecimal
%Xuppercase hexadecimal