Complete PHP Tutorial – Chapter 7

Here you’ll learn about PHP Control Structures like IF/Else Loop, While Loop, Switch Loop, For/Foreach Loop etc

If Else Statements

Conditional statements perform different things for different states. The if else statement runs a piece of code if a condition is true and other code if the statement is false. The if can also be done by its self, if the statement is not true then nothing will happen and the program will simply continue. Let’s have a look at an example:

PHP Code:

<?php
if(condition){
//run this if condition is true
}else{
//run this code if condition is false, you can also remove this else and just have the if.
}
?>

Now let’s look at an example using some actual code:

PHP Code:

<?php
$x = 2;
$y = 4;
if($x == $y){
echo $x;
}else{
echo $y;
}
?>

Notice in the condition area where we are asking php if $x is equal to $y we can use comparison operators that we looked at earlier. These can also be added to logical operators by separating two conditions with them by simply placing them in the middle.

Elseif Statements

You can use an elseif statement as a continuation of your already existing if statement. They specify a new condition to test and can be used as many times as needed. Here is the syntax for them:

PHP Code:

<?php
if(condition){
//run this if condition is true
}elseif(condition){
//run this code if second condition is true
}else{
//run this code if condition is false
}
?>

And once again here is a code example:

PHP Code:

<?php
$x = 2;
$y = 4;
$z = 3;
if($x == $y){
echo $x;
elseif($x == $z){
echo $z;
}else{
echo $y;
}
?>

While Loop

A loop allows you to run the same snippet of code multiple times with relative ease. This can be amaze extremely helpful tool however is where many beginners start to make logic errors.

The first loop I will show you is the while loop, it is very similar to the if statement but instead if checking a condition once, it will continue to do if until the condition is false. The syntax is very simple:

PHP Code:

<?php
while(conditon){
//so until the condition is false do this code
}
?>

It’s very important to remember that if the condition becomes false the code will loop forever. Make sure there is a way for the condition to exit.

Now let’s have a look at an actual code example:

PHP Code:

<?php
$apple = 20;
$pear = 10;
while($apple > $pear){ //While apple is bigger than pear.
echo $pear; // output pear
$pear++; //add one to pear so eventually the condition becomes false and the loop exits
}
?>

There is also a similar loop named the “do while” loop. It is the same as a whlie loop but it checks for the condition after it runs the code, meaning the code inside will always run at least once. It’s syntax is largely the same but has the while statement in a different position. Let’s have a look at the syntax and a working code example.

PHP Code:

<?php
do{
//code
}while (condition);
?>

PHP Code:

<?php
$x = 0;
do{
$x++;
}while ($x==5);
echo $x;
?>

For/Foreach Loop

A for loop is used when you know how many times you will need to loop the code in advance. Within them you first need to initialize a loop counter, then you need a condition to be checked so the program knows when to exit the for loop. You also need an increment to change the counter variable every iteration of the loop.

PHP Code:

<?php
for(init; test; increment){
//code
}
?>

Let’s have an example that shows numbers 0 to 5. We will do this by accessing our counter variable which can be accessed only from within the loop.

PHP Code:

<?php
for($a; $a > 6; $a++){
echo $a;
}
?>

You can also use a Foreach loop which instead of giving a direct amount of times to run the loop, you give an array. The Foreach loop will then run the loop for every element of the array.

PHP Code:

<?php
$numbers = array(1,2,3);
foreach($numbers as $num => $value){
echo $value;
}
?>

As you can see, this will print out every element within the array. The as in the loop allows us to access the current value within the array that the loop is using. This is updated every iteration to the latest value.

Switch Statement

A switch statement allows for, what would be, a long list of elseif statement but in a more optimised and clean manner. You give the switch a variable and then make a case statement for each value you want to check for. You end each case with a break statement and you can also use default as something a lot like the else command.

Let’s have a look at a quick example of a switch statement.

PHP Code:

<?php
$animal = “Dog”;
switch($animal){
case “Cat”:
echo “your animal is a cat”;
break;
case “Dog”:
echo “your animal is a dog”;
break;
case “Fish”:
echo “your animal is a fish”;
break;
default:
echo “your animal doesnt exist”;
}
?>

Ternary Conditionals

Ternary conditionals can allow for more compact and easy to read code by letting you do simple if/else statements on the fly. They have a syntax like this:

PHP Code:

<?php
variable = condition ? trueValue : falseValue;
?>

Now let’s see a working example.

PHP Code:

<?php
$var = 7;
$var = $var < 10 ? 11 : 7;
?>

These are useful for doing a very quick if statement however if you need to go deeper than 2 levels and want to add an elseif or such to the statement then ternary operators may be aren’t your best choice.

Include and Require

The include control structure allows you to insert a php file into the contents of another. These can be useful when a script (such as one connecting to a database) needs to be run a large amount of times.

Copying out all the code would require a large amount of time and wasted space. With include you can simply run a single line to take care of something like connections. They have a syntax of:

PHP Code:

<?php
include ‘filename.php’;
?>

This can be extremely useful when creating a header or a footer for your page as well. Require has the same syntax (except replace include with require) but unlike include, if the server fails to find the script a fatal error will be returned. With include the server would ignore this and keep going however, it would still show an alert on the page.

Continue to Next Chapter – Complete PHP Tutorial – Chapter 8

Credit Goes to Mr. RYDIOO

You may also like:

Sarcastic Writer

Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.

Related Posts