PHP: if
A typical web developer knows exactly how a PHP if statement looks like:
if ( expression ) { // if the expression was true proceed here } else { // there was a false expression } |
HTML mess with PHP
When it comes to merge PHP and HTML the things are becoming ugly. Indeed most of the template systems as Smarty are improved and developed to overcome this issue, however when working with a native PHP code with no template system or with template system where the PHP code is allowed the things are really bad.
Let me show this in a breve example. Image you have to show different formatted HTML depending on a PHP expression. Something like that
<?php if (expression) { ?> <div class="message">OK. Your registration is successful</div> <?php } else { ?> <div class="error">Something went wrong! Please try again later! </div> <?php } ?> |
Now you can see how difficult to maintain this code is when it doesn’t make use of only one code of HTML markup. Imaging you’ve to print differently formatted tables! Indeed the PHP curly brackets are different to follow.
PHP: the different IF syntax
So there is a PHP syntax that tries to help you manage this. You can write more human readable code like this:
<?php if ( expression ) : ?> <div>message goes here</div> <?php endif ?> |
Thus you get the ENDIF instead of only one curly bracket. That’s indeed readable enough. In fact you can use this syntax with any conditional or loop statement in PHP:
<?php foreach($array as $key => $val) : ?> <div class="message"><?php echo $val ?></div><br /> <?php endforeach ?> |
To return in the previous example the code above should be transformed in that:
<?php if ( expression ) : ?> <div class="message">some message here!</div> <?php else : ?> <div class="error">some error here!</div> <?php endif ?> |
Thanks for the article! I was always wondering about that syntax, but now I found it very very useful!
adrian
Nice, well known though.
However this remains really misunderstood!
I don’t think this syntax is readable. However when mixing HTML with PHP everything’s becoming messy always!
that’s a good suggestion. Will be doing like that from now on.
I think that’s just a newbie fix and it doesn’t do anything but confuse the community.. keep the { } syntax.
Thanx for the data.
Thank you very much for this post. It really helped understanding how to implement HTML-Code in an “If – Else” Statement.
It just saved my code.
Nice,well known though this post
you save me!
likeeeeeeeeeeeeeeeeeeeeeeeeeee
very very very thanks…
Thanks for your explanation on , I know understand that it allows for you not to write at the end of the PHP statement
Thanks for explanation with proper example. Finallay i can understand how to use
if-else-endif Statements in PHP.
You cheat a little. In example HTML mess with PHP you have 5 lines, but in PHP: the different IF syntax you intentionally put only 3 lines, just to look simple. Even more, in example HTML mess with PHP you write stylesheet, just to look more complicated.
But basically you’re right.
Nice, well done