2016-12-19 6 views
-1

私はたくさんのものを使用したいが、それらを整理したかったので、外部PHPファイルからifステートメントの一部を含めることを試みています。PHP - ステートメントエラーが含まれている場合

私はエラーを取得する -

Parse error: syntax error, unexpected 'else' (T_ELSE)

私の主なコードは次のとおりです。

<?php 

require 'Lib/Functions.php'; 

$message = "hello"; 

if($message == '') { 
    $message_to_reply = Pick('|Please ask me a question|Got any questions?|Want to talk?'); 
} 

include 'Response/Data.php'; 
include 'Response/General.php'; 

else { 
    $message_to_reply = 'Sorry, im not sure what you mean?'; 
} 

echo $message_to_reply; 

?> 

そして含まGeneral.phpなどはこのようなものです:

<?php 

elseif(preg_match('[hello|hi|Greetings]', strtolower($message))) { 
    $message_to_reply = Pick('Hi there!|Hello there!|Hi, it\'s nice to meet you|Greetings'); 
} 

?> 
+0

if文を作成しています。次に、2つの項目を含めると(ifまたはelseにはない)、elseステートメントが来ます。問題はif文だけを使用していることです。そして、突然、else文があります(この場合、ifも同様です) – Mitch

答えて

1

あなたの主なコードが好きなはずです

<?php 

require 'Lib/Functions.php'; 

$message = "hello"; 

if($message == '') { 
    $message_to_reply = Pick('|Please ask me a question|Got any questions?|Want to talk?'); 
    include 'Response/Data.php'; // move inside if statement 
    include 'Response/General.php'; // move inside if statement 
} 
else { 
    $message_to_reply = 'Sorry, im not sure what you mean?'; 
} 

echo $message_to_reply; 

?> 

あなたgeneral.phpを開始します。

<?php 

// remove elseif // just if 
if(preg_match('[hello|hi|Greetings]', strtolower($message))) { 
    $message_to_reply = Pick('Hi there!|Hello there!|Hi, it\'s nice to meet you|Greetings'); 
} 

?> 

あなたはifなしelseまたはelseif声明独立させることはできません。これは、私たちが従わなければならないコードブロック構造です。

このヘルプをご覧ください。

1
if($message == '') { 
$message_to_reply = Pick('|Please ask me a question|Got any questions?|Want to talk?'); 

include 'Response/Data.php'; 
include 'Response/General.php'; 
} 

てみてくださいincludeとthの後ろにカッコを入れるエン他の部分

1

ifとelse if/elseif以外の条件の間に他のコードを入れてはいけません。

elseifだけを含んでいるファイルをincludeしています。これは、if文の内側またはelse部分の末尾に含めることができます。

関連する問題