![]()
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
SyntaxPHP 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:
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:
CommentsThere three types of comments in PHP:
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. ExpressionsAn 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:
OperatorsExpressions 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:
VariablesGeneral pointsThe 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:
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 variablesA 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:
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:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||