Page 1 of 1

Learning PHP

Posted: Thu Jul 22, 2010 10:11 am
by emilcarlo
Good evening,

I am still learning PHP, and while reading understanding one of the tutorials that was linked in this forum, I was caught with this code:

Code: Select all

		<?php
			if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) 
			{
		?>
			<h3>strpos() must have returned non-false</h3>
			<p>You are using Internet Explorer</p>
		<?php
			} 
			else 
			{
		?>
		<h3>strpos() must have returned false</h3>
		<p>You are not using Internet Explorer</p>
		<?php
			}
		?>
I do understand that the code's purpose is to check if the user's browser is IE or not. What I am very curious about is the way the code is written.

Code: Select all

		<?php
			if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) 
			{ -[b][i] why is this in between <?php ?> tags[/i][/b]
		?>
			<h3>strpos() must have returned non-false</h3>
			<p>You are using Internet Explorer</p>
		<?php
			} 
			else 
			{
		?>
		<h3>strpos() must have returned false</h3>
		<p>You are not using Internet Explorer</p>
		<?php
			} - [b][i]why is this in between <?php ?> tags[/i][/b]
		?>
I am trying to learn how to write a good php code, and I have just stumbled on a tutorial the seems the code is not well-written (As for me since I am a newbie). Can anyone enlighten me on this?

Re: Learning PHP

Posted: Thu Jul 22, 2010 12:18 pm
by JakeJ
You cannot write a conditional statement in HTML. You can't tell html to display content based on certain conditions (if this is true then write something). That's what you need php for.

On a php page, you can do things 3 ways.

1. Write everything in php and put all of your html code in an echo statement. Notice how i the example below, I put html tags inside of an echo statement.

Code: Select all

<?php
If ($x==1) {
   echo "<h1>TIENE EXITO!</h1>";
}
Else {
    echo "<h1>NO EXITO</h1>";
}
2. You can break in and out of php at will and write regular html.

Code: Select all

<? If ($x==1) { ?>
<h1>TIENE EXITO!</h1>
<?php }
Else { ?>
<h1>NO EXITO</h1>
<?php }
3. Hybrid. You can use long stretches of php code and occasionally break out of it. Or you can use long stretches of html and occasionally break in to html. It's flexible.

The point is; if you want to test conditions, use variables, display data from a database and many other things, you will need php rather than straight html.

Re: Learning PHP

Posted: Thu Jul 22, 2010 1:03 pm
by emilcarlo
I appreciate your response, thanks for that ^^ I was caught unaware with how the php tag was written, I thought it is similar to html hihi :)

Re: Learning PHP

Posted: Thu Jul 22, 2010 7:50 pm
by JakeJ
I did make a mistake in my second example though...

I wrote the php opening tag as "<?" which does work but you should ALWAYS use "<?php"

In fact, in one of my apps, I find that if I break out of php and then reference a variable inside of another tag that only has "<?" that I can't reference it. Of course I only ever do that by accident.