|
|
Capitolo 8. VariablesBasics
Variables in PHP are represented by a dollar sign followed by the
name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A
valid variable name starts with a letter or underscore, followed
by any number of letters, numbers, or underscores. As a regular
expression, it would be expressed thus:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Nota:
For our purposes here, a letter is a-z, A-Z, and the ASCII
characters from 127 through 255 (0x7f-0xff).
In PHP 3, variables are always assigned by value. That is to say,
when you assign an expression to a variable, the entire value of
the original expression is copied into the destination
variable. This means, for instance, that after assigning one
variable's value to another, changing one of those variables will
have no effect on the other. For more information on this kind of
assignment, see the chapter on Expressions.
PHP 4 offers another way to assign values to variables:
assign by reference.
This means that the new variable simply references (in other words,
"becomes an alias for" or "points to") the original variable.
Changes to the new variable affect the original, and vice versa.
This also means that no copying is performed; thus, the assignment
happens more quickly. However, any speedup will likely be noticed
only in tight loops or when assigning large
arrays or
objects.
To assign by reference, simply prepend an ampersand (&) to the
beginning of the variable which is being assigned (the source
variable). For instance, the following code snippet outputs 'My
name is Bob' twice:
One important thing to note is that only named variables may be
assigned by reference.
User Contributed Notes Variables |
|
[email protected]
23-Mar-2001 11:31 |
|
If, for some reason you need to declare
a dynamic variable global, say
$usertype1, $usertype2 etc...
do it like this:
$i=1;
$ready=#number of
generated
variables#
while ($i <
$ready) {
$var="usertype$i";
global
$$var;
###do the action with
the var, for
example:##
echo $$var;
$i++;
}
|
|
[email protected]
24-Apr-2001 06:31 |
|
I had trouble digging this out, and searches for various terms (memory
model, garbage collection, &c) were not productive.
PHP4
uses reference counting for garbage collection. Details about this system
can be found here:
|
|
[email protected]
08-Dec-2001 02:12 |
|
a great way to pass variables from page to page...
select:
How PHP/FI handles GET and POST method data
ie:
/cgi-bin/php.cgi/[email protected]&var=value
The
relevant components of the PHP symbol table will be:
$argc
= 4
$argv[0] = abc
$argv[1] = def
$argv[2] = [email protected]&var=value
$EMAIL_ADDR = [email protected]
$var = value
|
|
[email protected]
13-Jan-2002 11:21 |
|
Here is a tip to save the bandwidth rainforest. There is no reason to
send meta tags to Mozilla compatible
browsers. <?php if(!strstr($HTTP_USER_AGENT,
"Mozilla")) { ?> <meta name="author"
content="I, me and myself" /> <? } ?>
|
|
[email protected]
01-Mar-2002 03:06 |
|
Actually, you can use other chars (including spaces) in the variable name,
if you are using variable variables. E.g., the following works for me (in
PHP 4.1.1):
$x="blah
blah-blah"; $$x="value"; echo "var==" .
$$x; echo "var==" . ${"blah blah-blah"};
Its
output is: var==value var==value
You can use it also in
object variables (properties).
|
|
[email protected]
18-Mar-2002 02:37 |
|
'Here document' syntax is explained under the 'echo' function. Somehow I
always try to find it here... In short:
echo
<<<EOD blablabla tralala EOD;
|
|
[email protected]
19-Mar-2002 10:31 |
|
In the example listed above:
<?php $foo = 'Bob';
// Assign the value 'Bob' to $foo $bar = &$foo; //
Reference $foo via $bar. $bar = "My name is $bar"; // Alter
$bar... echo $foo; // $foo is altered too. echo
$bar; ?> the documentation above it is somewhat
ambiguous: $bar = &$foo means that $foo becomes the reference for
$bar, thus $foo = "Bob". As I read it, $bar should've become
the reference for $foo, took me a bit to turn it around. Just some
clarification.
|
|
[email protected]
06-Apr-2002 09:16 |
|
Another way to assign a variable a large amount text without having to
worry about quotes getting in the way is like so:
$aVariable =
<<<END "HEY!" END;
print
"$aVariable";
output: "HEY!"
Where as
this is will produce an error: $aVariable =
""HEY!"";
|
|
[email protected]
14-Apr-2002 03:21 |
|
<<------------------< Env. Variables
>------------------> If you are looking for an explanation of
environment variables, go to
If
you wanted to set environment variables, look into the putenv()
function.
|
|
[email protected]
25-May-2002 05:53 |
|
How does one send a variable from a particular page, over a secure network
that uses SSL in order for it to be processed by the web server?
|
|
[email protected]
03-Jul-2002 04:05 |
|
Can someone tell me why this page (www.gbasquare.host.sk/news/news.php)
doesn't work??? Yesterday it worked but today the server has upgraded the
php script and the page doesn't work. Thank you
|
|
stlawson AT sbcglobal DOT net
06-Jul-2002 01:47 |
|
In 'ghent's comment on the 'above example' I think he confuses the
confusion ;)
Here is the example referred
to:
<?php $foo = 'Bob'; // Assign the value
'Bob' to $foo $bar = &$foo; // Reference $foo via
$bar. $bar = "My name is $bar"; // Alter $bar... echo
$bar; echo $foo; // $foo is altered
too. ?>
�Reference $foo via $bar�/�Alter $bar�� IS correct,
but it is a little obscure. Here�s what it means:
$bar is assigned
a reference to $foo, thus $bar �references� or points to $foo which
contains the string �Bob�. Essentially what is happening here is �Bob� is
stored in memory at a particular address. &$foo returns that address
[where �Bob� is stored]. That address is assigned to $bar. So, in the
string �My name is $bar�, $bar �uses� the address it contains to find
�Bob� (which is �in� $foo) and thus the string becomes �My name is Bob�.
When the string is assigned to $bar, because $bar refers to $foo, it gets
assigned to the same address location that �Bob� is stored at, thus �Bob�
is overwritten by �My name is Bob�. The trick here is to realize that
$bar behaves as if it is $foo, so when something is assigned to $bar (the
alias of $foo), it�s as if it was being assigned to $foo!
After the
above script is run, the output will look like this:
My name is
BobMy name is Bob
e.g. both $foo and $bar print the same
thing.
In �C++� the same code snippit would look like
this:
foo = �Bob� ; bar = &foo ; bar = �My name is � +
*bar ;
Notice that in C/C++ it is necessary to manually dereference
the pointer (*bar) � PHP does this automagically.
BTW: You might
think that �echo $bar;� would display the address of $foo � not so! More
PHP automagic ;)
|
|
[email protected]
13-Jul-2002 03:24 |
|
Can I call array items multiply as Perl?
It
seems: $x=array(1,2,3,4,5,6,7,8); $y=$x[3,1,5,7,-1];
|
|
[email protected]
18-Aug-2002 04:01 |
|
If you use global variables within a function, take note that changing the
variable keyword from "global", somewhere down the line, to
"return", that that variable can either be one or the other. It
can't be both "returned" and "global".
So, if
within a function you have on line x: global $var;
and then on
line x+1: return $var;
that $var becomes no longer global, and
instead just and only "returned".
|
|
|
| |