Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 74,740 times

Contents

Related Categories

An Introduction to PHP - Variables & Operators

gez

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.

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

Comments

  • Re: [3703] An Introduction to PHP

    Posted by radha_telanakula on 02 Jan 2007

    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

  • Re: [3703] An Introduction to PHP

    Posted by pjlewis on 04 Apr 2006

    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 tutor...

  • Re: [3703] An Introduction to PHP

    Posted by keepbalance on 04 Apr 2006

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


    instead of just $num?

  • file download

    Posted by narayan on 21 Feb 2004

    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.Plea...

  • String concatenation

    Posted by jeen on 28 Jan 2004

    You can also do string concatenation like
    [code]
    $h = "Hello";
    $w = "World";

    $message = "$h $w";

    [/code]