PHP BasicIntroduction to PHP PHP Starting Requirements Downloading XAMPP Local Server PHP Variable Concept and Data Type PHP Coding Syntax Showing Output in PHP PHP String Concatenation Increment & Decrement in PHP PHP Logical Operator and Usage Part-1 PHP Logical Operator and Usage Part-2 PHP Comparison Operators Arithmetic and Assignment Operators PHP Switch Case Statement For loop in PHP Super Global Variable $_REQUEST and $_GET Part-1 String Length in PHP Function Declaration and Usage
An operator is the most important term in php language like other programming languages. Because without these operators it is impossible to implement logic, compare, math and so on. In php language, there are almost 6+ types of operators. Most of them are highly used in developing work. Let's go through below.
Arithmetic operators are mainly used for doing the calculation in PHP language. These operators are used for doing several calculations. They are addition(+), subtraction(-), multiplication(*), division(/), modulas(%). For adding two several numbers (+) is used. Let's an example from below;
<?php //addition of two numbers $firstNum = 60; $secondNum = 50; $result = $firstNum+$secondNum; echo $result; ?>
The output of Addition:
In the same way, we can easily implement subtraction, multiplication and division also by using -, *,/. Let's see the below PHP code where subtraction, multiplication and division are used.
<?php $firstNum = 60; $secondNum = 50; $subResult = $firstNum - $secondNum; //subtraction $multiResult = $firstNum * $secondNum; //multiplication $divResult = $firstNum / $secondNum; //division echo "The subtraction is " . $subResult . "
"; //subtraction output print echo "The multiplication is " . $multiResult . "
"; //multiplication output print echo "The division is " . $divResult; //division output print ?>
Output of Subtraction, Multiplication and Division(combined):
The subtraction is 10 The multiplication is 3000 The division is 1.2
Modulus is mainly used to find out the keeping value of a variable divided by another variable. This is applicable for integer value but also can be used for float/double values. For doing the modulus calculation modulus(%) operator is used. For better understanding let's look below example
<?php $firstNum = 6; $secondNum = 5; $modulus = $firstNum % $secondNum; echo $modulus; ?>
Output of modulus is
Assign operator is mainly an equal sign but it plays different duties in programming. In programming language, there is a confusion with equal(=) sign. Because it sometimes plays a role as an assignment operator or comparison operator. But there is a little bit of difference between them. Equal(=) is an assignment operator whereas double equal (==) is a comparison operator. We will discuss comparison(==) operator in upcoming next post. When the value of a variable is replaced by another value then it is called assign. For assigning a new value to a previously declared variable assign(=) operator is used. This is totally same as equal sign. Let's see an example for better clearance what is an assignment operator
<?php $num1 = 5; // value of num1 is 5 echo "Number1 value is " . $num1 . "
"; $num2 = 10; // value of num2 is 10 echo "Number2 value is " . $num2 . "
"; $num1 = $num2; // now value of num1 is 10 replaced by num2 echo "Number1 value is " . $num1 . "
Number1 value is 5 Number2 value is 10 Number1 value is 10