How to Display PHP Variable Values

You can display the value in a PHP variable on a web page by using any of the following statements: echo, print, print_r, or var_dump.

Variables in echo and print statements

You can display the value in a variable on a web page with an echo or print statement. For instance, if you set the $age variable to 12 and then use the following PHP echo statement in a PHP section, the output is 12.

echo $age;

If you include the following line in an HTML file:

<p>Your age is <?php echo $age ?>.</p>

the output on the web page is

Your age is 12.

For the purposes of the table, assume that $string1 is set to Hello and $string2 is set to World!.

print Statements
print Statement Output
print $string1; Hello
print $string1,$string2; HelloWorld!
print "$string1 $string2"; Hello World!
print "Hello ",$string2; Hello World!
print "Hello"," ",$string2; Hello World!
print '$string1',"$string2"; $string1World!

Single and double quotes have different effects on variables, as follows.

  • Single quotes (' '): When you use single quotes, variable names are echoed as is.

  • Double quotes (" "): When you use double quotes, variable names are replaced by the variable values.

Sometimes you need to enclose variable names in curly braces ({ }) to define the variable name. For instance, the following statements won’t output bird as the $pet variable.

$pet = "bird";
echo "The $petcage has arrived.";

In other words, the output won’t be The birdcage has arrived. Rather, PHP will look for the variable $petcage and won’t be able to find it. You can echo the correct output by using curly braces to separate the $pet variable:

$pet = "bird";
echo "The {$pet}cage has arrived.";

The preceding statement gives you

The birdcage has arrived.

How long a variable holds its value

A variable keeps its information for the entire script, not just for a single PHP section. If a variable is set to “yes” at the beginning of a file, it will still hold “yes” at the end of the page. For instance, suppose your file has the following statements:

<p>Hello World!</p>
<?php
    $age = 15;
    $name = "Harry";
?>
<p>Hello World again!</p>
<?php
    echo $name;
?>

The echo statement in the second PHP section will display Harry. The web page resulting from these statements is

Hello World!
 
Hello World again!
 
Harry

Display variables with print_r statements

PHP provides a function named print_r for looking at the value in a variable. You can write the following statements to display a variable value:

$weekday = "Monday";
print_r($weekday);

The output from print_r is

Monday

Display variables with var_dump statements

PHP provides a function named var_dump that you can use to display a variable value and its data type.

You can write the following statements to display a variable value:

$weekday = "Monday";
var_dump($weekday);

The output of var_dump is

string(6) "Monday"

The output shows that the value in $weekday is Monday. The output also shows that the value is a string data type that is six characters long.

You'll use var_dump frequently for troubleshooting PHP. Its use is essential for that purpose.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com