note 108030 deleted from control-structures.if by googleguy
| From: | googleguy@php.net | Date: | Wed, 10 Oct 2012 20:38:18 +0000 |
| Subject: | note 108030 deleted from control-structures.if by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191245@lists.php.net to get a copy of this message | ||
Note Submitter: marcin_wo_wroc at o2 dot pl
----
Here's something uncommon:
How to make one else statement for two nested if conditions?
aka: how to make one else for two ifs.
By default people simply copy & paste code in else like that:
<?php
if ( is_object($times)){
if ((float)$times->getTime()>0){
//code
}else{
//else code Alpha
}
}else{
//duplicated else code Alpha
}
?>
but it's much better and easier to simply use one condition inside of another, like that:
<?php
if ( is_object($times) ? (float)$times->getTime()>0 : false){
//put here your code that gonna be executed if $times is an object and if $times->getTime() is
greater than zero
//condition is the same as:
//if (is_object($times)){
// if ((float)$times->getTime()>0){
// //code
// }
//}
}else{
//put here your else statement for conditions above
}
?>
simple & beautiful :)