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:

Specifier
Description
%b
binary
%c
ASCII character
%d
signed decimal number
%e
scientific number
%u
unsigned decimal number
%f
float with local settings
%F
float without local settings
%o
octal number
%x
lowercase hexadecimal
%X
uppercase hexadecimal

:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort, krsort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file, filesize
 • mkdir, rmdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home