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:
<?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
}
?>
Archived Comments
1. @bibiki, you are right for simpler conditions what you have said is true. But for some unique cases
View Tutorial By: Marcin at 2012-04-05 02:34:13
2. I don't get it... there seem to be only two different sets of instructions, exactly one of which has
View Tutorial By: bibiki at 2012-04-05 02:01:32
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
PHP convert string to lower case
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Error: Length parameter must be greater than 0
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8