【发布时间】:2020-12-27 12:01:42
【问题描述】:
我尝试解决这个练习,但我不知道如何寻找解决方案。根据我目前的知识,我的程序应该打印出 smg,但我只得到 error 。
错误信息:
Warning: Undefined variable $x in /in/NVFqg on line 6
Charile bit your finger!
这是我的代码:
<?php
function isBitten()
{
if ($x <= 50) { // line 6 //
echo "Charile bit your finger!";
$x = rand(0, 100);
}
else {
echo "Charlie did not bite your finger";
}
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Charlie</title>
</head>
<body>
<?php
isBitten();
?>
</body>
</html>
非常感谢您的帮助!谢谢!
【问题讨论】:
-
在尝试读取变量之前需要定义变量。这里:
if ($x <= 50),您还没有定义$x,但正在尝试比较它,这就是引发未定义变量错误的原因。
标签: php if-statement random