PHP函数中的语法错误,不知道为什么

时间:2015-01-01 08:45:48

标签: php

我收到语法错误,我无法弄清楚原因。 "解析错误:语法错误,意外' {'在第6行"

function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} else (is_array($customFields)) {
$parsed = $customFields;
}

// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}

return $returnField
}

7 个答案:

答案 0 :(得分:1)

你忘了在$returnField之后加分号,也使用elseif而不是别的(否则不需要任何参数)。使用以下代码

function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} elseif (is_array($customFields)) {
$parsed = $customFields;
}

// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}

return $returnField;
}

希望这有助于你

答案 1 :(得分:0)

else后面没有任何表达。删除表达式或改为使用elseif

答案 2 :(得分:0)

你错过了分号

return $returnField;
}

另外条件else (is_array($customFields))格式错误....否则不要采取任何条件......你可以改用Elseif

答案 3 :(得分:0)

else (is_array($customFields)) {

无效。如果或删除条件

,请使用其他方法

答案 4 :(得分:0)

function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == true && $customFields != null) {
$parsed = array($customFields);
} else {
$parsed = $customFields;
}

// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}

return $returnField;
}

答案 5 :(得分:0)

它应该是if(is_array($ customFields))或elseif(is_array($ customFields))

答案 6 :(得分:0)

我已经修改了你的函数,我看到应该是和elseif而不是其他在foreach循环中,代码中的语法是错误的。我还假设您将使用单级关联数组。

<?php
    function ExtractCustomField($fieldName, $customFields) {
            // $customFields might be an object, NULL, or an array.
            $parsed = array();
            if (is_array($customFields) == false && $customFields != null) {
                $parsed = array($customFields);
            } elseif (is_array($customFields)) {
                $parsed = $customFields;
            }

            // loop through the fields and find the one we are looking for
            $returnField = null;
            foreach($customFields as $field) {
                if ($field == $fieldName) {
                    $returnField = $field;
                    break;
                }
            }
            return $returnField;
        }

        echo ExtractCustomField('name', array('name','emial','mobile','password'));
?>