-1

I am trying to include parts of an if statement from external php files because i wanted to use lots of them but have them organized.

I get the error -

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

My main code is:

<?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;

?>

And the General.php etc included are like this:

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

?>
Dave
  • 3,073
  • 7
  • 20
  • 33
zeddex
  • 1,260
  • 5
  • 21
  • 38
  • You are making a if statement. Then include 2 items (not in an if or else) and then an else statement comes. So the problem is, you are only using an if statement. And then there suddenly is an else statement (Wich in this case should be an if aswell) – Mitch Dec 19 '16 at 09:23

3 Answers3

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

Try enclosing your brackets after include and then start the else part

fizzi
  • 116
  • 4
1

Your main code

<?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;

?>

your general.php should like;

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

?>

You cannot make else or elseif statement independent without if. This is code block structure we need to follow.

hope this help.

weirdo
  • 334
  • 1
  • 10
1

There should not be any other code between if and else other than another if / elseif conditions.

you are including a file which has only elseif in it, this can be included inside if statement or at the end of else part and in that included file also you cannot mention elseif without if statement above it.