Example 1
So, here's a normal If statement:
<?php
if ($forum == "CodeCall")
{
echo "This is the best forum!";
}
?>
However, you can also do it like this:
<?php if ($forum == "CodeCall"): ?> This is the best forum! <?php endif; ?>
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.
Example 2
Here's another normal If statement, then:
if ($forum == "CodeCall")
{
echo "This is the best forum!";
}
else
{
echo "This is not the best forum!";
}
But we can rewrite it using a VB-like syntax, like this:
<?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;
?>
So, what's happened here:
- 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!


Sign In
Create Account


Back to top









