0

I have two php files first is sample.php and the second is if.php. I have some code written in sample.php basically declaration of two variables and an else statement. I have the if statement in if.php file which I'm including before else statement. But on running my code its shows an error:

Unexpected 'else'...etc.

Can you please help me figuring out what is wrong with the code?

Sample.php

<?php
$a=10;
$b=20;

include 'if.php';
else
{
echo $b.' is greater';
}
?>

if.php

<?php
if($a>$b)
{
echo $a.' is greater';
}
?>
7
  • I'm fairly certain that you cannot execute a if / else logic like this. Also, why? Why not just write the if sentence with the else? Commented Oct 6, 2015 at 6:31
  • @Epodax he has shown his code for if.php in the second half. Commented Oct 6, 2015 at 6:32
  • if.php is right on the bottom of my question.. and i don't know if i can do this or can't.. but what if i have to??? Commented Oct 6, 2015 at 6:33
  • So he has, my bad - It's still early and I haven't had my coffee :) Commented Oct 6, 2015 at 6:33
  • How do you even come to such ideas? Commented Oct 6, 2015 at 6:35

2 Answers 2

2
include 'if.php';
else

Nice, there is no such thing as include else. Even if you have an if clause at the end of your included file, the include line breaks its sequence.

else can only come after either an if or an else if block, no third option. Period.

Here is how PHP parser will see your 2 files

if($a>$b){
  echo $a.' is greater';
}
include "if.php";
else {
  echo 'na';
}

Which is clearly invalid.

If you still want to keep things as they are, like you mentioned in the comments. You can do 1 small trick (amongst many other methods). Make your else another if which is contrary to the first if. Like

if.php

if($a>$b){
  echo $a.' is greater';
}

main file

include "if.php";
if($a<=$b) {
  echo 'na';
}
Sign up to request clarification or add additional context in comments.

3 Comments

actually i have two php files first is establishing connection to a sql database and and checking if the connection is successfully established so my if statement is in another file and in main file i have an else statment
like you said i can't do this so its a better idea for me to keep both my if and else statements in a single php file
@sharan what you are trying to achieve can be done using functions. You can return data from functions and use this response in your if else condition.
1

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue.

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

footer.php

<?php
echo "<p>Copyright &copy; 1999-" . date("Y") . " W3Schools.com</p>";
?>

index.php

<!DOCTYPE html>
<html>
<body>

<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>

</body>
</html>

Live Preview

Source from PHP 5 Include Files

So your code looks like this

if($a>$b)
{
  echo $a.' is greater';
}
include "if.php";
else 
{
  echo 'na';
}

Simply Its totally wrong.

2 Comments

hahah i read it on w3school but unfortunately not solving my porblem
use if after include file. So no harm at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.