An Introduction to PHP

Variables & Operators

PHP supports the basic data types of strings, integers, double precision floating point numbers, etc, but is a weakly typed language. This means that variables are not tied to a specific data type, and may take any of the data types. Variables are not declared in PHP, but must be prefixed with a $ sign.

String concatenation is achieved in PHP using the dot (.) operator. The following example uses a variable called anyDataType. The data type is given a string literal, and an integer values. The output is displayed using the dot operator to concatenate a break to each line printed.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Using Variables</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
  <?php
    // Use several data types with one variable
    $anyDataType = "Hello world";
    print $anyDataType . "<br>";
    $anyDataType = 64;
    print $anyDataType . "<br>";
    $anyDataType += 1900;
    print $anyDataType . "<br>";
  ?>
</p>
</body>
</html>

Operators

Arithmetic Operators

Operator Description
+ Unary plus, or addition.
- Unary minus or subtraction.
* Multiplication.
/ Division.
% Modulus (remainder from a divide operation).
++ Increment (pre-increment if placed before the variable, post-increment if placed after).
-- Decrement (pre-decrement if placed before the variable, post-decrement if placed after).

Arithmetic Operator Examples

// Caluclate the sum of a and b
$total = $a + $b;
// Calculate the difference between a and b
$difference = $a - $b;
// Calculate the product of a and b
$product = $a * $b;
// Calculate the quotient of a and b
$quotient = $a / $b;
// Calculate the remainder when a is divided by b
$remainder = $a % $b;
// Increment the value of a
$a++;
// Decrement the value of b
$b--;
// The pre-inrecrement version
$num = ++$a;
// The pre-decrement version
$num = --$b;

Self Assignment

If an assignment is made that requires the value of the variable being assigned to, PHP offers a convenient shortcut by placing the operator immediately before the assignment operator.

$total += $a; // Equivalent to $total = $total + $a;
$difference -= $a; // Equivalent to $difference = $difference - $a;
$product *= $a; // Equivalent to $product = $product * $a;
$quotient /= $a; // Equivalent to $quotient = $quotient / $a;
$remainder %= $a; // Equivalent to $remainder = $remainder % $a;

Relational Operators

Relational operators (sometimes called comparison operators) are used to compare values in an expression. The return value will be either true or false.

PHP Relational Operators

Operator Description
< Less than (eg. $x < 10).
<= Less than or equal (eg. $x <= 10).
== Equivalence (eg. $x == 10).
> Greater than (eg. $x > 10).
>= Greater than or equal (eg. $x >= 10).
!= Not equivalent (eg. $x != 10).

Logical Operators

Logical operators allow you to combine the results of multiple expressions to return a single value that evaluates to either true or false.

PHP Logical Operators

Operator Description
! Logical Not. For example, (!$x) will evaluate to True if $x is zero.
&& Logical And. For example, ($x > 4 && $y <= 10) will evaluate to True is $x is greater than four and $y is less than or equal to ten.
|| Logical Or. For example, ($x > 10 || $y > 10) will evaluate to True is either $x or $y are greater than ten.

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup