Outputting the Value of a Variable
To build a dynamic and interactive PHP program, it is useful to output variables and their values. The PHP language allows for multiple methods of value output. This topic covers the standard methods of printing a value in PHP and where these methods can be used.
Variables in PHP come in a variety of types. Depending on the use case, you may want to output them to the browser as rendered HTML, output them for debugging, or output them to the terminal (if running an application via the command line).
Below are some of the most commonly used methods and language constructs to output variables:
echo- Outputs one or more strings
printf- Outputs a formatted string and returns the length of the outputted string
sprintf- Formats a string and returns the formatted string
print_r- Outputs or returns content of the argument as a human-readable string
var_dump- Outputs human-readable debugging information about the content of the argument(s) including its type and value
var_export- Outputs or returns a string rendering of the variable as valid PHP code, which can be used to recreate the value.
Note: When trying to output an object as a string, PHP will try to convert it into a string (by calling
__toString()- if the object has such a method). If unavailable, an error similar to
Object of class [CLASS] could not be converted to stringwill be shown. In this case, you'll have to inspect the object further, see: outputting-a-structured-view-of-arrays-and-objects.
echo and print
echo("test") won't do any harm either). They output the string representation of a variable, constant, or expression. They can't be used to print arrays or objects.
Assign the string
Joelto the variable
Output the value of $name using
Parentheses are not required, but can be used
Using multiple parameters (only
echo, is an expression (it returns
1), and thus can be used in more places:
The above is equivalent to:
Shorthand notation for
When outside of PHP tags, a shorthand notation for
echo is available by default, using
<?= to begin output and
?> to end it. For example:
Note that there is no terminating
;. This works because the closing PHP tag acts as the terminator for the single statement. So, it is conventional to omit the semicolon in this shorthand notation.
and operators and has left association.
Same example with brackets:
In short, there are two main differences:
echocan have multiple parameters.
Output a Multidimensional Array with index and value and print into the table
Output Multidimensional Array with index and value in table
Outputting a structured view of arrays and objects
print_r() - Outputting Arrays and Objects for debugging
print_r will output a human readable format of an array or object.
You may have a variable that is an array or object. Trying to output it with an
echo will throw the error:
Notice: Array to string conversion. You can instead use the
print_r function to dump a human readable format of this variable.
You can pass true as the second parameter to return the content as a string.
This outputs the following:
Further, the output from
print_r can be captured as a string, rather than simply echoed. For instance, the following code will dump the formatted version of
$myarray into a new variable:
Note that if you are viewing the output of PHP in a browser, and it is interpreted as HTML, then the line breaks will not be shown and the output will be much less legible unless you do something like
Opening the source code of a page will also format your variable in the same way without the use of the
Alternatively you can tell the browser that what you're outputting is plain text, and not HTML:
var_dump() - Output human-readable debugging information about content of the argument(s) including its type and value
The output is more detailed as compared to
print_r because it also outputs the type of the variable along with its value and other information like object IDs, array sizes, string lengths, reference markers, etc.
You can use
var_dump to output a more detailed version for debugging.
Output is more detailed:
Note: If you are using xDebug in your development environment, the output of var_dump is limited / truncated by default. See the official documentation for more info about the options to change this.
var_export() - Output valid
var_export() dumps a
PHP parseable representation of the item.
You can pass true as the second parameter to return the contents into a variable.
Output is valid PHP code:
To put the content into a variable, you can do this:
After that, you can output it like this:
This will produce the following output:
Outputting large integers
On 32-bits systems, integers larger than
PHP_INT_MAX are automatically converted to float. Outputting these as integer values (i.e. non-scientific notation) can be done with
printf, using the
float representation, as illustrated below:
Note: watch out for float precision, which is not infinite!
While this looks nice, in this contrived example the numbers can all be represented as a binary number since they are all powers of 1024 (and thus 2). See for example:
printf vs sprintf
printf will output a formatted string using placeholders
sprintf will return the formatted string
It is also possible to format a number with these 2 functions. This can be used to format a decimal value used to represent money so that it always has 2 decimal digits.
String concatenation with echo
You can use concatenation to join strings "end to end" while outputting them (with
You can concatenate variables using a
Similar to concatenation,
echo (when used without parentheses) can be used to combine strings and variables together (along with other arbitrary expressions) using a comma (,).
String concatenation vs passing multiple arguments to echo
Passing multiple arguments to the echo command is more advantageous than string concatenation in some circumstances. The arguments are written to the output in the same order as they are passed in.
The problem with the concatenation is that the period
. takes precedence in the expression. If concatenated, the above expression needs extra parentheses for the correct behavior. The precedence of the period affects ternary operators too.