什么是“解析错误:语法错误,意外”')'“是什么意思?

时间:2014-12-30 05:03:48

标签: php

为什么我在PHP中收到以下错误?

<?php

    require_once('validation_functions.php');
    $errors = array();

    //$username = trim($_POST["username"]);
    $username = trim("");

    if(!has_presence($username))
    {
        $errors['username'] = "Username can't be left blank";
    }
?>

<?php echo form_errors($errors); ?>

这是以下问题:

  在第20行的C:\ wamp \ www \ sandbox \ validation_functions.php中

解析错误:语法错误,意外')'

这是上面详述的文件validation_functions.php

 <?php

function has_presence($value)
{
    //IF $value is SET AND value is NOT EXACTLY EQUAL TO "" RETURN TRUE 
    return (isset($value) && $value !== ""); //RETURNS BOOLEAN
}

function has_max_length($value, $max)
{
    //IF $value is LESS THAN OR EQUAL TO $max Return TRUE
    return (strlen($value) <= $max)); //RETURNS BOOLEAN 
}

function has_inclusion_in($value, $set)
{
    //IF $value is INCLUDED in $set RETURN TRUE 
    return in_array($value, $set); //RETURNS BOOLEAN
}

function form_errors($errors = array())
{
    $output = "";

    if(!empty($errors))
    {
        $output = "<div class= \"error\">";
        $output .= "Please Fix The Following Errors:";
        $output .= "<ul>";

        foreach($errors as $key => $error)
        {
            $output .= "<li> {$error} </li>";
        }

        $output .= "</ul>";
        $output .= "</div>";
    }

    return $output;
  }

 ?>

这包含在上面引用的代码中。这是一个简单的错误,但我似乎无法理解是什么导致它。

2 个答案:

答案 0 :(得分:0)

标记的行中有一个额外的括号

function has_max_length($value, $max)
{
    //IF $value is LESS THAN OR EQUAL TO $max Return TRUE
 >>>>>>>>>   return (strlen($value) <= $max)); //<<<< here is a extra bracket, remove the last one!

应该是这样的:

return (strlen($value) <= $max)

答案 1 :(得分:0)

  

正确方法:返回(strlen($ value)&lt; = $ max);

function has_max_length($value, $max)
{
//IF $value is LESS THAN OR EQUAL TO $max Return TRUE
return (strlen($value) <= $max); //RETURNS BOOLEAN 

}