Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1
The following are some simple assignments:
$num = 20; # integer
$price = 14.95; # floating-point
$size = 'large'; # string
$myarray[0] = "red"; # scalar array, first element (0)
$myarray["qty"] = 6; # associative array, element "qty"
Note that either single (') or double quotes (") can be used for string values, but yield different results.
With double quotes, any variable contained within will be evaluated. With single quotes, they will not.
Thus,
$var = "red $price";
echo $var;

produces an output as follows:
red 14.95

However,
$var = 'green $price';
echo $var;

produces a more literal output:
green $price
If you want to use objects, use the new statement to create an instance of an object and relate it to a
variable, as shown in the following:
class myobj {
function do_myobj ($p) {
echo "Doing myobj with parameter $p.";
}
}
$var = new myobj;
$var->do_myobj(5);

This produces output:
Doing myobj with parameter 5.

You are advised to study a more thorough text on object-oriented techniques if you are not familiar with
them already.
PHP variables can also be equated to Boolean types (TRUE and FALSE). If a variable is undefined, null,
or zero, it equates to FALSE, and if it contains some value, it will equate to TRUE. Comparing a variable
with TRUE or FALSE is useful in conditional expressions and loops, as you will see later today in the
"Control Structures" sections.

Variables


Variables of all types are preceded with a dollar sign ($) and are case-sensitive.
By default, variables are available to the script in which they are used and in any scripts that are
included in that one (you'll look at incorporating one script into another script soon, using the INCLUDE
statement). In this sense, they are global.


However, when you use user-defined functions, variables are local to that function and are not available
outside it by default.

You've already seen some examples of manipulating user-defined variables, but there are also a large
number of pre-defined variables.
The easiest way to view the pre-defined variable for your system is to run phpinfo:
phpinfo();
Free download pdf