for the search, my way to preload images! haha

Search for in

Alternative to if /else statements

PHP Coding Tutorials | Submited May 3, 2008

Written by: John

If you're getting tired of always use if / else statements for only 1 variable then heres a simple aleternative that is fast, and really easy to remember.
If you have a bunch of if / else statements for one variable then this method would be a lot better to use. Heres an example of what you would currently have in your coding


$check = 'yes';
if($check == 'yes'){
echo 'go';
} else {
echo 'stop';
}

The above would simple return "go" because $check does equal yes, but we could write that totally different like the following example


$check = 'yes';
$do = ($check == 'yes') ? 'go' : 'stop';
echo $do;

The above will output "go" because $check does equal yes as you can see right before it. If $check did not equal 'yes' and instead equaled 'no' $do would echo out "stop".

So, its almost like saying
$variable = (if $check == 'yes') ? true : false;

true in the above statement would always be true as long as $check is aslways "yes"

tags Tags: alternative if else php easy tricks optimizations

0Thumbs up  Thumbs down0

Related Tutorials
Easy White Chocolate Coconut Cheesecake
Alternative to if /else statements
Creating "Your IP is" images
No Comments Found
Sorry, you have to be a member to comment tutorials