我通常會設置所有變量,即使它們可能不會返回任何東西。但現在我想知道:我必須在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