Tony Stark Tony Stark - 2 months ago 7
PHP Question

How to print out the name of the variable and its value to browser?

I have just started learning PHP, so please forgive me for asking a question like this.

I have a variable

$myVar = "hello";
and I want to print out the name of the variable and its value to the browser.

I tried

echo "$myVar: $myVar"; //this simple prints out the value of $myVar twice


also tried

echo '$myVar'+ ":$myVar"; //somehow the number 0 was printed out to the browser


also tried

echo '$myVar: $myVar'; //this just prints out the string literal itself


I understand the difference between double quotes and single quotes, but just can't seem to get this simple example working.

Answer

You can simply do this:

echo "\$myVar: $myVar";

Use backslash \ to display the variable name as it is.