Posted on August 1st, 2008
30921 views
Quickly Assign Variables a Value In One Line Using If...Else in PHP
Do you have a code like this: <? 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] 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? <? Do you want to use proper grammar when you output sentences with a dynamic number? <? 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! |