The manual Page
Version française
   
index | glossary | news | downloads | links ]
  PHP
PHP basics
syntax
controle structures
MySQL
Oracle
source code
 
news
glossary
links
downloads
 
credits
contact
 
 
search
 
last update
19/02/2003
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Mesurez votre audience


  Syntax and variables in PHP

Syntax

PHP is a quite simple programming language, and there is actually no real syntaxical constraints.

About names of variables and functions: variables names are case-sensitive, but it is not the case of function names. Si the section dealing with variables below.

PHP ignore whitespace (spaces, tabs and newlines) between tokens:

myfonction (parameter1,
            parameter2,
            parameter3);
is the same as
MyFonction(parameter1,parameter2, parameter3);

It is obviously not the case in for strings.

PHP statements follow each other and are separated (and terminated) by semicolons (";"). Only braces ("{" and "}") are not followed by semicolons:

statement 1;
statement 2;
while (test1) {
	statement 3;
	statement 4;
	if (test2) {
		...
	}
	...
	statement n;
}
...
statement m;

Comments

There three types of comments in PHP:

/* C style comments written on
   several lines */
// C++ style comments
# Bourne shell style comments

C++ and Bourne Shell style comments start with the "//" or "#" and end at the end of the current line. C style comments start at the "/*" and stop only at the first "*/", whether it is on the same line or not. This is really useful to test PHP code by commenting out several lines at the same time.

Expressions

An expression is the basic building block of a programming language. Anything with a value (explicitely quoted or not) can be thought of as an expression:

5
5+5
$a
$a == 5
sqrt(9)

Operators

Expressions are combined and manipulated with operators. Here is the complete list of operators available in PHP; they are showed along in descending order of precedence:

operator associativity
!, ~, ++, --, @, (cast) right
*, /, % left
+, -, . left
<<, >> left
<, <=, >=, > non-associative
==, != non-associative
& left
^ left
| left
&& left
|| left
? : (conditional operator) left
=, +=, -=, *=, /=, %=, ^=, .=, &=, |= left
And left
Xor left
Or left

Variables

General points

The first point to mention about variables is that their names are case-sensitive. This means that $A is different from $a. However, function names are not case-sensitive, both for built-in or user-defined functions.

All variable names start with a dollar sign ($). There is no limit for the length of a variable name. A variable name is made up of letters, digits and the underscore symbol (_). A name necesaarily starts with a letter or the underscore sign (_). No other characters are allowed as a starter for a name. Examples of variable names:

$a
$foo
$this_is_a_variable
$_this_too
$variable8

In PHP it is useless to explicitely declare variables at the beginning of a program. PHP automatically declares a variable the first time a value is assigned to it. Variables are also untyped: it is possible to assign a value of any type to a variable.

Dynamic variables

A dynamic variable is a variable whose name is dynamically set, for instance by using the value of another variable. Let's presume this:

$var = "foo";

Now, let's say we want to create a variable whose name would be the value of $var (this value may not be "foo"). So we do:

$$var = "bar";

That's all! We have just created a dynamic variable called $foo.

We may wonder what such variables may used for. A typical example of their possible use are associative arrays: with dynamic variables, it is very easy to create real variables whose names would be the keys of an associative array:

$table["mickael"] = "19";
$table["john"] = "33";

while (list($key, $value) = each($table)) {
	$$key = $value;
}

echo "Mickael is $mickael years old, and John is $john years old.";

This may be be very useful when we get the result of a SQL query with the mysql_fetch_array function (see database connectivity in PHP).

Nevertheless, there may be an ambiguity when using dynamic variables on several levels. For instance, how should PHP interprete this: $$var[1]? This could indeed be the value of $var[1] or the value of the cell number 1 of the array $$var... To sort this out, we use braces: ${$var}[1] and ${$var[1]}. Braces are also used to use dynamic variables in strings:

echo "Hello ${$me}";

printable format printable format



Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.