2011-12-22 6 views
0

実際に私の独自の関数を作成していないので、私が試しているのはfunction NewsUpdateという行の下のスクリプトを開始することです。 if文では、スクリプトが「電子メールメッセージ」の文字列「News Update」を見つけた場合、それらのスクリプトを開始します。そうでない場合は(else文が一番下にあります)失敗し、「News Update not found電子メールメッセージ」エラーPHPを使用したif文の後に関数を開始する

にしかし、私はエラーを取得:

Parse error: syntax error, unexpected 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!"; 
} 

答えて

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

} 
+0

ありがとう、これは完了しました。 –

0

if文のあとに{を追加してみてください。関数定義の最後に;の後に1つの}があります。

ここにelseステートメントを追加するには、有効な命令またはブロックが1つ必要です。

2

本当の答え:そのような条件付きで関数を定義しないでください。それは、厄介な維持不能なコードになります。

悪い答え:

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

同様に、あなたにあなたのstrpos CANブローアップ。 strposは、検索する文字列が文字列の先頭にある場合は整数0を返します。この文字列はブール値falseと解釈されます。あなたがそうであるように決してテストstrposは、常に適切に「針」が「干し草の山」ではない場合をキャッチ、それでも0-配置針を許可します

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

を持っています。

+0

ブラケットがありません。 '/ * blablabla * /}' – ariefbayu

+0

もしそれが悪い習慣だと思っても、これを達成するより良い方法があるとしても、関数について学び、動的に定義されたものを必要とする場合、これは最初の実装であり、悪い習慣を除いて、彼が間違っていたことに関する質問がありました。 – khael

+0

@silent:それを試してください: 'if(true)function x(){return 'x'; } '。関数定義自体の周りに{}がないかぎり、構文エラーで終了します。 –

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を()を使用して呼び出された場合関数の内部コードのみが実行されます。これは、実行中に好きなだけ何度でも呼び出すことができます。また、パラメータを関数に定義で渡すこともできます(この例では、名前と色を渡しています)。関数は、私が示したように、呼び出し元のコードに情報を返すことができます。ファイルの最後か最初にすべての関数を定義するのが一般的ですが、そうする必要はありません。

+1

関数は以下の場合に内部で定義できます: 'if(true) { 関数call_it($ c){ echo $ c; } } ' – ariefbayu

+0

ifステートメント内で関数を定義することは、後方互換性を保証するための一般的な習慣です:' if(!function_exists( 'json_encode')){function json_encode(){...}} ' – deceze

+0

機能に新しい誰か私はこの道を下ることが簡単ではないと感じました。私は注意を払って答えを更新しました。 – Ravenex

関連する問題