On Sat, Jan 15, 2011 at 10:20 AM, David McGlone <da...@dmcentral.net> wrote:
> On Thursday, January 13, 2011 12:45:30 pm Nathan Rixham wrote:
>> ...
>> $categorys = array('home', 'services', 'gallery', 'about_us',
>> 'contact_us', 'testimonials');
>> foreach($categorys as $category) {
>>    $temp = str_replace("_", " ", $category);
>>    $_GET['page'] != $category && $temp = '<a href="index.php?page='.
>> $category .'">'.$replace.'</a>';
>>    echo "<li>{$temp}</li>" . PHP_EOL;
>> }
>
> Nathan, thanks for showing me this. I understand the code, except I don't
> understand how you got by without using a conditional (if/else). If it were
> me, I would have written it like:
> ...

The logical operators are lazy i.e. for "&&", if the expression on the
left is true, then and only then is the right part evaluated. So $temp
is only modified if
   $_GET['page'] != $category is true.

Either way, you get the same answer, vis a vis if the left is false,
the "&&" must be false, so no need to evaluate the right side.

This idiom is common in shell scripts and is called among other
things: "short cut or short circuit evaluation"

> My questions are, is this wrong? is it amaturish?

Not in other languages, I doubt in PHP.

-- 
Regards
Evil Son

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to