Categories
PHP

Using Operators in PHP – Learn PHP in 7 Days – Day #2

Operators are used for performing a variety of simple tasks like specifying values to variables, mathematical functions, comparing data etc. Although operators in PHP are categorized in 11 types, but for the sake of this crash course I will be showing you 5 tutorials. But let me list out all the 11 categories of operators.

  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Error Control Operators
  • Execution Operators
  • Increment and Decrement Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Type Operators

Assignment Operator

This will be the most used operator in PHP. Function of this operator is very simple, as the name says the assignment operator is used to assign values to a variable. May it be a number, a string or another variable, or a memory address (if you remember the pointers concept in C language?)

Assignment operator is represented by equals ( = ) sign. Here are a few examples :

$myvar = 4;
$my_string_var = "abc";
$myvar2 = $myvar;
$my_address_var = & $myvar;
?>

Note that my_address_var and myvar point to same memory location thereby contain same values.

Arithmetic Operators

Arithmetic Operators are only used to perform mathematical functions like addition, subtraction etc. There are 5 arithmetic operators in PHP like any other language.

Addition (+)         Subtraction (-)            Multiplication (*)         Division (/)      Modulus (%)

Is there any scope of explanation required for the above operators? Anyways ill try it, these operators are to be put in between 2 operands. Addition is simply adding 2 numbers, similarly subtraction is for subtracting, multiplication for multiplying and division for getting the quotient.

If you don’t know what a modulus operator does, it simple returns the remainder when 2 numbers are divided. Here are a few examples however for self explanation

<?php
$add = 2 + 3;
$subtract = 7 - 2;
$multiply = 3 * 4;
$divide = 24 / 3;
$modulus = 5 % 2; ?>

echo “Perform addition: 2 + 4 = “.$add.”<br />”;
echo “Perform subtraction: 6 – 2 = “.$subtract.”<br />”;
echo “Perform multiplication:  5 * 3 = “.$multiply.”<br />”;
echo “Perform division: 15 / 3 = “.$divide.”<br />”;
echo “Perform modulus: 5 % 2 = ” . $modulus
?>

This is the output for the above piece of code

3-operators-1

Comparison Operators

Comparison operators are used to check the relationship between variables and/or values. They can only return a true or false value based on the statement that we provide. There are 6 comparison operators (up to my knowledge)

Equal (==) – to check if both the operands are equal in value
Not Equal (!=) – to check if both the operands are not equal in value
Less Than (<) – to check if first value is less than the second
Greater Than (>) – to check if first value is greater than the second
Less Than or Equal (<=) – to check if first value is less than or equal to the second
Greater Than or Equal (>=) – to check if first value is less than or equal to the second

Note that there is a huge difference between “==” and “=”. == is used to evaluate where as = is used to assign. So statement like

if ($x = 3)

Will always return true even if values of $x was not 3.

<?php
$a = 4;
$b = 4;
$c = 6;

echo $a == $b;
echo $a != $b;
echo $a < $c;
echo $a > $c;
echo $a <= $c;
echo $a >= $c;
?>

The above code will output something like this.

3-operators-2

1 represents “true” in Boolean. Yeah you already know it smart pants. So here lines 1,3,5 are true and lines 2,4,6 are false. But we don’t see any 0′s right? I know you’re smart. Echo statement cannot output Boolean false or in other terms Boolean false is nothing but a NULL value, so echo statement cannot simply output NULL values. Hence you don’t see any 0′s here.

String Operators

I think we have already seen the string operator (which I call as the dot operator) used to join multiple strings or variables. If you don’t remember I won’t mind explaining once again. You can use the string operator to join multiple strings or strings and variables like this.

$middlename = "Ameenuddin";
echo "My name is" . "Atif";
$myfullname = "Mohammed " . $middlename . " Atif";
echo "Name : " . $myfullname;

So you remember it. That’s good. Let us move on to the last type of operators now

Increment and Decrement Operators

An increment or a decrement operator can simple increment or decrement the value of any variable with 1. This can work only on integers and floating point number so forget about strings here. (Am I being rude?). Let us take an example, say we want to increment the value of $counter with 1. This is the classic way we would write

$counter = $counter + 1;

Or maybe like this too (called as shorthand)

$counter += 1;

It may look a bit absurd to write long statements for the sake of increasing a value by 1. Like most of the programming languages PHP too supports increment and decrement operators. This is however further sorted into

  • Pre Increment/Decrement
  • Post Increment/Decrement

Pre Increment/Decrement

In this way, the value of the variable is first incremented or decremented based on the operator and then returned back. A few examples for explanation

<?php
$x = 3;
$y = 7;

echo ++$x;
echo “<br />”;
echo –$y;
?>

And the output for this is

3-operators-3

So you already knew that huh! Yes the value of x is increment by 1 and printed as 4 and the value of y is decremented by 1 and printed as 6.

Post Increment/Decrement

Post Increment/Decrement works exactly opposite to pre increment/decrement. Here the variable is first returned back and then the value of the variable is incremented or decremented based on the operator. Moving forth to examples to make it simpler

<?php
$x = 3;
$y = 7;

echo “the value of x = ” . $x– . “<br/>”;
echo “The value of x is now changed to $x” . “<br/>”;;
echo “the value of y = ” . $y++ . “<br/>”;;
echo “The value of y is now changed to $y”;
?>

3-operators-4

As you can see the value of $x—or $y++ is not reflected in the first echoed text because the variable is not incremented until after the line of code is executed. However, with the pre-increment “++$x” the variable does reflect the addition immediately on the same line because it is done before returning output to the buffer.

Related posts:

  1. PHP Syntax, Variables, Echo Statement – Learn PHP in 7 Days – Day #1
  2. Learn PHP in 7 Days
  3. Sanitizing Variables in PHP without Regular Expressions
  4. MS DOS Tutorials – How to Create, Delete, Change, Rename Directory and Files
  5. MS DOS Tutorials – Learn MS DOS with an ease