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

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /