|
|
En PHP, les variables sont repr�sent�es par un signe
dollar "$" suivi du nom de la variable. Le nom est sensible �
la casse (ie : $x != $X).
Les noms de variables suivent les m�mes r�gles de nommage que
les autres entit�s PHP. Un nom de variable valide doit commencer par
une lettre ou un soulign� (_), suivi de lettres, chiffres ou
soulign�s. Exprim� sous la forme d'une expression
r�guli�re, cela donne :
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Note�:
Dans nos propos, une lettre peut �tre une des lettres minuscules
(a � z) ou majuscules (A � Z), et les caract�res
ASCII de 127 � 255 (0x7f-0xff).
En PHP 3, les variables sont toujours assign�es par valeur.
C'est-�-dire, lorsque vous assignez une expression �
une variable, la valeur de l'expression est recopi�e dans
la variable. Cela signifie, par exemple, qu'apr�s avoir
assign� la valeur d'une variable � une autre,
modifier l'une des variables n'aura pas d'effet sur l'autre. Pour plus
de d�tails sur ce genre d'assignation, reportez-vous �
Expressions.
PHP 4 permet aussi d'assigner les valeurs aux variables
par r�f�rence. Cela
signifie que la nouvelle variable ne fait que r�f�rencer
(en d'autres terme, "devient un alias de", ou encore "pointe sur") la
variable originale. Les modifications de la nouvelle variable
affecteront l'ancienne, et vice versa. Cela signifie aussi
qu'aucune copie n'est faite : l'assignation est donc beaucoup
plus rapide. Cela se fera notamment sentir dans des boucles,
ou lors d'assignation de grands objets (tableaux).
Pour assigner par r�f�rence, ajoutez simplement
un & (ET commercial) au d�but de la variable qui
est assign�e (la variable source). Dans l'exemple suivant,
"Mon nom est Pierre" s'affichera deux
fois :
Une chose importante � noter est que seules les variables
nomm�es peuvent �tre assign�es par r�f�rence.
User Contributed Notes Les variables |
|
[email protected]
23-Mar-2001 10: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 05: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 01: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 10: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 02: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 01: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 09: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 08: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 02: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 04: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 03: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 12: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 02: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 03: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".
|
|
|
| |