VARIABLES
Variables in PHP are quite different from compiled languages such as C and
Java. This is because their weakly typed nature, which in short means you
don’t need to declare variables before using them, you don’t need to declare
their type and, as a result, a variable can change the type of its value as much
as you want.
Variables in PHP are preceded with a $sign, and similar to most modern
languages, they can start with a letter (A-Za-z) or _(underscore) and can then
contain as many alphanumeric characters and underscores as you like.
Examples of legal variable names include
$count
$_Obj
$A123
Example of illegal variable names include
$123
$*ABC
As previously mentioned, you don’t need to declare variables or their
type before using them in PHP. The following code example uses variables:
$PI = 3.14;
$radius = 5;
$circumference = $PI * 2 * $radius; // Circumference = π* d
You can see that none of the variables are declared before they are used.
Also, the fact that $PIis a floating-point number, and $radius(an integer) is
not declared before they are initialized.
PHP does not support global variables like many other programming
languages (except for some special pre-defined variables, which we discuss
later). Variables are local to their scope, and if created in a function, they are
only available for the lifetime of the function. Variables that are created in
the main script (not within a function) aren’t global variables; you cannot sethem inside functions, but you can access them by using a special array
$GLOBALS[], using the variable’s name as the string offset. The previous
example can be rewritten the following way:
$PI = 3.14;
$radius = 5;
$circumference = $GLOBALS["PI"] * 2 * $GLOBALS["radius"];
➥// Circumference = π* d
You might have realized that even though all this code is in the main
scope (we didn’t make use of functions), you are still free to use $GLOBALS[],
although in this case, it gives you no advantage.
Indirect References to Variables
An extremely useful feature of PHP is that you can access variables by using
indirect references, or to put it simply, you can create and access variables by
name at runtime.
Consider the following example:
$name = "John";
$$name = "Registered user";
print $John;
This code results in the printing of "Registered user."
The bold line uses an additional $to access the variable with name speci-fied by the value of $name("John") and changing its value to "Registered user".
Therefore, a variable called $Johnis created.
You can use as many levels of indirections as you want by adding addi-tional $signs in front of a variable.
.Managing Variables
Three language constructs are used to manage variables. They enable you to
check if certain variables exist, remove variables, and check variables’ truth
values.
2.4.2.1isset() isset()determines whether a certain variable has already
been declared by PHP. It returns a boolean value trueif the variable has
already been set, and falseotherwise, or if the variable is set to the value NULL.
Consider the following script:
if (isset($first_name)) {
print '$first_name is set';
}
This code snippet checks whether the variable $first_nameis defined. If
$first_nameis defined, isset()returns true, which will display '$first_name is
set.' If it isn’t, no output is generatedthem inside functions, but you can access them by using a special array
$GLOBALS[], using the variable’s name as the string offset. The previous
example can be rewritten the following way:
$PI = 3.14;
$radius = 5;
$circumference = $GLOBALS["PI"] * 2 * $GLOBALS["radius"];
➥// Circumference = π* d
You might have realized that even though all this code is in the main
scope (we didn’t make use of functions), you are still free to use $GLOBALS[],
although in this case, it gives you no advantage.2.4.2.2unset() unset()“undeclares” a previously set variable, and frees
any memory that was used by it if no other variable references its value. A call
to isset() on a variable that has been unset()returns false.
For example:
$name = "John Doe";
unset($name);
if (isset($name)) {
print ’$name is set';
}
This example will not generate any output, because isset()returns
false.
unset()can also be used on array elements and object properties similar
to isset().2.4.2.3empty() empty() may be used to check if a variable has not been
declared or its value is false. This language construct is usually used to check
if a form variable has not been sent or does not contain data. When checking a
variable’s truth value, its value is first converted to a Boolean according to the
rules in the following section, and then it is checked for true/false.
For example:
if (empty($name)) {
print 'Error: Forgot to specify a value for $name';
}
This code prints an error message if $namedoesn’t contain a value that
evaluates to true.
Superglobals
As a general rule, PHP does not support global variables (variables that can
automatically be accessed from any scope). However, certain special internal
variables behave like global variables similar to other languages. These vari-ables are called superglobalsand are predefined by PHP for you to use. Some
examples of these superglobals are
☞$_GET[]. An array that includes all the GETvariables that PHP received
from the client browser.
☞$_POST[]. An array that includes all the POSTvariables that PHP received
from the client browser.
☞$_COOKIE[]. An array that includes all the cookies that PHP received from
the client browser.
☞$_ENV[]. An array with the environment variables.
☞$_SERVER[]. An array with the values of the web-server variables.
These superglobals and others are detailed in Chapter 5, “How to Write a
Web Application with PHP.” On a language level, it is important to know that
you can access these variables anywhere in your script whether function,
method, or global scope. You don’t have to use the $GLOBALS[]array, which
allows for accessing global variables without having to predeclare them or
using the deprecated globalskeyword.
Variables in PHP are quite different from compiled languages such as C and
Java. This is because their weakly typed nature, which in short means you
don’t need to declare variables before using them, you don’t need to declare
their type and, as a result, a variable can change the type of its value as much
as you want.
Variables in PHP are preceded with a $sign, and similar to most modern
languages, they can start with a letter (A-Za-z) or _(underscore) and can then
contain as many alphanumeric characters and underscores as you like.
Examples of legal variable names include
$count
$_Obj
$A123
Example of illegal variable names include
$123
$*ABC
As previously mentioned, you don’t need to declare variables or their
type before using them in PHP. The following code example uses variables:
$PI = 3.14;
$radius = 5;
$circumference = $PI * 2 * $radius; // Circumference = π* d
You can see that none of the variables are declared before they are used.
Also, the fact that $PIis a floating-point number, and $radius(an integer) is
not declared before they are initialized.
PHP does not support global variables like many other programming
languages (except for some special pre-defined variables, which we discuss
later). Variables are local to their scope, and if created in a function, they are
only available for the lifetime of the function. Variables that are created in
the main script (not within a function) aren’t global variables; you cannot sethem inside functions, but you can access them by using a special array
$GLOBALS[], using the variable’s name as the string offset. The previous
example can be rewritten the following way:
$PI = 3.14;
$radius = 5;
$circumference = $GLOBALS["PI"] * 2 * $GLOBALS["radius"];
➥// Circumference = π* d
You might have realized that even though all this code is in the main
scope (we didn’t make use of functions), you are still free to use $GLOBALS[],
although in this case, it gives you no advantage.
Indirect References to Variables
An extremely useful feature of PHP is that you can access variables by using
indirect references, or to put it simply, you can create and access variables by
name at runtime.
Consider the following example:
$name = "John";
$$name = "Registered user";
print $John;
This code results in the printing of "Registered user."
The bold line uses an additional $to access the variable with name speci-fied by the value of $name("John") and changing its value to "Registered user".
Therefore, a variable called $Johnis created.
You can use as many levels of indirections as you want by adding addi-tional $signs in front of a variable.
.Managing Variables
Three language constructs are used to manage variables. They enable you to
check if certain variables exist, remove variables, and check variables’ truth
values.
2.4.2.1isset() isset()determines whether a certain variable has already
been declared by PHP. It returns a boolean value trueif the variable has
already been set, and falseotherwise, or if the variable is set to the value NULL.
Consider the following script:
if (isset($first_name)) {
print '$first_name is set';
}
This code snippet checks whether the variable $first_nameis defined. If
$first_nameis defined, isset()returns true, which will display '$first_name is
set.' If it isn’t, no output is generatedthem inside functions, but you can access them by using a special array
$GLOBALS[], using the variable’s name as the string offset. The previous
example can be rewritten the following way:
$PI = 3.14;
$radius = 5;
$circumference = $GLOBALS["PI"] * 2 * $GLOBALS["radius"];
➥// Circumference = π* d
You might have realized that even though all this code is in the main
scope (we didn’t make use of functions), you are still free to use $GLOBALS[],
although in this case, it gives you no advantage.2.4.2.2unset() unset()“undeclares” a previously set variable, and frees
any memory that was used by it if no other variable references its value. A call
to isset() on a variable that has been unset()returns false.
For example:
$name = "John Doe";
unset($name);
if (isset($name)) {
print ’$name is set';
}
This example will not generate any output, because isset()returns
false.
unset()can also be used on array elements and object properties similar
to isset().2.4.2.3empty() empty() may be used to check if a variable has not been
declared or its value is false. This language construct is usually used to check
if a form variable has not been sent or does not contain data. When checking a
variable’s truth value, its value is first converted to a Boolean according to the
rules in the following section, and then it is checked for true/false.
For example:
if (empty($name)) {
print 'Error: Forgot to specify a value for $name';
}
This code prints an error message if $namedoesn’t contain a value that
evaluates to true.
Superglobals
As a general rule, PHP does not support global variables (variables that can
automatically be accessed from any scope). However, certain special internal
variables behave like global variables similar to other languages. These vari-ables are called superglobalsand are predefined by PHP for you to use. Some
examples of these superglobals are
☞$_GET[]. An array that includes all the GETvariables that PHP received
from the client browser.
☞$_POST[]. An array that includes all the POSTvariables that PHP received
from the client browser.
☞$_COOKIE[]. An array that includes all the cookies that PHP received from
the client browser.
☞$_ENV[]. An array with the environment variables.
☞$_SERVER[]. An array with the values of the web-server variables.
These superglobals and others are detailed in Chapter 5, “How to Write a
Web Application with PHP.” On a language level, it is important to know that
you can access these variables anywhere in your script whether function,
method, or global scope. You don’t have to use the $GLOBALS[]array, which
allows for accessing global variables without having to predeclare them or
using the deprecated globalskeyword.
No comments:
Post a Comment