Hi, it's Xav. Now here's something pointless, but interesting nevertheless. It is actually possible to use another syntax when writing control structures in PHP! This could potentially be useful for VB programmers who want an easy way into PHP, before they learn the {} syntax.
Example 1
So, here's a normal If statement:
However, you can also do it like this:Code:<?php
if ($forum == "CodeCall")
{
echo "This is the best forum!";
}
?>
The text inbetween the two PHP tags will only be printed if the variable $forum contains the value "CodeCall". This makes the two above statements identical.Code:<?php if ($forum == "CodeCall"): ?>
This is the best forum!
<?php endif; ?>
Example 2
Here's another normal If statement, then:
But we can rewrite it using a VB-like syntax, like this:Code:if ($forum == "CodeCall")
{
echo "This is the best forum!";
}
else
{
echo "This is not the best forum!";
}
So, what's happened here:Code:<?php
if ($forum == "CodeCall"):
echo "This is the best forum!";
elseif ($forum == "DiC"):
echo "This is a load of rubbish!";
else:
echo "I don't know this forum!";
endif;
?>
- The curly braces { } have disappeared from the code.
- A colon : has been added after every condition statement.
- We now have an "endif" statement to mark the end of the decision. This is followed by a semi-colon ;.
Conclusion
It may not be as nice as the { } syntax, but, like all other things in programming, has a use. Have fun!
Test yourself: Write a condition that rewrites the example above using a switch statement, but using the alternative syntax. Refer to PHP: Alternative syntax for control structures - Manual if you are stuck.
~Xav
+rep if useful. Leave any comments below!
The only thing with not using the { } is that the code is a lot harder to read. If VB programmers are gonna learn PHP, they might as well learn it with the correct PHP syntax.![]()
Nice tutorial. If you want points you must PM me. I also wont be giving out +rep during the contest for tutorials.
C'mon it's not that big of a deal.![]()
I reserve +reputation for posts I find useful. That said, if you ever post something I find useful, you will receive reputation from me (as probably Jordan too). However, since tutorials are awarded 25 points toward winning the competition, Jordan sees no reason to award you with another 20 points for the same thing.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks