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 )

PHP code to write to a CSV file from MySQL query

Different versions of PHP - History and evolution of PHP

PHP code to import from CSV file to MySQL

Send push notifications using Expo tokens in PHP

Encrypting files using GnuPG (GPG) via PHP

PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......

A Basic Example using PHP in AWS (Amazon Web Services)

Decrypting files using GnuPG (GPG) via PHP

Parent: child process exited with status 3221225477 -- Restarting

Error: Length parameter must be greater than 0

Convert a hex string into a 32-bit IEEE 754 float number in PHP

Password must include both numeric and alphabetic characters - Magento

Convert XML to CSV in PHP

PHP file upload prompts authentication for anonymous users

Count occurrences of a character in a String in PHP

Latest Articles (in PHP)