Outputting the Value of a Variable

suggest change

Introduction

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.

Remarks

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:

Note: When you output an object as a string, PHP calls __toString() if the object has that method. If it does not, PHP 8 raises Error: Object of class CLASS could not be converted to string. Inspect the object with print_r or var_dump instead; see Outputting a structured view of arrays and objects.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents