Output Methods
Output methods are used to display the data or output on screen. Various methods are provided by PHP for the same. These methods and their usage is as described below:
print method
Print statement is used to display output on screen. It can be used with or without braces like print or print()
. This method returns a bool value TRUE on call and can accept only one argument.
Code example usign print
statement to display text and variables
echo method
echo
is the most commonly used statement or method for printing in PHP. It can be used with or without braces like echo or echo()
. This method of printing is faster because it does not return any value. An example to display text and variables using echo statement.It can also accept multiple parameters as its input.
Code example using echo
statement to display text and variables
printf method
This method of output uses format specifiers in order to display output on screen. It is also used in C/C++ language.
Code example using printf
method
sprintf method
sprintf
writes the formatted string to a variable.Type handling is also defined as under:
Type |
Specifiers |
String |
s |
Integer |
d,u,c,o,x,X,b |
Double |
g,G,e,E,f,F |
Code exmaple using sprintf
method
var_dump method
var_dump
method in PHP is used to dump information about one or more variables. This structured information includes its type and value. Arrays and objects are also explored in order to show their structure.
var_dump(variable1, variable2, variable3,....); //syntax
Code example using var_dump
method
print_r method
print_r
method is used to display information about a variable in a more human readable form.
print_r(variable, return); //syntax
Parameter |
Description |
variable |
The variable to return information about. |
return |
If set true, the function will not print the information and only returns. Default is false |
Code example using print_r
method