I usually set all my variables, even if they might not return anything. But now I wonder:
What harm will it make if I echo an empty variable?
eg.
<?php
$a = 'a';
$b = 'b';
if($a==$b)
{
$data = 'Yes they where the same';
};
echo $data;
?>
Why must I do like this?
<?php
$data = ''; // declare varibale
$a = 'a';
$b = 'b';
if($a==$b)
{
$data = 'Yes they where the same';
};
echo $data;
?>
Thanks for your answers!
Extending the question:
What is best practice:
$data = '';
or
$data;
or using
if (isset($data)){
echo $data;
}
There is no “empty” variable if you haven’t defined it. So you’re trying to output something that doesn’t exist. Obviously it makes no sense and causes notices (try to run the script with
E_ALL)