Tuesday, 28 February 2017

Variables

A variable is a special container that you can define to "hold" a value. A variable
consists of a name that you can choose, preceded by a dollar ($) sign. The variable
name can include letters, numbers, and the underscore character (_). Variable
names cannot include spaces or characters that are not alphanumeric. The following
code defines some legal variables:
$a;
$a_longish_variable_name;
$2453;
$sleepyZZZZ
Remember that a semicolon (;) is used to end a PHP statement. The semicolons in
the previous fragment of code are not part of the variable names.
NEW
TERM
A variable is a holder for a type of data. It can hold numbers, strings of
characters, objects, arrays, or booleans. The contents of a variable can
be changed at any time.
As you can see, you have plenty of choices about naming, although it is unusual to
see a variable name that consists exclusively of numbers. To declare a variable, you
need only to include it in your script. You usually declare a variable and assign a
value to it in the same statement.
$num1 = 8;
$num2 = 23;
The preceding lines declare two variables, using the assignment operator (=) to
give them values. You will learn about assignment in more detail in the Operators
and Expressions section later in the hour. After you give your variables values, you
can treat them exactly as if they were the values themselves. In other words
print $num1;
is equivalent to
print 8;
as long as $num1 contains 8.
Dynamic Variables
As you know, you create a variable with a dollar sign followed by a variable name.
Unusually, the variable name can itself be stored in a variable. So, when assigning
a value to a variable
$user = "bob";
is equivalent to
$holder="user";
$$holder = "bob";
The $holder variable contains the string "user", so you can think of $$holder as a
dollar sign followed by the value of $holder. PHP interprets this as $user.
Note You can use a string constant to define a dynamic variable instead of a
variable. To do so, you must wrap the string you want to use for the variable
name in braces:
${"user"} = "bob";
This might not seem useful at first glance. However, by using the
concatenation operator and a loop (see Hour 5, "Going with the Flow"), you
can use this technique to create tens of variables dynamically.
When accessing a dynamic variable, the syntax is exactly the same:
$user ="bob";
print $user;
is equivalent to
$user ="bob";
$holder="user";
print $$holder;
If you want to print a dynamic variable within a string, however, you need to give
the interpreter some help. The following print statement:
$user="bob";
$holder="user";
print "$$holder";
does not print "bob" to the browser as you might expect. Instead it prints the strings
"$" and "user" together to make "$user". When you place a variable within
quotation marks, PHP helpfully inserts its value. In this case, PHP replaces $holder
with the string "user". The first dollar sign is left in place. To make it clear to PHP
that a variable within a string is part of a dynamic variable, you must wrap it in
braces. The print statement in the following fragment:
$user="bob";
$holder="user";
print "${$holder}";
now prints "bob", which is the value contained in $user.
Listing 4.1 brings some of the previous code fragments together into a single script
using a string stored in a variable to initialize and access a variable called $user.
Example:
<html>
 <head>
 <title> Dynamically setting and accessing variables</title>
 </head>
 <body>
 <?php
 $holder = "user";
 $$holder = "bob";
 // could have been:
 // $user = "bob";
 // ${"user"} = "bob";
 print "$user<br>"; // prints "bob"
 print $$holder; // prints "bob"
 print "<br>";
 print "${$holder}<br>"; // prints "bob"
print "${'user'}<br>"; // prints "bob"
 ?>
 </body>
 </html>

output:-
bob
bob
bob
bob

No comments:

Post a Comment