People love to compare things. Is this better than that? Are you fatter than me? Am I just as ugly as you are? Is Picard Greater than or Equal to Kirk? I'll tell you right now, Cisco was greater than Kirk and Picard combined. Deep Space 9 FTW. Well, in PHP you can compare all day long with the help of Comparison Operators. They allow you to compare two or more values and then have the program respond appropriately. We will cover these more during the upcoming tutorial on Statements, but here is how they look in code: <html> <body> <?php $my_name = “Mr. T”; if ( $my_name == “Mr. T”) { echo “Your name is really Mr. T? I pity you fool!”; } ?> </body> </html> In the above example, we assigned the value “Mr. T” to the variable $my_name. We then made a program that stated if the value of $my_name was indeed “Mr. T” then print “Your name is really Mr. T? I pity you fool!”. If the value of $my_name had been anything else, the line would not have printed. Note that we used the == to compare the value of $my_name to our criteria (Mr. T). In the following code we will use the not equal to operator (!=). <html> <body> <?php $my_name = “Charlie Brown”; if ( $my_name != “Mr. T”) { echo “Your name is not Mr. T? Then quit that jibba-jabba!”; } ?> </body> </html> The code above checks to see if the value of $my_name is “Mr. T”. Since it isn't, it prints out the line:
You name is not Mr. T? Then quit that jibba-jabba! If the value had been “Mr. T”, then nothing would have occurred. If it is still a little confusing, have no fear. It'll be clear as mud after the next few tutorials.
blog comments powered by Disqus |
|
|
|
|
|
|
|