# If Else Statements and Arithmetic operations in PHP

## Everything You Need To Know About If Else Statements and Arithmetic operations in PHP

Page Contents

Welcome to another tutorial of PHP. We hope this journey is going great for you so far. In this tutorial we will discuss how we can make some decisions in PHP using if and else statements and also what are the basic arithmetic operations. There is going to be a lot of practical coding. So let’s start.

### Arithmetic Operations

All kind of arithmetic operations is possible in PHP including addition, subtraction, multiplication and division. We apply these to integer and float data types. Let’s discuss all the operations in detail.

Syntax of addition is the best logical answer comes in mind. Write first value, add a “+” sign and then add the other value. In this way both of the values will be added. Code example is given.

<?php

\$firstValue =35;

\$secondValue= 12;

\$result=\$firstValue+\$secondValue;

echo \$result

?>

Whole code in HTML will look like this: Can you guess the output of this program? I am sure you can. It will be just like this As you know the addition, you can easily do other operations. Just remember the signs.

Now your task is to write and run a program with these specifications.

1. Store value of 56 in a variable.
2. Store value of 23 in the second variable.
3. Store 5 in the third variable.
4. Make a variable to store the result of the first variable multiply with second variable.
5. Subtract the value of the third value from the result and output the result.

### If Else Statements

This part of the tutorial helps you learn the decision making in PHP. But before jumping to it, let’s first discuss how we can make a comparison. We can use different operators to compare values as

• Equals to: Put a “==” between two values and it will tell whether they are equal or not.
• Not equals to instead of “==”, just use a “!=” as a not equals to sign.
• Greater than: “>” is used to determine which value is greater than the other.
• Less than: As expected “<” sign is used for this purpose.
• Greater than or equals to “>=” will be true if either first value is equal or greater than other.
• Less than or Equals to: “<=” is to determine whether the first value is less or equal to the other or not.
• And: We sometimes need to use ‘&&’ to put an ‘And’ statement.
• Or: ‘||’ works as the OR operator in PHP.

### If Statement

The correct way of writing this statement is given

If(CONDITION){

CODE

}

These “{}” are here to decide the boundary of if statement. Now, the “CODE” given inside this statement will only execute if the CONDITION is true. Otherwise, all of the code will be skipped. For example,

<?php

\$test=20;

If (\$test==19){

echo “Variable value is 20”;

}

echo “Always print”;

Here we defined a value of \$test variable to 20. And in the ‘if’ condition we are checking whether it is equal to 20 or not. If it is 20, it will print the echo statement inside the ‘if’ brackets. Otherwise, just the second echo will be printed that is outside of the brackets.

Here is the output of this code As expected right? Now try one more time with \$test =19;

### Else Statement

It is another really important statement, it always works with if condition. You cannot separately write an else statement. Here are the rules to understand its working

• We can write an ‘if’ statement without the ‘else’
• But if we are writing an ‘else’ statement, there must be an ‘if’ before it.

Here is the example

###  else if Statement

Another statement that needs to be discussed here is a combination of the previous two, called else if statement. At least one if statement must be there to write an else if statement

Let’s discuss the three of them together: Here is an actual example of decision making. Output will be: That is all for today. Let us know your suggestions and reviews through comments. Next tutorial is going to be about Loops in PHP. We will write a PHP script to print the table of 5. So be ready and do practice all the techniques we discussed today. As all of them will be used in the next tutorials.

##### Part Ten: FORMS IN PHP – PHP FORM EXAMPLE WITH DATABASE

Main Image Source : Pixabay

Also See : BITCOIN PRICES: ARE WE IN A BUBBLE?

#### Dave Peterson

Dave Peterson Passion for adventure and sharing his life long journey with as many others as possible. "What lies behind us and what lies before us are tiny matters compared to what lies within us." HENRY S. HASKINS 