我通常会设置所有变量,即使它们可能不会返回任何东西。但现在我想知道:我必须在PHP中声明我的变量吗?
如果我回显一个空变量会有什么危害?
例如。
<?php
$a = 'a';
$b = 'b';
if($a==$b)
{
$data = 'Yes they where the same';
};
echo $data;
?>
为什么我要这样做?
<?php
$data = ''; // declare varibale
$a = 'a';
$b = 'b';
if($a==$b)
{
$data = 'Yes they where the same';
};
echo $data;
?>
感谢您的回答!
扩展问题:
什么是最好的做法:
$data = '';
或
$data;
或使用
if (isset($data)){
echo $data;
}
变量...不是varibale – Macmade
启用错误/错误报告以及'$ a'和'$ b'的不同值来自己测试。 – nickb