Login Register


PHP Operators filter_list
Author
Message
PHP Operators #1
It's been a bit since I've added anything useful to the forum so I decided to write a list of PHP operators with their definition and a code example.

What is an operator?
An operator is something that takes one or more expressions and creates a new value.

Example: 2 + 3
2 and 3 are variables
The + sign is our operator
The new value is 5

Prerequisites
In order to FULLY grasp everything in this tutorial you should understand basic algebra and have some understanding of programming.

Let's Get Started!



Arithmetic Operators
An arithmetic operator is an operator used for simple manipulation of numbers (+,-,/,*,etc)

+ Addition
This one shouldn't need much explanation, if it does visit Khan Academy

Example:
PHP Code:
$var = 1 + 3; echo $var; // should output 4

- Subtraction
This one shouldn't need much explanation.

Example:
PHP Code:
$var = 5 - 1; echo $var; // should output 4

* Multiplication
The * is used to represent the multiplication symbol.

Example:
PHP Code:
$var = 5 * 2; echo $var; // should output 10

/ Division
The / is used to represent the division symbol.

Example:
PHP Code:
$var = 10 / 2; echo $var; // should output 5

% Modulus
The % symbol is used to gather the remainder after division.

Example:
PHP Code:
$var = 11 % 3; echo $var; // should output 2

A good way to use Modulus is checking for even or odd numbers.

Example:
PHP Code:
$var = $input % 2; if ($input == 0) { // No remainder so this is even echo 'it is even!'; } else { echo "boo it's odd"; }

** Exponent
Not to be confused with the multiplication symbol, the exponent will allow you to raise numbers to the nth power.

Example:
PHP Code:
$var = 5 ** 2; echo $var; // should output 25

-$var Negation
Dangerously close to the subtraction symbol, the negation symbol (Prefixing a variable) makes that int negative.

Example:
PHP Code:
$a = 5; echo -$a; // should output -5




Incrementing/Decrementing Operators
An Incrementing operator is a single operator that allows you to increase your variable by 1. A decrementing operator is the same thing except with subtraction.

++ Increment
This will simply add 1 to a variable by reference. (It wont return anything).

Example:
PHP Code:
$a = 5; $a++; echo $a; // should output 6

-- Dscrement
This will simply subtract 1 from a variable by reference. (It wont return anything).

Example:
PHP Code:
$a = 5; $a--; echo $a; // should output 4




Comparison Operators
A comparison operator allows you to compare to variables, thus outputting true or false. Commonly used for if statements.

== Equal
This will check to see if two variables equal each other in a non-strict fashion.

Example:
PHP Code:
echo 1==1; // should output 1(True) echo 1==2; // should output 0(False) echo 1=='1' // should output 1 since == is not strict

=== Identical
This will check to see if two variables equal each other and are the same casting type. (Int, string, etc). There are few reasons to actually do this and some developers will argue if you actually should. But one good reason is in PHP there is an issue where 0 == 'a' will return true. This is due to "type juggling". The only way to make 0 == 'string' return false is to use identical. Like 0 === 'a' (This will return False)

Example:
PHP Code:
echo 1==1; // should output 1(True) echo 1==2; // should output 0(False) echo 1=='1' // should output 0 since int does not equal string

!= Not Equal
This will check to see if two variables are not equal to each other in a non-strict fashion.

Example:
PHP Code:
echo 1!=1; // should output 0(False) echo 1!=2; // should output 1(True) echo 1!='1' // should output 0

!== Not identical
This will check to see if two variables are not equal to each other in a strict fashion.

Example:
PHP Code:
echo 1!==1; // should output 0(False) echo 1!==2; // should output 1(True) echo 1!=='1' // should output 1 since int != string

< Less Than
Check to see if one variable is less than the other.

Example:
PHP Code:
echo 1<2; // Should output 1(True) echo 2<1; // Should output 0(False) echo 1<1; // Should output 0(False)

> Greater Than
Check to see if one variable is greater than the other.

Example:
PHP Code:
echo 1>2; // Should output 0(False) echo 2>1; // Should output 1(True) echo 1>1; // Should output 0(False)

< Less Than Or Equal To
Check to see if one variable is less than or equal to the other.

Example:
PHP Code:
echo 1<=2; // Should output 1(True) echo 2<=1; // Should output 0(False) echo 1<=1; // Should output 1(True)

> Greater Than Or Equal To
Check to see if one variable is greater than or equal to the other.

Example:
PHP Code:
echo 1>=2; // Should output 0(False) echo 2>=1; // Should output 1(True) echo 1>=1; // Should output 1(True)



Logical Operators
These are used with comparison operators to create if statements. I will only cover the "code" version of these operators since I believe using "and, xor, and or" are crimes against humanity.

&& And
This is when you want something to be true AND something else to be true as well.

PHP Code:
echo TRUE && TRUE; // outputs 1(True) echo FALSE && TRUE; // outputs 0(False) echo FALSE && FALSE; // outputs 0(False)

|| Or
This is when you want something to be true OR something else to be true. Both can be True but to return true only one HAS to be true.

PHP Code:
echo TRUE || TRUE; // outputs 1(True) echo FALSE || TRUE; // outputs 1(True) echo FALSE || FALSE; // outputs 0(False)

!= Not
This is when you want something to not be true in order for it to return true.

A very weird idea, but think of it this way. If you wear a red shirt and someone says are you wearing a red shirt you say "Yes"(True). If they ask are you wearing a blue shirt you say "No"(False). If they ask are you NOT wear a blue shirt (shirt != blue) you say "yes"(True).

PHP Code:
echo 1 != 2; // outputs 1(True) echo 1 != 1; // outputs 0(False)




Assignment Operators
When assigning a value to a variable you use the = symbol. But along with the = symbol you can use all of the arithmetic operators to modify the value before sending it to a var. Instead of doing examples for each one I'll just do a generic example.

PHP Code:
$a += $b; // This is the same as $a = $a + $b $a -= $b; // This is the same as $a = $a - $b $a *= $b; // This is the same as $a = $a * $b $a /= $b; // This is the same as $a = $a / $b $a %= $b; // This is the same as $a = $a % $b


In Closing
I hope you find this useful and possibly learned something. There are some things I didn't include like BitWise, but in the future I might make a dedicated tutorial for that. But until then for people just starting with PHP (Or any C based language) this should be a decent resource for a good while.

Thank you
(This post was last modified: 07-21-2014, 08:40 PM by zimba.)
[Image: iQ3pcQu.png]
BTC Address: 1DCKgDaWcmc9dxBkhe9qrTQtrQpoFUzXdn

Reply





Messages In This Thread
PHP Operators - by h3r0 - 07-21-2014, 08:00 PM



Users browsing this thread: 1 Guest(s)