Marc Fraser | Web Design and Development Blog A Web Design and Development Blog by Marc Fraser. Marc Fraser is a student, situated in Central Scotland.

10Oct/090

PHP Operators

(from Full Article)

Here are most types of PHP Operators: Assignment Operators, Arithmetic Operators, Comparison Operators, and String Operators.

Assignment Operators

We have already met this type. This is where we set a variable equal to something or make a variable equal to another variable.

Arithmetic Operators

English Operator Example
Addition + 1+1
Subtraction - 5-3
Multiplication * 5*2
Division / 10/2

An example of the use of Arithmetic operators is shown below:

<?php

$add = 1+1;

$subtract = 5-3;

$multiply = 5*2;

$divide = 10/2;

print “Addition Answer: ” . $add . “<br>”;

print “Subtraction Answer: ” . $subtract. “<br>”;

print “Multiplication Answer: ” . $multiply. “<br>”;

print “Addition Answer: ” . $divide. “<br>”;

?>

The output of the above code is:

Addition Answer: 2
Subtraction Answer: 2
Multiplication Answer: 10
Addition Answer: 5

This showing that the server is carrying out the calculations, using our arithmetic operators. As well as the above, you could also use these operators in conjunction with each other like so:

<?php

$quantity = 4;

$value = 5;

$total = $quantity * $value;

print $total;

?>

The output from the above is 20.

Comparison Operators

Comparison Operators are used to check the relationship between variables/values. These are used inside statements (such as if statements) to check whether the result is true or false. Listed are some PHP comparison operators:

We will assume, for this example, that $a = 2 and $b = 3.

English Operator Example Result
Equal to == $a == $b False
Not equal to != $a != $b True
Less than < $a < $b True
Greater than > $a > $b False
Less than or equal to <= $a <= $b True
Greater than or equal to >= $a >= $b False

Please note the two equal signs in the equal to example – this is a common beginners’ mistake.

We will go into further examples of Comparison Operators in the next section on PHP If Statements.

String Operators

We have already met String Operators, please read prior section named Concatenation.

6Oct/091

Introduction to PHP, the basics. [Full Article]

Introductions

Article Scope

This article aims to express the basics, which a beginner should consider when learning PHP. It will explain how to create a very basic PHP page that will contain fundamental commands to PHP beginners.

For this article, I will be using XAMPP to execute my PHP code – this is free to download and is great, especially if you do not have a web server with PHP installed.

   

Bad Behavior has blocked 66 access attempts in the last 7 days.