If Else Statements and Arithmetic operations in PHP

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- If Else Statements and Arithmetic operations in PHP

All kind of arithmetic operations are 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.

Addition

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:

If Else Statements and Arithmetic operations in PHPCan you guess the output of this program? I am sure you can. It will be just like this

If Else Statements and Arithmetic operations in PHPAs 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 second variable.
  3. Store 5 in third variable.
  4. Make a variable to store the result of first variable multiply with second variable.
  5. Subtract the value of third value from the result and out put the result.
If Else Statements- If Else Statements and Arithmetic operations in PHP

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

  • Equals to: Put a “==” between two values and it will tell whether it 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 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

If Else Statements and Arithmetic operations in PHPAs 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

If Else Statements and Arithmetic operations in PHPOutput of this code is as expected

If Else Statements and Arithmetic operations in PHPelse if statement

Another statement that needs to be discussed here is a combination of 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:

If Else Statements and Arithmetic operations in PHPHere is an actual example of decision making. Output will be:

If Else Statements and Arithmetic operations in PHPThat 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 next tutorials.

Part One: HOW TO RUN YOUR FIRST PHP SCRIPT ON WINDOWS
Part Two: BASICS OF HTML5 ON THE WAY TO PHP LEARNING
Part Three: PHP WORKING WITH DATA TYPES AND VARIABLES
Part Five: TUTORIAL ON TYPES OF LOOPS IN PHP – PHP LOOP TYPES
Part Six: INDEXED AND ASSOCIATIVE ARRAYS IN PHP
Part Seven: FUNCTIONS IN PHP
Part Eight: INTRODUCTION TO MYSQL
Part Nine:CONNECTING MYSQL DATABASE PHP
Part Ten: FORMS IN PHP – PHP FORM EXAMPLE WITH DATABASE
Like this post:
error

Leave a Reply

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