Page 2 of 4

Posted: Mon Jan 08, 2007 11:13 am
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 ;)

Posted: Mon Jan 08, 2007 11:30 am
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>';
}
?>

Posted: Mon Jan 08, 2007 12:36 pm
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.

Posted: Mon Jan 08, 2007 12:37 pm
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
}

Posted: Mon Jan 08, 2007 12:39 pm
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.

Posted: Mon Jan 08, 2007 12:49 pm
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).

Posted: Mon Jan 08, 2007 12:52 pm
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.

Posted: Mon Jan 08, 2007 1:09 pm
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';
:)

Posted: Mon Jan 08, 2007 2:10 pm
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.

Posted: Mon Jan 08, 2007 2:16 pm
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!

Posted: Mon Jan 08, 2007 3:04 pm
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.

Posted: Mon Jan 08, 2007 3:22 pm
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 */;

Posted: Mon Jan 08, 2007 4:07 pm
by RobertGonzalez
Dude, you don't even need parentheses...

Code: Select all

<?php
$mybool = isset($thisvar);
?>

Posted: Mon Jan 08, 2007 4:12 pm
by Luke
exactly... so does anybody know why people use a ternary in that case? I even see that in the Zend framework.

Posted: Mon Jan 08, 2007 4:27 pm
by onion2k
I nest ternary statements. That's true evil according to my colleagues. Especially when I do it in SQL.