Displaying Output

echo vs. print

Is there really any difference at all? Sure! Is it significant? Nope. In most cases, you should just use echo, for 3 reasons:

First, echo is slightly faster than print. If you were to run echo 10 million times and run print 10 million times, you’d probably see that echo is about 10-20 milliseconds faster on average. So the performance difference is pretty insignificant.

The reason for this is because print actually returns a value (it always returns the number 1), whereas echo does not return anything. That can be useful in some more complex scenarios, but it’s pretty rare.

Second, echo is one character shorter. One less character is one less character.

Third, most people use echo, and conformity can be good here.

<?=$variable ?>

Every once in a while, someone will post a little code snippet like this:

Hello, <?=$first_name;?>

The <?=$variable ?> syntax (the “;” at the end is optional) is simply shorthand for <?php echo $variable; ?>

Inevitably, someone (it used to be me) says, “You shouldn’t do that! The short_open_tag setting could be disabled on someone’s server, and it’s also looking like it’s going to be deprecated.”

Both of these last two possibilities are true, but the <?= tag is not actually tied to the short_open_tag setting. In fact, the <?= shortcut is globally available for PHP 5.4 and later, so it’s pretty safe to use at this point.

print_r(), var_export, and var_dump()

Sometimes it’s helpful when debugging to be able to quickly dump out the contents of more complex values like arrays and objects. You can use any of these 3 functions to do that, but they have slightly different outputs. Let’s see what they look like with this little script:

<?php
$arr = ["foo","bar"];

class ExampleObject
{
  private $foo = "Private Foo";
  public $bar = "Public Bar";
  public $null_field;
  public $bool_field = false;

}

$obj = new ExampleObject();

echo "########## PRINT_R ##########\n";
print_r($arr);
echo "\n";
print_r($obj);
echo "\n";
echo "\n";
echo "########## VAR_EXPORT ##########\n";
var_export($arr);
echo "\n";
var_export($obj);
echo "\n";
echo "\n";
echo "########## VAR_DUMP ##########\n";
var_dump($arr);
echo "\n";
var_dump($obj);

The output:

########## PRINT_R ##########
Array
(
    [0] => foo
    [1] => bar
)

ExampleObject Object
(
    [foo:ExampleObject:private] => Private Foo
    [bar] => Public Bar
    [null_field] => 
    [bool_field] => 
)


########## VAR_EXPORT ##########
array (
  0 => 'foo',
  1 => 'bar',
)
ExampleObject::__set_state(array(
   'foo' => 'Private Foo',
   'bar' => 'Public Bar',
   'null_field' => NULL,
   'bool_field' => false,
))

########## VAR_DUMP ##########
array(2) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
}

object(ExampleObject)#1 (4) {
  ["foo":"ExampleObject":private]=>
  string(11) "Private Foo"
  ["bar"]=>
  string(10) "Public Bar"
  ["null_field"]=>
  NULL
  ["bool_field"]=>
  bool(false)
}

You’ll notice that the print_r() output is often significantly easier to read, but it has a very significant weakness, which you can see if you look closely at the null_field and bool_field output.

They’re blank!

This is because print_r() is very similar to just looping through all the array elements/fields/properties and simply echo-ing the values. If you’ve ever tried to echo a null or echo a false value, you won’t see anything show up. And if you try to echo a “true” boolean, you’ll see a 1 show up. That’s just how PHP displays those particular value types when they’re treated as if they’re strings.

Meanwhile, both var_export and var_dump very clearly provide more technical details, and that includes the display of whether something is null, or true, or false.

The var_export function is essentially intended to output code that could be copied-and-pasted into a PHP script and used as valid code. Whether you really will need to do that very much is debatable, but there you have it.

The var_dump function is the most technical, showing you the data types and lengths of the values, which can be extremely valuable information when debugging data issues.

The one advantage of both print_r and var_export is that you can pass true as a second parameter and their output will be returned from the function instead of just displayed. This can be useful when writing to a debug log file instead of printing output to the screen, for example:

file_put_contents("my_debug_log.txt", "The \$my_array contains: " . print_r($my_array,true));

However, in a pinch, you can use ob_start() and ob_get_clean() to capture the output of var_dump() into a variable, if you really want to. That said, var_export is a great middle-ground option, still being pretty readable while rendering the values fairly accurately.

Leave a Reply

Your email address will not be published.

*