WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Alternative syntax for for PHP if() control structures
Categories : PHP, Language Expressions Click here to Update Your Picture
Justin French
Date : Mar 07th 2003
Grade : 3 of 5 (graded 4 times)
Viewed : 31978
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Justin French
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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.