if-then-else

Overall, the cert guide is pretty comprehensive on the subject of the conditional statements.

One tiny insignificant detail. There is an example that goes like
< ?php
if ($a < 100) {
echo 'Less than 100';
} else {
echo 'More than 100';
}
?>
Well, it's evident that the second message should have been better off like: "More than or equal to 100".

In the next example we see something like:
< ?php
if ($a < 100) {
echo 'Less than 100';
echo "\nNow I can output more than one line";
}
?>
Here we see how for the second echo, we use double quotes. The reasons were explained earlier in the certification guide. In brief, PHP interpreter evaluates double quotes, looking for variables or escape characters like \n. So far, so good. In the following examples though, the \n is omitted, leaving the reader with:
echo 'Less than 100';
echo "Now I can output more than one line";

So the reader might wonder why the first line uses single quotes and the second uses double quotes. I think just being consistent and keeping the \n won't cause such confusion.

As a side note:
Using double quotes only when necessary, sticking to single quotes on all other occasions, is good practice and will result in somewhat faster execution of your PHP applications. It's a good habit to use single quotes. But ... don't lose your sleep over it, it's not the silver bullet for PHP apps optimization, there are plenty of other areas for improvement.

This entry was posted on Friday, March 11th, 2005 and is filed under PHP certification. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

One Response to “if-then-else”

  1. apple ipad 2 review Says:

    Thank you a lot for sharing this with all people you actually recognize what you are talking about! Bookmarked. Please additionally visit my website =). We may have a hyperlink exchange arrangement between us!

Leave a Reply