×
☰ See All Chapters

PHP Output Functions

PHP print

print function displays output on browsers and returns 1 if output is printed successfully, otherwise returns nothing. Sinlge print function cannot print multiple statements. Print value can be passed to print function with or without parentheses.  print "Hello Word!"; and print ("Hello Word!"); both are same.

PHP print example

<?php

$print_return_value = print "Hello Word!";

print "</br>".$print_return_value

?>  

Output:

php-output-functions-0

PHP echo

Using echo function you can print multiple values, values must be separated by comma. echo function does not return any value. As echo function does not return any value it is faster than print function.

PHP echo example

<?php

echo "Hello", "Word!";

?>  

Output:

php-output-functions-1
 

PHP var_dump

var_dump function displays the variable value along with its data type.

PHP var_dump example

<?php

 

$string_var = "Hello world!";

echo var_dump($string_var). "<br>";

 

$integer_var = 10;

echo var_dump($integer_var) . "<br>";

 

$floating_var = 11.5;

echo var_dump($floating_var) . "<br>";

 

$array_var = array(10, "Hello world!", 11.5, array("one", "two", "three"));

echo var_dump($array_var) . "<br>";

 

?>  

Output:

php-output-functions-2
 

PHP printf

Using printf function you can display output with the help of format specifiers.

Below is the different format specifiers supported.

%% - Returns a percent sign

%b - Binary number

%c - The character according to the ASCII value

%d - Signed decimal number (negative, zero or positive)

%e - Scientific notation using a lowercase (e.g. 1.2e+2)

%E - Scientific notation using a uppercase (e.g. 1.2E+2)

%u - Unsigned decimal number (equal to or greather than zero)

%f - Floating-point number (local settings aware)

%F - Floating-point number (not local settings aware)

%g - shorter of %e and %f

%G - shorter of %E and %f

%o - Octal number

%s - String

%x - Hexadecimal number (lowercase letters)

%X - Hexadecimal number (uppercase letters)

PHP printf example

<?php

$count = 10;

$fruit = "Apple";

printf("I have %u %s.", $count, $fruit);

?>

Output:

php-output-functions-3
 

All Chapters
Author