6

I'm fairly new to PHP. I started learning it like 3 weeks ago. I cannot find the answer to this question on StackOverflow, Google or Youtube. The PHP documentation to this just confuses me. To get on with the question, how does PHP code mixed in with HTML work?

<?php if (something) { ?>
    <p>Hello</p>
<?php } ?>

The p element will only display if something has a truthy value, how is this?... I thought for sure that the PHP engine ignored what was going on around the outside of the codeblocks (e.g. <?php ?>) and only parsed what happens on the inside.

The code below gets parsed by the PHP engine normally and sent to the browser without affecting any HTML elements (even though its clearly between 2 code blocks).

<?php echo $something; ?>
<p>Hello</p>
<?php echo $something; ?>

I hope I'm not going to get flamed for asking this question since a lot of people seem to understand how it works in like a tenth of second.

P.S. I asked this question in chat early and thought I understood it correctly but when I went to implement it my mind was still like, how does this work exactly? It just seems like some kind of hack to me.

j0k
  • 22,600
  • 28
  • 79
  • 90
Codist
  • 1,198
  • 2
  • 11
  • 28

5 Answers5

6

Easy now. Definitely need a php tutorial for you to start on http://www.tizag.com/phpT/

Here is what your doing:

<?php 
//Anything inside me php processes
if($something)
{
    echo "<p>something</p>";
}
//About to stop processing in php
?>
<p>Anything outside of the php statement above will just be printed to the dom</p>

Quick Note: It is good practice to separate your PHP from your HTML

<?php if ($something) { ?>  <-- where is the other {
<p>Hello</p>
<?php } ?> <-- oh I see it.
Jared Drake
  • 982
  • 4
  • 12
  • I know of this. I understood this in a tenth of a second. I was just asking how it works by code blocks? I know that it good practice to separate HTML and PHP as well. =) But I can't help that my mind is still in the ruts about my question. – Codist Aug 20 '12 at 04:31
  • Yes it is, So like PHP goes to standby mode until it reaches the other brace and then the conditional will be evaluated if $something is truthy and the p elements will be outputted. What is with this,

    Hello

    Does endif; act like the closing brace and break the execution?
    – Codist Aug 20 '12 at 04:46
  • @W3Geek In that syntax, yes it would act as a closing brace. – Jared Drake Aug 20 '12 at 05:09
3

In your first example it is indeed true that <p>Hello</p> will be rendered if and only if 'something' returns true.

If you close a php tag with ?> but have an 'unclosed' execution, like if (blah) { ..., the PHP engine understands your desires and does accordingly.

Why?

The PHP engine is kept 'waiting' until the execution is closed with } and then the final result is evaluated and the browser continues on with the lines below.

Obviously if you leave out the final } you will see some errors, which tells you that PHP was expecting you to finish what you started and you did not

khaverim
  • 3,386
  • 5
  • 36
  • 46
  • +1 Great Answer indeed. I was thinking if you left out the ending bracket you would see some error because PHP is like WTF mate?... you have a unclosed conditional. So with this,

    Hello

    endif closes the execution?
    – Codist Aug 20 '12 at 04:40
  • executions are started at { and ended at } in PHP. The whole if statement in PHP is: if ($something) { //do stuff }. But you CAN use endif; if you want; see http://stackoverflow.com/questions/564130/difference-between-if-and-if-endif – khaverim Aug 20 '12 at 04:43
  • One last thing, I came up with a rule of thumb about this. If something evaluates as false in the condition then its like the HTML code is hidden away (not outputted) after the PHP engine parses through it? – Codist Aug 20 '12 at 20:05
  • yes, because, again, the PHP engine is 'waiting' until the statement gets closed and thus nothing is rendered until PHP knows true or false. Just like it is in PHP-only code – khaverim Aug 21 '12 at 02:56
1

Both the php and html are parsed in-line. So, as it moves down your script it will run php scripts within tags, and display html in the order which they are placed. For example:

<? $someVar = "someVar string value"; ?>
<h1>This is a title</h1>
<? if(1 == 1){?>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<? } ?>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: <?=$someVar;?></p> // <?= is a short hand for echo

This will show as:

<h1>This is a title</h1>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: someVar string value</p>

Basically just think of it as the server reading down your script and parsing whatever it sees as it goes. If there is html, it will display it and if there is php which does some sort of calculation and then spits out html, it will show the spat out html.

VictorKilo
  • 1,839
  • 3
  • 24
  • 39
0

You can write php code anywhere in HTML using php code block

<?php echo "whatever " ?> 

or

<?php echo "<h1>Here everything will displayed in h1 </h1> "; ?>

and if you use control structure ( if, switch etc.. ) then it will behave like all other languages, means if something is true then it will execute the part written between { }.

so if you write an undefined variable in if condition then it will not execute code block of because undefined variable is treated as false condition .

additionaly you can check any variable value by var_dump($variable)

xkeshav
  • 53,360
  • 44
  • 177
  • 245
0

PHP's Alternative syntax for control structures

<!DOCTYPE html>
...
<div>
<?php if ( the_thing === true ) : ?>
    <p>The thing is true! \o/</p>
<?php else if ( the_other_thing === true ) : ?>
    <p>The other thing is true! meh</p>
<?php else : ?>
    <p>Nothing is true :-(</p>
<?php endif; ?>
</div>
...
Fred Gandt
  • 4,217
  • 2
  • 33
  • 41