如何判断变量是否为数字以及是否为偶数?

时间:2014-10-11 19:32:00

标签: php

我需要我的脚本告诉我变量是否为数字,如果是,则是否为偶数。到目前为止我的代码:

        $Numbers = array("1", 2, "three", 4, 5, "six");

        foreach ($Numbers as $Value) {
            if (is_numeric($Value)) {
                if ($Value % 2 == 0) {
                    echo "$Value is numeric, and even";
                } else {
                    echo "$Value is numeric, but not even";
                }
            } else {
                echo "$Value is not numeric";
            }

我的浏览器显示空白页。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

语法错误。关闭foreach循环

 $Numbers = array("1", 2, "three", 4, 5, "six");

        foreach ($Numbers as $Value)
        {
            if (is_numeric($Value))
            {
                if ($Value % 2 == 0)
                {
                    echo "$Value is numeric, and even";
                }
                else
                {
                    echo "$Value is numeric, but not even";
                }
            }
            else
            {
                echo "$Value is not numeric";
            }
}