PHP Operators
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.
PHP Concatenation
We can display our variables in a meaningful fashion with the use of concatenation. Concatenation makes use of the period (.). Please see the example below which is an example of affixing a value to the end of the variable:
<?php $name = “Marc”; $name .= “Fraser”; ?>
Basically what ‘.=’ is doing is saying use the current value of $name and add “Fraser” to the end of it. If I were to now print $name, it would output “MarcFraser”.
We can also use concatenation to create readable sentences:
<?php $name = “ Marc Fraser”; $age = 16; print “We would like to welcome you,” . $name . “, age” . $age . “ to our website!”; ?>
This will output “We would like to welcome you, Marc Fraser, age 16 to our website!”. To do this, we close the string (close quotation mark) and add our period, which tells the server that we wish to begin parsing PHP again. When we want to stop parsing PHP we add a period and then begin our string again (quotation mark).
PHP Variables
Variables are important to PHP. A variable is a way of storing a value, such as text (“Hello”), numeric values (such as the integer value of 1) and also Boolean values (true and false). A variable can be re-used throughout your code instead of typing out the value over and over again. Variables will also not be new to you if you’ve had any experience of Algebra in Mathematics.
You define a variable in PHP like so:
<?php $name = “Marc Fraser”; $age = 16; ?>
The first things you should notice is the dollar sign ($). In PHP this signifies that the item is a variable, I can name a variable whatever I wish such as $a, $b, $c, as long as I do not omit it – otherwise syntax errors will occur. At this time, you should also note that variables are case sensitive, therefore $a is different in the eyes of PHP than $A.
Once our data is stored in a variable, we can perform operations to it, such as print, string manipulation and if the variable is an integer, calculations can be done. Below is how to print a variable:
<?php $name = “Marc Fraser”; $age = 16; print $name; print $age; ?>
The above will output “Marc Fraser16”.
Variable naming conventions:
- PHP Variables may only start with a letter or underscore (_).
- Spaces are not permitted in PHP variables and so underscores, or capitalization should be used – such as: $this_variable, $thisVariable.
- Variables may only comprise of alphanumeric characters.
Outputting HTML using PHP [+ Semicolons]
Semicolons
Semicolons are used extensively in PHP. The semicolon generally (there are exceptions) signifies the end of a PHP statement and should not be forgotten, otherwise unsightly errors will occur. An example will be shown in the Outputting HTML code using PHP section.
Outputting HTML code using PHP
Outputting HTML code using PHP is extremely easy. It can be done using the PHP Print command or indeed PHP Echo. The difference between the two commands can be read on the PHP FAQts site.
Below is how to output “This is my first output PHP string” in HTML paragraph and bold tags. This example will also show how the semicolon is used (read prior section, Semicolons).
<html> <head> <title>My first PHP outputs</title> </head> <body> <?php print “<p><strong>This is my first output PHP string</strong></p>”; // For consistency I will show the use of the echo command echo “<p><strong>This is my first output PHP string</strong></p>”; ?> </body> </html>
If you run the above code, you will see that you get the output:

- First PHP output.
The first line is of the print command and the second line is the output of the echo command. Please note that there is no difference between the two. Please also note the usage of quotation marks – these are used to tell the PHP code that we wish not to execute the contents contained within.
PHP Comments
(from Full Article)
We place comments in PHP code to make the code more maintainable and readable. These comments will not be viewable to the public, only to the person editing the PHP file. To comment in PHP we should prefix ‘//’ to our comment, for example:
We can also place comments over multiple lines by using ‘/*’ to begin and ‘*/’ to end our comment – as shown below.
Comments will be a lot more useful when more advance techniques in PHP are used.
A basic page in PHP is shown below, using our start, and end tags as well as comments. Please note that nothing will show, as comments are being used.