if-then-else

March 11th, 2005. Tagged: PHP certification

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.

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov