LeeS Posted August 10, 2011 Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244440-include-error/ Share on other sites More sharing options...
LeeS Posted August 10, 2011 Author Share Posted August 10, 2011 ignore this. Fixed it. Did not see I had included ECHO!! Link to comment https://forums.phpfreaks.com/topic/244440-include-error/#findComment-1255504 Share on other sites More sharing options...
tqla Posted August 10, 2011 Share Posted August 10, 2011 Here is the proper structure of an if else statement. I don't see "{ }" in your syntax. <?php if ($a > $b) { echo "a is greater than b"; } else { echo "a is NOT greater than b"; } ?> Link to comment https://forums.phpfreaks.com/topic/244440-include-error/#findComment-1255505 Share on other sites More sharing options...
LeeS Posted August 10, 2011 Author Share Posted August 10, 2011 Thanks for that. I have amended. Link to comment https://forums.phpfreaks.com/topic/244440-include-error/#findComment-1255524 Share on other sites More sharing options...
DavidAM Posted August 10, 2011 Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244440-include-error/#findComment-1255598 Share on other sites More sharing options...
The Little Guy Posted August 10, 2011 Share Posted August 10, 2011 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 https://forums.phpfreaks.com/topic/244440-include-error/#findComment-1255603 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.