|
|
|
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 | | | Retrieve text from table and email to your e-
address in pipe delimited format. Categories : PHP, MySQL | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | A PHP function to encrypt and decrypt a number or string or a combination of the two. Categories : PHP, Encryption, Security | | | Using $PHP_AUTH_USER and $PHP_AUTH_PW to authenticate. Categories : Authentication, PHP | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality. Categories : PHP, GD image library, Graphics | | | readline -- Reads a line Categories : PHP, PHP Functions, Readline | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Math operations on big numbers Categories : PHP, Math. | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | |
| | | | 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.
| |
|
|
|