ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

20 – PHP If Statement

The if statement is a powerful tool when used properly and it is also necessary for most programming languages because it serves as a conditional operator of your statement. The if statement has different syntax in other programming languages while there are some languages that has the same syntax just like C#, PHP, C++, Java, etc..

 

In PHP the syntax of if statement are the following:

if (condition) {
   Statement...................
}

 

For those who are not familiar with the if statement, come to think of this folowing statement:

 

In our daily life we always make decisions for isntance, If you have a long quiz tomorrow in your Math subject, then you will study hard and review those lessons that your teacher discussed. Otherwise, you will fail the long quiz.

 

The given example was just a simple if/then statement that happens in our daily life and this quite often appears in programming.

 

Example

 

if statement Code:

$your_name= "Kenjie";

if ( $your_name== "Kenjie" ) {
	echo "Your name is Kenjie!<br />";
        echo "Hello " . $your_name;
}
 

Result:

Your name is Kenjie!
Hello Kenjie

You will notice that we compare the variable $your_name with kenjie because of that, the result will be true or equal.

 

Now, let see what will happen when the if statement is not equal or false.

if statement Code:

$your_name = "craig";

if ( $your_name == "Kenjie" ) {
	echo "Your name is Kenjie!<br />";
        echo "Hello " . $your_name;
}

Result:

In the given example, the variable $your_name contained the value “Craig” which is obviously not equal to “Kenjie“. Therefore: The result is false or not equal.

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!