PHP 函数如果函数 == true

PHP function if function == true

你好,我对奇数或偶数有疑问,我想在此基础上执行功能,但我收到错误:解析错误:语法错误,意外'spocti'(T_STRING), 在 C:\xampp\htdocs\functions.php 中期待 '(' 在第 34 行 我有这个代码

<form method= "post" action= "">
    <input type= "text" name= "cislo" />
    <input type= "submit" name= "submit" />
 </form>

    <?php
        if (isset($_POST['cislo'])){

            $cislo = $_POST['cislo'];

            function spocti(){
                if ($cislo % 2 == 0)
                return true;
            }
        }
        if (function spocti == true){
            echo "Its even";
        }
     ?>

阅读 PHP manual 可能有帮助:

首先,您的函数需要接受一个值参数,否则您正在测试的值将不会在函数范围内可用

其次,您通过分配给该函数的名称调用该函数

第三,你在调用函数的时候传递了你要测试的值

function spocti($value){
    if ($value % 2 == 0)
        return true;
    }
}

if (spocti($cislo) == true){
    echo "Its even";
}

请注意,对于 returns 布尔值 true/false 的函数,您不需要在 if 测试 [=15] 中使用与 true 的比较=]

if (spocti($cislo)){
    echo "Its even";
}