0

我已经尝试了大约 2 个小时,但我找不到错误。一个变量从一个 html 表单传递到这个页面,我正在尝试为“医院”这个词创建一个过滤器,并将其重新拼写为“医院”。错误可能是微不足道的,但我找不到它。任何帮助将不胜感激。

<?php
if ($_POST["location"] == "hospitol") 
    function convertSpace($string){
        return str_replace("Hospitol", "Hospital", $string);
    }
    $string = $_POST["location"];
    echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
else
    $location=$_POST["location"];
?>

apache记录的错误是;

PHP Parse error:  syntax error, unexpected T_STRING, expecting '(' in
home/linux/public/job_scheduler_input_check.php on line 5, referer:
http://localhost/calendar.php

注意: apa​​che 所指的行不正确,因为我将其从较大的文件中取出。

4

5 回答 5

2

此代码的结构方式不正确,您在单行条件之后定义了一个函数。为了清楚起见,您应该使用 {} 括号正确地包围您的陈述。该函数也应该在开始时定义。

<?php
function convertSpace($string){return str_replace("Hospitol", "Hospital", $string);}
if ($_POST["location"] == "hospitol") 
{
  $string = $_POST["location"];
  echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
}
else
{
  $location=$_POST["location"];
}
?>
于 2012-05-21T06:27:45.837 回答
1

if else 条件的 {} 开始结束标记在哪里?你的语法是错误的。应该是

    <?php
         if ($_POST["location"] == "hospitol") {
         function convertSpace($string){return str_replace("Hospitol", "Hospital", $string);
         $string = $_POST["location"];
         echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace")); }
        else {
               $location=$_POST["location"]; 
             }
 ?>
于 2012-05-21T06:27:58.243 回答
0
  <?php
    if ($_POST["location"] == "hospitol") {
    function convertSpace($string){
        return str_replace("Hospitol", "Hospital", $string);
     }
    $string = $_POST["location"];
    echo filter_var($string, FILTER_CALLBACK, array("options"=>"convertSpace"));
    } 
    else
    $location=$_POST["location"];
    ?>
于 2012-05-21T06:28:29.713 回答
0

请检查您的 if 条件,这是错误的,甚至在 if 条件下调用函数也不是一个好习惯。

于 2012-05-21T06:30:39.587 回答
0

我认为您不能只在 if 语句中声明一个函数。使用 {} 作为 if 并将函数移出。

于 2012-05-21T06:26:40.583 回答