首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP语法错误--使用汇编语言

PHP语法错误--使用汇编语言
EN

Stack Overflow用户
提问于 2017-12-06 09:47:10
回答 2查看 530关注 0票数 0

这是我写的代码,它不起作用。它应该询问用户一个奇数,并检查它是否正确。

代码:

代码语言:javascript
复制
$guess = $_POST['guess'];
$submit = $_POST['submit'];

if(isset($submit)){
        if ($guess/2 %0){
                echo "the number you are guessing is not odd";
        }elseif{

         ($guess<$rand)
                echo "Your Guess is Smaller than the secret number";
        }elseif{
                ($guess>$rand)
                         echo "Your Guess is bigger than the secret number";
        }else{
                          ($guess==$rand)
        echo "you guessed currectly. now try anouthe number!";}





else
        header("Location: index.php");
                exit();}

?>
EN

回答 2

Stack Overflow用户

发布于 2017-12-06 09:50:56

你能试试这个吗?

如果你把'()‘放错了,你把’()‘放进了你的“()”。

代码语言:javascript
复制
<?php
$rand = rand(1, 99);

$guess  = $_POST['guess'];
$submit = $_POST['submit'];

if(isset($submit))
{
    if($guess / 2 % 0)
    {
        echo "the number you are guessing is not odd";
    }
    elseif($guess < $rand)
    {
        echo "Your Guess is Smaller than the secret number";
    }
    elseif($guess > $rand)
    {
        echo "Your Guess is bigger than the secret number";
    }
    elseif($guess == $rand)
    {
        echo "you guessed currectly. now try anouthe number!";
    }
}
else
{
    header("Location: index.php");
    exit();
}

?>

我还没有测试这段代码,所以我需要你的反馈。编辑:,你已经证实了这一点。

我想给你提供关于elseifhttp://php.net/manual/en/control-structures.elseif.php的手册

请考虑更容易/更简洁的编码。就我个人而言,我喜欢使用':‘而不是'{}',当您使用HTMLPHP混合使用时,代码更少,更容易阅读,例如:

代码语言:javascript
复制
<?php
$rand = rand(1, 99);

$guess  = $_POST['guess'];
$submit = $_POST['submit'];

if(isset($submit)):
    if($guess / 2 % 0):
        echo "the number you are guessing is not odd";
    elseif($guess < $rand):
        echo "Your Guess is Smaller than the secret number";
    elseif($guess > $rand):
        echo "Your Guess is bigger than the secret number";
    elseif($guess == $rand):
        echo "you guessed currectly. now try anouthe number!";
else:
    header("Location: index.php");
    exit();
endif;
?>

不要忘记检查$_POST数据。

array也是如此,但这是一个附带注意事项:

代码语言:javascript
复制
$arr = array(1 => 'hi', 2 => 'hello'); // old
$arr = [1 => 'hi', 2 => 'hello']; // new
票数 3
EN

Stack Overflow用户

发布于 2017-12-06 09:52:31

这不是php中的if-else构造的正确语法。

elseif部件需要在它之后有一个条件(在打开大括号之前),而else完全不需要条件。

代码语言:javascript
复制
if ($guess/2 %0){
        echo "the number you are guessing is not odd";
} elseif ($guess<$rand) {
    // ....
} else {
    echo "you guessed currectly. now try anouthe number!";
}

当然,在进行其他操作之前,您必须确保ifelseif匹配所有“错误”情况。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47671211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档