Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
PHP
Hi again, to all readers of PHP section! Hope you grasped a lot about arrays and type casting from my last article and tried them out. If anyone is wondering why I explained about arrays upfront with a lot of importance, you will be able to clear yourself soon. Today we are going to focus on Operators in PHP.
Operators
When you develop applications or websites surely you will have to use a whole bunch of operators. If you have done programming in your past, I’m sure you are very much familiar with many of these. Actually we used a few in our previous articles too, even though we did not specially focus on them. So now it is time for me to explain more about operators. So what are these? Operators are various symbols that we can use to manipulate values and variables by performing an operation on them. In general, an operator can take one, two or three arguments (operands) – the things which need to be manipulated by the operator.
There are several kinds of operators available in PHP. Let’s take a look at each of them now.
Arithmetic operators allow you to perform basic mathematical operations and these are quite straight forward. These are applied to variables/values of integer or double data types.

Let’s take a look at some examples:
<?php $a = 24; echo $a + 10; // 34 (Addition operator) echo $a - 3; // 21 (Subtraction operator) echo $a * 2; // 48 (Multiplication operator) echo $a / 6; // 4 (Division operator) echo $a % 5; // 4 (Modulus operator) echo ++$a; // 25 (Increment operator) echo --$a; // 24 (Decrement operator) ?>
I suppose addition, subtraction, multiplication and division are self explanatory. The modulus operator (%) returns the remainder of dividing 24 by 5, which is 4. The final two arithmetic operators increment and decrement the value of variable by 1 respectively.
I would like to explain another point about the increment (++) and decrement (--) operators. These operators can have two different kinds of effects on its operands depending on the order they appear. If the operator appears before the variable, PHP first increments the value of variable and return the incremented value (pre-increment). In contrast, if the operator appears after the variable, PHP first returns the current value of variable and increment the value afterwards (post-increment). See the following example:
<?php $a = 24; echo ++$a; // Prints 25 (Pre-increment operator) ?> <?php $a = 24; echo $a++; // Prints 24 (Post-increment operator) echo $a; // Prints 25 ?>
The decrement operator too has the same effect. It is up to you to decide the suitable effect for your application.
String Operators
There is only one string operator present and it is the concatenation operator. It can be used to combine multiple string values to form a new string value.
<?php $word1 = "Hello"; $word2 = "World"; echo $word1." ".$word2; // Hello World ?>
Assignment Operators
The purpose of assignment operators is to set values to variables. These can take two operands.

The second column (equivalent to) can be of a big help for you to understand the meaning of each assignment operator since it is comprised of the operators we learnt in previous headings. Assignment operators are used to store modified values in to variables.
<?php $x = 50; // $x is set to 50 $x += 10; // $x is incremented by 10; $x=60 $x -= 20; // $x is decremented by 20; $x=40 $x *= 2; // $x is multiplied by 2; $x=80 $x /= 8; // $x is divided by 8; $x=10 $x %= 3; // $x is set to its remainder when divided by 3; $x=1 $a = "hello"; $a .= " world"; // the word "world" is combined to the $a; $a="hello world" ?>
As you notice, assignment operators exist for each of the arithmetic operators and string concatenation operator.
Comparison Operators
The aim of comparison operators is to compare two operands. The return value of an expression with a comparison operator is a Boolean value (true or false) depending on the result of comparison.

Again these are self explanatory as most are commonly used symbols in mathematics. I would like to point out it is easy for someone to confuse the == operator with = operator. You should take note that while == is a comparison operator (to check if something is equal to another), = is an assignment operator (assign a value to a variable). Make sure you won’t make a mess of these two or you will end up with unexpected results. I have few examples on comparison operators below.
<?php $result = (3==4); var_dump($result); // bool(false) $result = (3!=4); var_dump($result); // bool(true) $result = (3>4); var_dump($result); // bool(false) $result = (3<4); var_dump($result); // bool(true) $result = (3<=4); var_dump($result); // bool(true) $result = (3>=4); var_dump($result); // bool(false) ?>
Logical Operators
Logical operators are used to combine the results of logical comparisons. For example you may want to check whether someone’s age is between 18 and 55 – you can make use of logical operator “&&”. The following are some commonly used logical operators in PHP.

Let’s look at some examples:
<?php $age = 24; $result = ($age>18 && $age<55); var_dump($result); // bool(true) $result = ($age==20 || $age==24); var_dump($result); // bool(true) $result = !($age==24); var_dump($result); // bool(false) ?>
That’s it for the topic about operators. Apart from that, I also wish to convey some PHP naming standards to you in today’s article. It is true that you have the freedom to establish your own standards. But imagine you’re working for a team of developers in a large-scale project. You will have to adhere to established standards as the other developers too may have to modify codes you have written. Since you already have the knowledge about variables and constants, I thought it’s a good idea to get used to the some well-known conventions from the beginning itself.

Alright! We learnt PHP operators and had a quick look at few naming conventions today. We are about to wrap up this month’s article. But before that I would like you to think about following scenarios and find out the operators which can be used to solve each of them… Start thinking, it’s fun! :-)
Given Namal’s and Sherin’s salaries, you need to know whether Namal gets a higher salary than Sherin.
Given Sunil’s annual income, you need to calculate monthly salary.
Given first name and surname of a person, you need to output full name.
Given a number, you need to know whether it is odd or even.
Given Janaka’s age and exam results, you need to know whether he has passed and younger than 18 years.

Post new comment