How to make one else for two ifs in PHP

By: Marcin  

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:
if ( is_object($times)){
 if ((float)$times->getTime()>0){
     //else code Alpha
    //duplicated else code Alpha

but it's much better and easier to simply use one condition inside of another, like that:

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
    //    }
    //put here your else statement for conditions above

Archived Comments

Most Viewed Articles (in PHP )

Latest Articles (in PHP)

Comment on this tutorial