Databases Reference
In-Depth Information
The not-equals operator (
!=
) is the opposite of the equality operator (
==
). Similarly,
the
!==
operator is a type-sensitive not-equals and is the opposite of
===
.
There are two handy functions—
isset( )
and
empty( )
—for checking the state of a
variable:
// Has the variable been declared?
if (isset($x))
echo "x is set";
// Is the variable empty?
if (empty($x))
echo "x is empty";
A variable that doesn't exist (is not set) is always empty. However, a variable that's
empty may or may not exist, but if it does, it has a
NULL
value. A third function,
unset(
)
, can be used to destroy a variable:
$x = 5;
unset($x);
// Prints "not set"
if (isset($x))
echo $x;
else
echo "not set";
Loops
The standard loop constructs are
for
,
while
, and
do...while
. Let's look at different
ways to print out the integers 0 to 9 using these constructs.
The
for
statement has three parameters: an instruction to initialize any variables, a
condition that must be met for the loop to continue, and an instruction that is executed
after each round of the loop. To count from 0 to 9, we can start by setting the variable
$x
to 0 (
$x=0
). We increment
$x
one by one (
$x++
) as long as it is less than 10 (
$x<10
):
for ($x=0; $x<10; $x++)
echo "$x\n";
The
while
loop can take the same parameters, but here they are placed at different
locations in the code:
$x = 0;
while ($x < 10)
{
echo "$x\n";
$x++;
}