2016-12-19 140 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语句。然后突然有一个其他的声明(在这种情况下,应该是一个如果也是) – 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做出独立elseelseif声明。这是我们需要遵循的代码块结构。

希望得到这个帮助。

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'; 
} 

尝试在包含和th之后附上括号恩开始else部分

1

if和else if/elseif条件之间不应该有其他代码。

您正在包含一个只包含elseif的文件,它可以包含在if语句中,或者包含在else部分的结尾以及包含文件中,如果没有上面的语句,也不能提及elseif。