Library tutorials & articles

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.

Comments

  1. 28 May 2009 at 22:12

    It is not just talk though: Zend server is flourishing despite the recession - "the Q1 of 2009 has been our strongest quarter ever," says Suraski - and although PHP's enterprise market share is small compared to Java or .NET, it is growing. Read more on ITJOBLOG.

  2. 02 Jan 2007 at 06:39
    Here they show how to send valued in one file to another(both are in same folder)
    but i need to send values in one folder to another


  3. 04 Apr 2006 at 10:20

    Yes, I think you're correct, although you could easily have a line like this:

    $num = $_POST['num'];


    to get around that problem. Which I think is what's missing from this tutorial Wink [;)]





  4. 04 Apr 2006 at 09:47

    In the PHP file should it be "$_POST["num"]" ?

    instead of just $num?

  5. 21 Feb 2004 at 04:09
    hello

    I have in php a header which brings a save as box , but when file is down loaded it contains the 6 blank spaces at the start of of document .
    I have to avoid storing these blank spaces.Please help me to solve this problem.
  6. 28 Jan 2004 at 15:16

    You can also do string concatenation like

    Code:

    $h = "Hello";
    $w = "World";


    $message = "$h $w";


  7. 01 Jan 1999 at 00:00

    This thread is for discussions of An Introduction to PHP.

Leave a comment

Sign in or Join us (it's free).

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

Related discussion

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Dec 3

    The Auckland PHP December meetup

    Auckland, New Zealand

    Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.

We'd love to hear what you think! Submit ideas or give us feedback