PHP Outputting the Value of a Variable

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


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
  • print - Outputs a string and returns 1 (always)
  • 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 string will be shown. In this case, you'll have to inspect the object further, see: outputting-a-structured-view-of-arrays-and-objects.

Got any PHP Question?