在使用PHP的if语句之后启动函数时出错

时间:2011-12-22 02:46:33

标签: php mysql

Haven之前并没有真正创建自己的函数,所以我尝试的是启动function NewsUpdate行下面的脚本,但我希望它们能够分开。在if语句中,当脚本找到字符串" News Update"在电子邮件中'它将启动那些脚本,如果没有(else语句一直在底部)它将失败并说"新闻更新未在电子邮件中找到"

但我收到错误:

  

解析错误:语法错误,意外T_FUNCTION

我做错了什么?

// Test code for email message.
$open_email_msg =  file_get_contents('emailmessage.html');

// A script that searches the e-mail for the string News Update,
// and if it is found it will start the function NewsUpdate

if(strpos($open_email_msg,"News Update"))
    function NewsUpdate ($open_email_msg) {
        // Login to MySQL Datebase
        $hostname = "localhost";
        $db_user = "user";
        $db_password = "pass";
        $database = "tablename";
        $db_table = "bx_news_entries";
        $db = mysql_connect($hostname, $db_user, $db_password);
        mysql_select_db($database,$db);
        $subject = 'Test News Article';
        $tags = str_replace(' ',',',$subject); // DDIE
        $uri =  str_replace(' ','-',$subject); // DDIE
        $when = strtotime("now");    // date article was posted
        $categories = 'Events';
        $content = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.';
        $snippet = 'Lorem ipsum dolor sit amet.';
        // $snippet = explode(".", $content, -1);
        # THIS CODE WILL TELL MYSQL TO INSERT THE DATA FROM THE EMAIL INTO YOUR MYSQL TABLE
        $sql = "INSERT INTO $db_table(`caption`, `snippet`, `content`, `when`, `uri`, `tags`, `categories`, `DATE`) values ('$subject', '$snippet', '$content', '$when', '$uri', '$tags', '$categories', '$when')";
        if($result = mysql_query($sql ,$db)) {
        } else {
            echo "ERROR: ".mysql_error();
        }
        echo "<h1>News Article added!</h1>";
    }

else {
    echo "<h3>'News Update</h3>not found in e-mail message!";
}

4 个答案:

答案 0 :(得分:2)

真实答案:不要像那样有条件地定义函数。这会产生讨厌的不可维护的代码。

答案不好:

        if(strpos($open_email_msg,"News Update")) {
                                                  ^---- missing bracket
            function NewsUpdate ($open_email_msg) {

同样,你的strpos CAN 会爆炸你。 strpos如果您要搜索的字符串位于字符串的开头,则返回一个整数0,然后将其解释为布尔值false。从来没有像你一样测试strpos,总是有

if (strpos(...) === FALSE) {

这将正确地捕捉'针'不在'haystack'中的情况,但仍然允许0针定位。

答案 1 :(得分:2)

将您的功能移到上方并调用它:

function NewsUpdate ($open_email_msg) {
    //function declaration
}

//Test code for email message.

$open_email_msg =  file_get_contents('emailmessage.html');

//A script that searches the e-mail for the string News Update, and if it is found it will start the function NewsUpdate

if(strpos($open_email_msg,"News Update"))
    NewsUpdate ($open_email_msg);
else {

    echo "<h3>'News Update</h3>not found in e-mail message!";

}

答案 2 :(得分:0)

好吧,尝试在if语句后添加一些{。在函数定义结束时}之后有一个;

在那里添加else语句之前,您需要一条有效的指令或一个块。

答案 3 :(得分:0)

您的语法完全不正确。函数是一个代码块,您可以在脚本中的不同位置执行。函数未在使用它的位置定义,不能在if块内定义。 编辑:正如所指出的,函数可以在主代码块内定义,甚至可以在if内有条件地定义。然而,这是一个更高级的用法,如果不小心使用,可能会让你陷入麻烦。你需要做的是在页面主要执行之外的某个地方定义你的函数,然后在if块中调用它。例如:

<?php
//main execution
if (var == true) {
   $myVar = myFunction('Bob', 'blue'); //we are calling the function and passing the values 'Bob' and 'blue' to the variables in the function definition
   echo $myVar //this will print the returned value 'Yay'
}
//end of main execution

function myFunction($name, $color) {
  echo "Hi, " . $name . ", You like " . $color; //this uses the passed in values which are assigned to the variable names in the definition
  return 'Yay'; //we are returning a value of 'Yay' to the calling code
}

?>

基本上,函数定义与脚本的主执行是分开的,只有在使用myFunction()函数调用函数时才会运行函数内部的代码。这可以在执行期间多次调用。您还可以通过定义将参数发送到函数(例如我在示例中传递的名称和颜色),函数可以将信息返回给调用它的代码,如我所示。注意,通常的做法是在文件的结尾或开头定义所有函数,但是你不必这样做。