Jump to content

'INCLUDE' error


LeeS

Recommended Posts

I have a page which uses an IF ELSE to choose which php file to INCLUDE.

<?php if($subs_today < 2)
echo include ('SubForm_Yes.php');
else
echo include ('SubForm_No.php');
?>

The resulting page displays a '1' below where INCLUDE code is inserted.

This number '1' appears after either choice (SubForm_Yes or SubForm_No)

 

Any Ideas?

Link to comment
Share on other sites

Curly braces are only required if there is more than one statement to be executed:

 

if ($a > $b)
   echo "a is greater than b";
else
   echo "a is NOT greater than b";

is perfectly valid syntax. However, it is too easy, when modifying the code later, to add another line of code in there without realizing that the braces are not present, and you end up creating a bug:

 

if ($a > $b)
   echo "a is greater than b";
else
   echo "a is NOT greater than b";
   echo "also, means b is less than a";

that extra echo I added will ALWAYS be executed because there are no curly braces.

 

I have adopted the habit of keeping the statements on the same line as the IF and ELSE if I don't use braces:

 

if ($a > $b)   echo "a is greater than b";
else           echo "a is NOT greater than b";

 

Then, if I later want to add a statement, it is obvious that the braces need to be added:

 

if ($a > $b)   echo "a is greater than b";
else {
   echo "a is NOT greater than b";
   echo "also, means b is less than a";
}

 

 

Link to comment
Share on other sites

Another few things:

 

1. include isn't a function and shouldn't have parenthesis they are allowed but it isn't good practice.

2. if the file has functions/classes defined within it, you should use "require_once" this way you won't get an error for having something more than on time in your code such as a "SomeFunction has already been defined" error.

3. includes only show a warning if the file is not found, whereas require will show a fatal error. I would recommend using require and require_once over include and include_once.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.