Re: Re: Rewriting string

From: Date: Sat, 15 Jan 2011 12:36:33 +0000
Subject: Re: Re: Rewriting string
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-310778@lists.php.net to get a copy of this message
On Sat, Jan 15, 2011 at 10:20 AM, David McGlone <david@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

« previous php.general (#310778) next »