Publishing System Settings Logout Login Register
One Line If...Else Statements for Variables
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on August 1st, 2008
30921 views
PHP Coding
Quickly Assign Variables a Value In One Line Using If...Else in PHP

Do you have a code like this:
<?
if($hour < 12) { $greeting = "Good morning!"; }
else { $greeting = "Good afternoon!"; }
?>


Did you know that you can shorten that code into a single line with a simple PHP format?

The format is below, and each part is explained in the paragraphs following.  Seeing as the code example at the beginning of the tutorial is fairly easy to understand and infer the purpose of, we're going to use that.

<?
$[VarName] = ([If]) ? [IfTrue] : [IfFalse];
// The following is what the example at the beginning of the tutorial would look like.
$greeting = ($hour < 12) ? "Good morning!" : "Good afternoon!";
?>


[VarName] The name of the variable that you want the output to be placed in.  For the example, it is greeting.

[If] The if statement your wanting to put the variable through.  It has to be in parentheses!  For the example, it is ($hour < 12).

[IfTrue] The variable's value if the if statement is true.  For the example, it is "Good morning!".

[IfFalse] The variable's value if the if statement is false.  For the example, it is "Good afternoon!".


Continue onto the next page to see various examples of this function in action!


Code Examples

Have you stored a stylesheet name in a cookie, but also have a default one for those who haven't yet set it?
<?
$css = (isset($_COOKIE['css'])) ? $_COOKIE['css'] : "default";
print $css.".css";
?>



Do you want to use proper grammar when you output sentences with a dynamic number?
<?
$numInSent = ($num == 1) ? "file" : "files";
print 'You have loaded '.$num.' '.$numInSent.'.';
?>



As you can see, this function can be very useful when scripting in PHP for those pesky two-line variable definers.  I hope this tutorial helped you with your future PHP endeavors! :)

ALWAYS make sure that your 'if' statement is in parentheses!

Thank you for viewing!
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Rcty

Hi!

I don't write many tutorials, and I'm really good at getting into PHP problems that even my really experienced PHP coding friends have to scour to find! However, I promise that I put legit content on my tutorials. :)
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top