Programming Tutorials

How to make one else for two ifs in PHP

By: Marcin in PHP Tutorials on 2012-04-04  

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
}
?>





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

Latest Articles (in PHP)