How To Develop Your Very Own PHP Game
Tutorial on making a PHP game, from scratch :)

Step 6: Conditional Expressions/IF() and IF()-ELSE()

By justImagine
The Expressions here doesn't refer to how your face looked in moods, but to a functional parameter in some PHP basic functions.
First, try making a file in your host and name it 'if1.php'. Then write these down:

<?php
$name = "Anton";

if($name == "Anton")
print("Welcome, Anton!");
?>


Those short script will have an output about like this:

Welcome, Anton!

Now, for the explanation. The term $name = "Anton"; creates a new variable called $name containing a string (a text or a line of characters that can be either alphabetic or numeric, and doesn't work as in for numeral processes like plus or multiply) "Anton". And then the function if() works as the conditional function. It states $name == "Anton" which pretty much means if the content of variable $name contains the string Anton. If the value of the test is true, then the statement print("Welcome, Anton!"); is executed. Keep in mind that it uses two equal signs (==). It means it does NOT create a variable, instead it checks the content of the variable whether it's the same as the conditional string (or content) or not. There is also three equals (===) to make difference between "1" with the boolean (true or false values, sometimes outputs 1 or 0) "TRUE" and "0" with "FALSE". There are also > (greater than), < (less than), >= (greater than-equal to), <= (less than-equal to), and != (not equal to). The first 4 can only be used in numerical operations, like if($number > 5) (the script means 'if the content of variable $number is greater than 5, but the statement to execute isn't written. Well, it's just an example, right?).
Now edit the if1.php and change the content of $name into "David", and add these after the print() on if():

else
print("Welcome, guest!");


The result will be:

Welcome, guest!

Why? Because we added the else() statement after if. This made the if() have the if()-else() form. The cause is, when the test on the if() returns FALSE value, then the statement on else() is executed. Like, if you're going to a mall, and you faced a two-way road. If you know that the road left side heads NOT to the mall, you'd go to the right side road. Like if(left_side_road heads to "mall"){ goToLeftSideRoad; }else{ goToRightSideRoad; } if you understand what I mean. In that case, I also used another form of if():

if(logical_test)
{
statement_if_true;
}


The underlined parts made it. Yup, it's if() with brackets. These are useful if the statement to execute under if() took more than one line. For example:

if(logical_test)
{
print("statement_1");
print("statement_2");

}


That if contains 2 statements to execute if the value is TRUE: the first print and the second one. Get it?

On the next meeting, we will talk about another conditional expressions. Keep your eyes peeled AND never get bored on learning!
 

0 comments so far.

Something to say?