Wednesday, 21 October 2015

#PHPro.: Become a Master in PHP +2:[Control Structures]

[Control Structures]

Control structures are very powerful and important in PHP, and any other language. They are used to test conditions or repeat some code a certain number of times until a certain condition is met. Enough of chit-chat, let's jump right into it.

IF statement
This one is very simple. All it does is tests if a certain statement is true, then runs the code. Else it runs another code. E.g.

make sure you use your php tags, else, apache will just be like what the hell is this.

<?php 

$number = 6; // lets create our variable

if ($number <= 4) {

        echo $number;
} else {
        echo "Nah";
}
?>
Now, what do you think this code will do? Well, we all know that 6 is not less than or equal to 4 so it should be obvious. If you didn't know that. you might want to go back to primary school to check if your math is right... Alright, so it won't echo out the number because the statement isn't true.It will echo "Nah" because we told it to. Simple, right?

Elseif
Thought if statements were cool, elseif is even more powerful. Take a look at the code above. What if we wanted to test another condition if the first wasn't true? That's what elseif is for.
Okay, let's use the same code from up there, only we add an upgrade ;)

<?php 

$number = 6; // lets create our variable

if ($number <= 4) {
        echo $number;
elseif ($number != 7) { // we're telling it to run this code if the statement above is not true
        echo "7 Is a cool number";
} else {  // this is basically a fallback. This code only runs if all of the above conditions are false.
        echo "None of this is true";
}
?>

Note: there is not a limit to how many elseif's you can use. You can test as many statements as you want. Can you now see the power of control structures? You have seen nothing yet.

Switch Statement

A switch statement is used when you want to test one statement against many conditions.

<?php
     switch(55+5) {
              case(50):
              echo "50";
              break;
              case(59):
              echo "59";
              break;
              case(60):
              echo "60";
              break;
      default:
      echo "not found";
      break;
}
 ?>
What this does is test the statement (55+5) against all those cases. So when it finds a match, it executes the code below. We need the break to tell php that we've found what we're looking for. Stop. Now, if none of the cases match, the default code is run. Now, imagine having to that with just the if and elseif. I can't. Thank you, php.



***
[Comparison and Logical Operators]
Comparison operators are used for logical reasoning. Let's just have practical examples.

5=2+3 || 7=2 "||" means OR, so this statement will turn out true if at least One of them is true. 5 is indeed equal to 2+3 so the overall statement is true, even though 7 is not equal to 2. Easy logic. There's nothing complicated about that. Its like saying "you are human or you are alien". It's true because you  have to be one of them.

&&(AND)
7=7 && 3=0 . This is false because for and, all statements must be true for the overall value to be true.
!(NOT)
Put ! in front of anything and it becomes not. So, for the statement to be true, it has to be false. Hahaha, how funny does that sound? I mean, the statement only runs when it is false. Examples please.
!=  !>  !<
***
[Loops]
Loops are very important in PHP. To be a master of PHP, you must be very comfortable with loops. And honestly, its where most of the cool stuff is.
A loop is simple a block of code that will run multiple times. It will keep running as long as a certain condition is true. Let's start with a while loop.

While loop
A while loop is done like this: declare a variable (which you'll use as your counter), set your condition, declare the code you want to run, then increment.

Let's make a loop that'll make the numbers 0 - 9 appear on the screen.
<?php 
 $counter // declares variable counter
while($counter < 10) // means this code will continue to run as long as counter is less than 10
        echo $counter; // makes the value of counter appear
        $counter = $counter + 1; // increases the value of counter by 1
?>
Now start to imagine the possibilities with this. Then try it. Because as Sensei says, "the true masters of PHP started by thinking out of what they were told, and then they made code".
For loop
For loop does basically the same thing as the while loop, only, the syntax is different. You might even find it easier to use. Take a look.

<?php 
      for($counter = 0; $counter < 10;) { // Yep, that's right. You declare the variable right in there.
      echo $counter;
      $counter ++; // a better way to increment. You can use this in all kinds of loops
}
?>
This does the same thing as the while loop we did above.

Meanwhile: Let's talk about concatenation. Its used to add variables/strings/numbers. For example, there's some variable called $this, a number 5 and a string "Yello" we want to appear on the screen. All we have to do is this:
echo $this . "Yello" . 5;
Thats all. 

Foreach loop

How about when you want to use elements from an array? It's easy, just use a foreach loop. It was born to do just that.

<?php 
 $numbers = array(12, 15, 66, 74);

foreach($numbers as $number){
     echo $number;
}
?>
You can also use if statements within loops. How cool is that?
***
Final remarks
Make sure you have these things down in your head. They are the backbones of php, together with functions, which we will enter next time. If you want to become a pro, you need to be proficient when using control structures.
That's all for now on PHPro.
Bye.

No comments:

Post a Comment