【问题标题】:PHP Parse syntax error in if statement [closed]if语句中的PHP Parse语法错误[关闭]
【发布时间】:2014-01-09 06:47:50
【问题描述】:

我有这个伪代码,我一直在尝试为和创建正确的 PHP 语法 我绝对无处可去。我继续在函数 group_job_items 上收到 Parse 错误。

解析错误:语法错误,第 17 行出现意外的“{”

这里有什么问题?任何帮助将不胜感激。

function group_job_items ()
{
    $jobs_by_category = array();
    foreach ($category_name as $category_id => $name)
    {
        foreach ($job_item as $item)
        {
            // skip job items that do not match the current category
            if ($item["category_id"] != $category_id) continue;

            if (!isset ($jobs_by_category[$name])
            {
                // create a list of jobs for the current category
                $jobs_by_category[$name] = array();
            }

            // add the current job item to the current category
            $jobs_by_category[$name][] = $item
        }
    }
}

【问题讨论】:

  • 计算左括号和右括号;你错过了一个结束。

标签: php if-statement foreach


【解决方案1】:
<?php

function group_job_items ()
{
    $jobs_by_category = array();
    foreach ($category_name as $category_id => $name)
    {
        foreach ($job_item as $item)
        {
            // skip job items that do not match the current category
            if ($item["category_id"] != $category_id) continue;

            if (!isset ($jobs_by_category[$name]))
            {
                // create a list of jobs for the current category
                $jobs_by_category[$name] = array();
            }

            // add the current job item to the current category
            $jobs_by_category[$name][] = $item;
        }
    }
}
?>

【讨论】:

    【解决方案2】:

    改变...

    if (!isset ($jobs_by_category[$name])
                {
                    // create a list of jobs for the current category
                    $jobs_by_category[$name] = array();
                }
    

    if (!isset ($jobs_by_category[$name]))
                {
                    // create a list of jobs for the current category
                    $jobs_by_category[$name] = array();
                }
    

    【讨论】:

      【解决方案3】:

      提交的代码有一些语法错误。

      此行末尾缺少右括号/括号:

      if (!isset ($jobs_by_category[$name])
      

      此行末尾缺少分号:

      $jobs_by_category[$name][] = $item
      

      进行这些更正应该可以解决 PHP 解析器错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-30
        • 1970-01-01
        相关资源
        最近更新 更多