Tony Stark Tony Stark - 1 month ago 6
PHP Question

want to print out the name of the variable and its value to browser

This question may sounds way too simple for the php experts out there, but 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. Could someone please help me out? Thanks in advance for any help!

Answer

You can simply do this:

echo "\$myVar: $myVar";

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

Comments