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

使用PHP在if语句后启动函数时出错,php,mysql,Php,Mysql,我以前没有真正创建过自己的函数,所以我尝试的是在函数NewsUpdate行下面启动脚本,但我希望它们是函数的一部分。在if语句中,当脚本在“email message”中找到字符串“News Update”时,它将启动这些脚本,如果没有(底部的else语句),它将失败并显示“News Update not found In e-mail message” 但我得到了一个错误: 分析错误:语法错误,意外的T_函数 我做错了什么 // Test code for email message. $op

我以前没有真正创建过自己的函数,所以我尝试的是在
函数NewsUpdate
行下面启动脚本,但我希望它们是函数的一部分。在if语句中,当脚本在“email message”中找到字符串“News Update”时,它将启动这些脚本,如果没有(底部的else语句),它将失败并显示“News Update not found In e-mail message”

但我得到了一个错误:

分析错误:语法错误,意外的T_函数

我做错了什么

// 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!";
}
//测试电子邮件的代码。
$open_email_msg=file_get_contents('emailmessage.html');
//在电子邮件中搜索字符串新闻更新的脚本,
//如果找到它,它将启动函数NewsUpdate
if(strpos($open\u email\u msg,“新闻更新”))
功能新闻更新($open\u email\u msg){
//登录MySQL数据库
$hostname=“localhost”;
$db_user=“user”;
$db_password=“pass”;
$database=“tablename”;
$db_table=“bx_新闻条目”;
$db=mysql\u connect($hostname、$db\u user、$db\u password);
mysql\u select\u db($database,$db);
$subject='测试新闻文章';
$tags=str_replace(“”,“,$subject);//DDIE
$uri=str_replace(“”,“-”,$subject);//DDIE
$when=strotime(“now”);//文章发布日期
$categories='Events';
$content=“知识产权是一种权利,是一种精英,是劳动和财富的暂时性权利,是一种最低限度的权利,是一种劳动和财富的权利,是一种权利,是一种权利,是一种权利,是一种权利,是一种权利,是一种权利;
$snippet='Lorem ipsum dolor sit amet';
//$snippet=explode(“.”,$content,-1);
#这段代码将告诉MYSQL将电子邮件中的数据插入MYSQL表
$sql=“插入到$db_表(`caption`、`snippet`、`content`、`when`、`uri`、`tags`、`categories`、`DATE`)中的值(`$subject`、`snippet`、`content`、`when`、`uri`、`$tags`、`categories`、`when`);
if($result=mysql\u查询($sql,$db)){
}否则{
echo“ERROR:.mysql_ERROR();
}
echo“添加新闻文章!”;
}
否则{
echo“'News updateno found in e-mail message!”;
}

好吧,试着在if语句之后添加一些
{
,在a
之后添加一个
}
位于函数定义的末尾


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

好吧,尝试在if语句之后添加一些
{
。在
之后添加一条
}
位于函数定义的末尾


在添加
else
语句之前,至少需要一条有效指令或一个块。

真正的答案:不要像这样有条件地定义函数。它会产生令人讨厌的无法维护的代码

回答不好:

        if(strpos($open_email_msg,"News Update")) {
                                                  ^---- missing bracket
            function NewsUpdate ($open_email_msg) {
同样,你的STRPO也会在你身上爆炸。如果要搜索的字符串位于字符串的开头,则strpos可以返回整数
0
,然后将其解释为布尔值false。永远不要像现在这样测试strpos,要一直测试

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

这将正确地捕捉到“针”不在“干草堆”中的情况,但仍然允许0位针。

真正的答案:不要像这样有条件地定义函数。它会产生令人讨厌的无法维护的代码

回答不好:

        if(strpos($open_email_msg,"News Update")) {
                                                  ^---- missing bracket
            function NewsUpdate ($open_email_msg) {
同样,你的STRPO也会在你身上爆炸。如果要搜索的字符串位于字符串的开头,则strpos可以返回整数
0
,然后将其解释为布尔值false。永远不要像现在这样测试strpos,要一直测试

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

这将正确地捕捉“针”不在“草堆”中的情况,但仍然允许0位针。

将函数移到上面并调用它:

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!";

}
功能新闻更新($open\u email\u msg){
//函数声明
}
//电子邮件消息的测试代码。
$open_email_msg=file_get_contents('emailmessage.html');
//在电子邮件中搜索字符串News Update的脚本,如果找到它,它将启动函数NewsUpdate
if(strpos($open\u email\u msg,“新闻更新”))
新闻更新($open\u email\u msg);
否则{
echo“'News updateno found in e-mail message!”;
}

将函数移到上面并调用它:

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!";

}
功能新闻更新($open\u email\u msg){
//函数声明
}
//电子邮件消息的测试代码。
$open_email_msg=file_get_contents('emailmessage.html');
//在电子邮件中搜索字符串News Update的脚本,如果找到它,它将启动函数NewsUpdate
if(strpos($open\u email\u msg,“新闻更新”))
新闻更新($open\u email\u msg);
否则{
echo“'News updateno found in e-mail message!”;
}

您的语法完全不正确。函数是一段代码,您可以在脚本中的不同位置通过over执行。函数在使用时未定义,不能在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()调用函数时,函数内部的代码才会运行;在执行过程中,您可以任意多次调用它。您还可以根据定义(如我在示例中传递的名称和颜色)向函数发送参数,并且函数可以将信息返回给代码calli