Little known PHP Idioms etc.
Moderator: General Moderators
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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>';
}
?>Does anybody use a short condition check?
not a tricky, but found very usefull for myself some time ago.
Code: Select all
$var = $x?true:false;- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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
}- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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).nitrino wrote:Does anybody use a short condition check?
not a tricky, but found very usefull for myself some time ago.Code: Select all
$var = $x?true:false;
why do people do this:
as opposed to this?
Never understood that.
Code: Select all
$var = $x?true:false;Code: Select all
$var = (bool) $x;It was just an example, it could beThe Ninja Space Goat wrote:why do people do this:as opposed to this?Code: Select all
$var = $x?true:false;Never understood that.Code: Select all
$var = (bool) $x;
Code: Select all
$var=$x>3?'apple':'banana';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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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... ??Burrito wrote:I use ternary operators all day long
Code: Select all
$var = (isset($something)) ? true : false; // comment these out and it does the same thing!
$var = (iset($something)) /* ? true : false */;- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Dude, you don't even need parentheses...
Code: Select all
<?php
$mybool = isset($thisvar);
?>