Everything You Need To Know About 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
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.
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:
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.
- Store value of 56 in a variable.
- Store value of 23 in the second variable.
- Store 5 in the third variable.
- Make a variable to store the result of the first variable multiply with second variable.
- 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
The Output of this Code is as Expected
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.
Also See: How to Make Money Doing Online Coding Lessons Gigs on Fiverr
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
Fun Fact
What is PHP and why it is used?
PHP is a server facet scripting language this is embedded in HTML. It is used to control dynamic content, databases, consultation tracking, even construct complete e-trade sites. It is incorporated with some of famous databases, together with MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
Also See : BITCOIN PRICES: ARE WE IN A BUBBLE?