Breadcrumbs

PHP Variables and Operators

Variables are spaces that allow you to store a small piece of data. Operators allow you to compare and do mathematically functions to the variables such as adding and subtracting them.

PHP variables are composed of the following format:

Code: PHP

<?php 
$variablename 
variablevalue
?>

All PHP variables start with the money sign ($). Followed by the name you wish to give the variable. This name will be used to identify the variable throughout the rest of the page. The variable value is the value that the variable holds. This value can be characters and numbers. Here are some examples of proper variable declaration and assignment of value.

Code: PHP

<?php 
$variable1 
"Hello World";
$variable2 "Have a nice day!";
$variable3 "135"
?>

As you see the creation of variable isn't very hard and provides great new fields to expand to in your PHP programming. Also demonstrated above the type of data stored within the variable doesn't matter.

Operators

Operators are what allow you to compare and manipulate the data held within variables. There are arithmetic, assignment, logical, and comparison operators. All handle with variables.

Arithmetic Operators

There are 7 arithmetic operators, addition (+), subtraction (-), multiplication (*), division (/), modulus (%), increment (++), and decrement (--). Most of these you know except the last three, modulus returns the remainder of a division problem, increment adds 1, and decrement subtracts 1.

Using arithmetic operators is easy, below is a simple example of using the addition operator:

Code: PHP

<?php 
$x 
4;
$y2;
$z $x $y
?>

There are a number of ways that you could do that such as simplifying it down to this:

Code: PHP

<?php 
$x 
4;
$y $x 
?>

This eliminates one variable that you don't need and if you simplify your code all over then you reduce server stress.

Assignment Operators

These operators are used in assigning variables their data. One that you probably already know is the assignment operator (=). This one you will use all over, but there are others seen below.

Operator Example Same As
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

Comparison Operators

These operators are used to compare variables and often used in conditional statements. There are 7 comparison operators, is equal to (==), is not equal (!=), less than (<), greater than (>), less than or equals to (<=), and greater than or equals to (>=). These all return either true or false. If the statement there is true then it returns true and vice versa. Here is an example of an if statement using comparison operators:

Code: PHP

<?php 
$x 
4;
$y 5;

if(
$x != $y) {
    echo 
"x doesn't equal y";

?>

The if statement above says that as long as $x is not the same value as $y then to do the next statements. When you get into switch and conditional statements comparison operators will be useful information to know.

Logical Operators

Logical operators go hand in hand with comparison operators and are used to continue the comparison statement. There are three logical operators, and (&&), not (!), and or (||). Here is an example of using the AND operator:

Code: PHP

<?php
$x 
4;
$y 5;

if (
$x != && $y == 5) {
    echo 
"x isn't 4 and y is 5";
}
?>

The logical operator makes it to say if $x doesn't equal 4 and $y equals 5 to execute the following commands. Although one of these is true the other isn't so it returns false and the if statement doesn't continue.

We could change around the statement above to if at least one of the comparisons returns true using the OR operator (||).

Code: PHP

<?php
$x 
4;
$y 5;

if (
$x != || $y == 5) {
    echo 
"Either x isn't 4 or y equals 5";
}
?>

Now the if statement will execute as one of the two comparisons are true. In this code the if statement reads as if $x doesn't equal 4 or $y equals 5 execute the following commands.

The NOT operator can be used like this:

Code: PHP

<?php
$x 
4;
$y 5;

if (!(
$x $y)) {
    echo 
"x doesn't equal y";
}
?>

Now you can always use the != operator to replace this, but you can also use the NOT operator along with functions such as isset():

Code: PHP

<?php
$x 
4;
$y 5;

if (!isset(
$y)) {
    echo 
"Y isn't there";
}
?>

The isset() function returns true if the variable exist, using the NOT operator there makes it so that it returns true if the variable $y isn't there.

Now you have a general knowledge of PHP variables and operators to allow you to take the data from a user and alter it, a great expansion for any PHP page.