note 108030 added to control-structures.if
| From: | marcin_wo_wroc at o2 dot pl | Date: | Fri, 23 Mar 2012 10:26:08 +0000 |
| Subject: | note 108030 added to control-structures.if | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186787@lists.php.net to get a copy of this message | ||
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 :)
----
Server IP: 69.147.83.197
Probable Submitter: 83.218.141.178
----
Manual Page -- http://www.php.net/manual/en/control-structures.if.php
Edit -- https://master.php.net/note/edit/108030
Del: integrated -- https://master.php.net/note/delete/108030/integrated
Del: useless -- https://master.php.net/note/delete/108030/useless
Del: bad code -- https://master.php.net/note/delete/108030/bad+code
Del: spam -- https://master.php.net/note/delete/108030/spam
Del: non-english -- https://master.php.net/note/delete/108030/non-english
Del: in docs -- https://master.php.net/note/delete/108030/in+docs
Del: other reasons-- https://master.php.net/note/delete/108030
Reject -- https://master.php.net/note/reject/108030
Search -- https://master.php.net/manage/user-notes.php