Page 1 of 1

Syntax question

Posted: Wed Feb 03, 2016 4:04 pm
by marasys
I'm new to PHP and having a problem with a section of code as shown below

Code: Select all

	 <?php
if ( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count( $_SESSION['ERRMSG_ARR'] ) > 0 ) { 

			echo '<ul>';
			foreach($_SESSION['ERRMSG_ARR'] as $msg) {
				echo ' <li> ',$msg,'</li>'; 
				}
			echo '</ul>';
			unset($_SESSION['ERRMSG_ARR']);
			}
		?>
produces code on the screen when it executes as follows:
[text]
0 ) { echo '
'; foreach($_SESSION['ERRMSG_ARR'] as $msg) { echo '
',$msg,'
'; } echo '
'; unset($_SESSION['ERRMSG_ARR']); } ?>
[/text]

But when I change the IF statement from >0 to != the code disappears

Code: Select all

if ( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count( $_SESSION['ERRMSG_ARR'] ) != 0 ) { 
So my question is why?

Re: Syntax question

Posted: Wed Feb 03, 2016 4:23 pm
by requinix
It's not disappearing. Do a View Source of the page and you'll see all your PHP code right there.

It means your code is not executing as PHP. Either you aren't using the .php extension on your files or you don't have PHP installed properly.

Re: Syntax question

Posted: Wed Feb 03, 2016 4:55 pm
by marasys
It's an .html file with the php embedded in the html code.

I've seen examples showing php at the beginning of an html file prior to the DOCTYPE statement, php in the head segment and in the html segment of html files. Are they all correct?

Re: Syntax question

Posted: Wed Feb 03, 2016 5:24 pm
by requinix
That's fine. But the file itself needs (typically) to be named *.php in order for the server to know it contains PHP code to execute and not just plain HTML.

Re: Syntax question

Posted: Wed Feb 03, 2016 5:51 pm
by Christopher
Yes, my guess is that the > is closing some HTML tag which is why what is displayed changes. A syntax checking editor would be helpful.