Sometimes the standard if() control structure in PHP can use up a LOT of lines, reducing the
simplicity and clarity of your code.
Imagine a config file which has setting for both your local test server, and the live production
server:
<?php
// messy way
$local = true ;
if( $local ) {
$admin_email = "justin@indent.com.au" ; // me
} else {
$admin_email = "someone@somewhere.com" ; // client
}
if( $local ) {
$base_url = "http://127.0.0.1/clientname/" ;
} else {
$base_url = "http://www.somewhere.com" ;
}
// etc
?>
Instead, I can reduce these 5 line config statements to just one line, and make things a LOT
easier to read:
<?php
// clean way
$local = true ;
$admin_email = ( $local ) ? "justin@indent.com.au" : "someone@somewhere.com" ;
$base_url = ( $local ) ? "http://127.0.0.1/clientname/" : "http://www.somewhere.com" ;
?>
It's a little bit hidden in the PHP docs, so not many people know about it. For more info, try:
http://www.weberdev.com/Manuals/PHP/language.expressions.html
Enjoy!
Justin French
Indent.com.au
Australia
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP , PHP , HTML , PDF , Excel Calendar using Date function Categories : HTML and PHP , PHP , Date Time , Calendar Rich Editor (RE) is a cross-browser WYSIWYG html editor Categories : Content Management , Editors and IDEs , PHP , Complete Programs PHP Event Logger Categories : PHP , Log Files , Errors and Logging , Beginner Guides SPL and ITERATOR : examples Categories : PHP , Object Oriented , PHP Classes , Sessions How to overide the Max_file_size barier Categories : PHP , PHP Configuration , Filesystem UDMSearch - a free search engine, indexing system. Categories : Search Engines , Linux , PHP , MySQL , ODBC how to check if a string contains a letter from a different language? Categories : PHP , Regexps , Languages Class: Info on Users, Servers and the running script Categories : PHP , Classes and Objects , User Interface , PHP Classes phpEasyMail: An easy way to send data from HTML-forms via EMail. Categories : Email , HTML and PHP , Complete Programs , PHP fforum fumanchi forum MySQL treestructure Categories : Complete Programs , PHP , MySQL ibase_close -- Close a connection to an InterBase database Categories : PHP , PHP Functions , InterBase Website colour changer Categories : PHP , HTML and PHP , Date Time Sending mail to a mailing list and showing progress Categories : PHP , Mail , Beginner Guides ezRemoteScripter - A little remote scripting (AJAX) helper Categories : PHP , Java Script , AJAX
Juan Pablo Aqueveque wrote : 897
pretty cool, thanks justin.
Eric Grigsby wrote : 899
Would the same principal if your IF statement was nested?
Philip Evans wrote : 900
Yes it will do. Because all you put either side of the colon are two values. It doesn`t matter if you use another if statement (either format) to generate that value.
Sarah King wrote : 952
Just a pointer for people who know other languages. This is php`s version of an iif() statement which I`ve seen all over the place.
If you`re struggling with something and know the syntax from another language then you`ll be able to do it in PHP. Can`t find it? Post a question on the forums.