+91-943-185-6038 me@shashidharkumar.com

The Basics Of IF
If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.

IF Strucure
IF (something == something else)
THEN Statement
} else {
ELSE Statement

The most common use of an IF statement is to compare a variable to another piece of text, a number, or another variable. For example:
if ($username == “webmaster”)
which would compare the contents of the variable to the text string. The THEN section of code will only be executed if the variable is exactly the same as the contents of the quotation marks so if the variable contained ‘Webmaster’ or ‘WEBMASTER’ it will be false.

Constructing The THEN Statment
To add to your script, you can now add a THEN statement:
if ($username == “webmaster”){
echo “Please enter your password below”;

This will only display this text if the username is webmaster. If not, nothing will be displayed. You can actually leave an IF statement like this, as there is no actual requirement to have an ELSE part. This is especially useful if you are using multiple IF statements.

Constructing The ELSE Statement
Adding The ELSE statement is as easy as the THEN statement. Just add some extra code:
if ($username == “webmaster”) {
echo “Please enter your password below”;
} else{
echo “We are sorry but you are not a recognised user”;

See also  How to use hooks (actions and filters) in wordpress and woocommerce

Other Comparisons
There are other ways you can use your IF statement to compare values. Firstly, you can compare two different variables to see if their values match e.g.
if ($enteredpass == $password)
You can also use the standard comparision symbols to check to see if one variable is greater than or less than another:

if ($age < “13”)
Or :
if ($date >$finished)
You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use:
if ($name == “” || $email == “” || $password == “”) {
echo “Please fill in all the fields”;