Syntax question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marasys
Forum Newbie
Posts: 2
Joined: Fri Jan 29, 2016 9:58 am

Syntax question

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Syntax question

Post 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.
marasys
Forum Newbie
Posts: 2
Joined: Fri Jan 29, 2016 9:58 am

Re: Syntax question

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Syntax question

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Syntax question

Post 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.
(#10850)
Post Reply