PHP OPERATORS

PHP OPERATORS
You’ve just learned how variables and constants work. Now it’s time for operators.

With use of operators you can influence variables and their values.

Just see the full list here.

ARITHMETIC OPERATORS

The first category of PHP operators that I wanted to introduce here are arithmetic operators. Their use is very intuitive and the operation result almost obvious. It all about addition, subtraction, multiplication and division of numbers. In addition to this group the operator returns the remainder of the division.

The list looks as follows:
“+” returns the sum of two numbers or strings,
“-” returns the difference,
“*” returns the multiplication,
“/” returns the division,
“%” returns the remainder of division.

COMPARISONS

The second category is comparison operators. PHP allows us to check whether two variables are equal or (if they’re not, which one is greater and which is smaller.

The comparison operators are:
“==” checks if two variables are equal in value,
“! =” checks if the variables are different in value,
“===” checks if the variables are identical,
“! ==” checks if the variables are not identical,
“>” checks if the variable on the left is greater than the variable on the right side,
“<” checks if the variable on the right is larger than the variable on the left side,
“> =” checks if the variable on the left is greater than or equal to variable on the right,
“<=” checks if the variable on the right is greater than or equal to variable on the left.

LOGICAL OPERATORS

The third category is logical operators. They are mainly used combined with conditional instructions that you will learn in next lessons. Their main purpose is to check the condition whether it’s true or false. There are three to choose from:

“!” negation operator (logical NOT),
“&&” conjunction operator (logical AND – and),
“||” alternative operator (logical OR – or).

INCREMENTATION

This category is pretty close to the arithmetic operators. It’s main purpose is incrementation and decrementation. It’s for quick increasing and decreasing the value of our variable by 1. They are divided into:

“$i++” post-incrementation – increases the value of the variable by 1,
“++$i” pre-incrementation – increases the value of the variable by 1,
“$i–” post-decrementation – decreases the value of the variable by 1,
“–$i” pre-decrementation – decreases the value of the variable by 1.
The difference between pre- and post-incrementation lies in the moment when the value increases.

Pre-increment increases the value before executing the command, while post-incrementation increases the value of the variable after executing the command. With decrementation it’s the same story, except that the value is reduced.

Another way to change the value of a variable is to use the assignment operator combined with an additional arithmetic operator.

If you want to increase the value by a certain number, instead of writing $i = $i + 7, you can immediately increase $i by 7 by using combined: $i += 7.

Below are other possibilities of assigning the changed value immediately to variables:

“$i += 5” increases in value by 5,
“$i -= 5” decreases in value by 5,
“$i *= 5” assigns a value 5 times greater,
“$i /= 5” assigns a value 5 times smaller,
“$i %= 5” assigns the value of the remainder from dividing the variable by 5.

The operator worth mentioning is the “@” error handler, which hides the error message from executing code. For now, however, it’s not worth your time. We’ll get to that later in the course.

STRING OPERATOR .

Yep, that’s now a mistake at the end of the heading.

The operator is ‘a simple dot’. We can use it the same way as plus with numbers. However, you use it with string variables.

It simply combines two strings into one.

For example:

<?php
$firstName = "Marcin";
$lastName = "Wesel";
$fullName = $firstName . " " . $lastName;
As the result of running the script above, the variable $fullN

Comments

Popular posts from this blog

FIRST LINE OF CODE IN PHP

VARIABLES AND CONSTANTS

SIMPLE PHP