Little known PHP Idioms etc.

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

What? I love me a do while loop. do this stuff while this stuff is true. if not, stop it! :-D It's the way to loop ;)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Do...while loops help out tremendously when handling database returns with multiple result sets...

Code: Select all

<?php
if ($qry = sybase_query($sql, $con))
{
    $rs = array();
    	
    do {
        while ($row = sybase_fetch_array($qry)) {
            $rs[] = $row;
        }
    } while (sybase_next_result($qry));
    
    sybase_free_result($qry);
}
else
{
    echo '<p>There were problems with your query: <strong>' . sybase_get_last_message() . '</strong>';
}
?>
nitrino
Forum Newbie
Posts: 10
Joined: Tue Jan 02, 2007 10:22 am
Location: Riga, Latvia

Post by nitrino »

Does anybody use a short condition check?

Code: Select all

$var = $x?true:false;
not a tricky, but found very usefull for myself some time ago.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Ooh that reminds me..one of the things I really want to see in PHP 6 is better else support:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
    // stuff
} else {
    // executes only if while is never true
}

foreach ($array as $value) {

} else {
    // executes only if $array is empty
}

for ($i=1; $i<0; ++$i) {

} else {
    // executes if increment second part is never true
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

nitrino wrote:Does anybody use a short condition check?
Its called the ternary operator and yes a lot of people use that. You should never nest them though.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

nitrino wrote:Does anybody use a short condition check?

Code: Select all

$var = $x?true:false;
not a tricky, but found very usefull for myself some time ago.
The ternary operation is useful, but it does sometimes make for code that is not as readable as one would like. I tend to stay away from it unless I am testing or unless it is absolutely necessary. In fact, I put into my company's coding standard to not use it unless it is absolutely necessary (which it seldom is).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

why do people do this:

Code: Select all

$var = $x?true:false;
as opposed to this?

Code: Select all

$var = (bool) $x;
Never understood that.
nitrino
Forum Newbie
Posts: 10
Joined: Tue Jan 02, 2007 10:22 am
Location: Riga, Latvia

Post by nitrino »

The Ninja Space Goat wrote:why do people do this:

Code: Select all

$var = $x?true:false;
as opposed to this?

Code: Select all

$var = (bool) $x;
Never understood that.
It was just an example, it could be

Code: Select all

$var=$x>3?'apple':'banana';
:)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

ole wrote:Ooh that reminds me..one of the things I really want to see in PHP 6 is better else support:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
    // stuff
} else {
    // executes only if while is never true
}

foreach ($array as $value) {

} else {
    // executes only if $array is empty
}

for ($i=1; $i<0; ++$i) {

} else {
    // executes if increment second part is never true
}

Very weird IMHO. I personally would not like any of those. They only add harder code to read (not standard control structures as most languages have) and I really don't see the benefit.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I use ternary operators all day long...and I love 'em.

curse those of you who talk smack on my friend the ternary, curse you all!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

not standard control structures as most languages have
You and I are both non-standard, does that make us inferior :P anyway Python has them.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Burrito wrote:I use ternary operators all day long
Don't get me wrong, I love ternaries... but in this case, I just see them as reduntant since the expression returns a boolean value anyway... ??

Code: Select all

$var = (isset($something)) ? true : false; // comment these out and it does the same thing!
$var = (iset($something)) /* ? true : false */;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dude, you don't even need parentheses...

Code: Select all

<?php
$mybool = isset($thisvar);
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

exactly... so does anybody know why people use a ternary in that case? I even see that in the Zend framework.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I nest ternary statements. That's true evil according to my colleagues. Especially when I do it in SQL.
Post Reply