任何人都可以告訴我爲什麼我的角色統計不會打印?我沒有收到任何錯誤或語法消息..爲什麼我的變量不打印? PHP
這是我的第一個php項目。我無法弄清楚我錯過了什麼!
<?
class character{
public $healthpoints = 100;
public $isdead = false;
public $class = "Mage";
public $level = 10;
}
function checkdeath() {
if($healthpoints >= 0){
$isdead = true;
}
}
new character();
function CharStats() {
echo $healthpoints;
echo $isdead;
echo $class;
echo $level;
}
CharStats;
?>
這是一個可變範圍問題 - http://php.net/manual/en/language.variables.scope.php。你的類變量不在你函數的範圍內。簡單的解決方案是將你的函數作爲類方法添加到你的類中 – Sean
它從讀取錯誤消息開始:https://3v4l.org/he30R – hakre