|
|
Kapitola 8. Prom�nn�Z�klady
Prom�nn� jsou v PHP reprezentov�ny znakem dolaru, n�sledovan�m n�zvem
p��slu�n� prom�nn�. V n�zvech prom�nn�ch se rozli�uje velikost p�smen.
N�zvy prom�nn�ch jsou pod��zeny stejn�m pravidl�m jako jin� n�v�t�
v PHP. Platn� n�zev prom�nn� za��n� p�smenem nebo podtr��tkem,
n�sledovan�m libovoln�m po�tem p�smen, ��slic nebo potr��tek. Jako
regul�rn� v�raz to lze zapsat takto:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Pozn�mka:
Pro na�e ��ely zde budeme za p�smena pova�ovat znaky a-z, A-Z
a ASCII znaky od 127 do 255 (0x7f-0xff).
V PHP 3 maj� prom�nn� v�dy p�i�azenu hodnotu. To znamen�, �e kdy�
p�i�ad�te v�raz do prom�nn�, cel� hodnota p�vodn�ho v�razu se
zkop�ruje do c�lov� prom�nn�. Tedy, nap��klad, po p�i�azen� hodnoty
jedn� prom�nn� do druh�, zm�na jedn� z nich se na druh� neprojev�.
V�ce informac� o tomto zp�sobu p�i�azen� viz
V�razy.
PHP nab�z� jin� zp�sob p�i�azen� hodnot prom�nn�m:
p�i�azen� odkazu. To znamen�, �e nov� prom�nn�
jednodu�e odkazuje (jin�mi slovy, "st�v� se aliasem" nebo "ukazuje na")
na p�vodn� prom�nnou. Zm�ny na nov� prom�nn� se projev� na t� p�vodn�
a naopak. To tak� znamen�, �e se nic nekop�ruje; proto je p�i�azen�
rychlej��. Av�ak toto zrychlen� bude zjistiteln� pouze v t�sn�ch cyklech
nebo p�i p�i�azov�n� velk�ch pol� �i objekt�.
Pro p�i�azen� odkazu sta�� jednodu�e p�ed prom�nnou, kter� bude
p�i�azov�na (zdrojov� prom�nn�), p�ed�adit ampersand (&). Nap��klad
n�sleduj�c� kus k�du vyp�e dvakr�t 'Jmenuji se Bob':
Jednou z d�le�it�ch v�c�, kter� je t�eba si uv�domit, je to, �e p�es
odkazy lze p�i�azovat pouze pojmenovan� prom�nn�.
User Contributed Notes Prom�nn� |
|
risa4 at law dot leidenuniv dot nl
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++;
}
|
|
dwood at templar dot com
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:
|
|
webmaster at webpagebydesign dot com
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
|
|
engard at all dot NOSPAM dot hu
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).
|
|
cd579 at hotmail dot com
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!"";
|
|
isaac at chexbox dot com
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.
|
|
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 ;)
|
|
r dot yaker at wiredfusion dot net
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".
|
|
ct at filanet dot dk
24-Oct-2002 11:32 |
|
It is not correct that '�' is ASCII 228. In fact, ASCII only defines
characters in the range 0-127.
But '�' is character 228 in the ISO
8859-1 (also known as Latin-1) character set.
|
|
peka yashuu de
21-Nov-2002 08:56 |
|
`that $var becomes no longer global, and instead just and only
"returned".`
The reason for this would be, that you
return the value of the variable, not the variable itself and a value
can`t be global, can it?
|
|
someone at somewhere dot com
23-Dec-2002 06:50 |
|
The comment from stlawson AT sbcglobal DOT net about the C/C++ isn't quite
correct:
You confuse the pointer notation of C/C++ with the
reference notation only availible in C++...
True, dreferencing has
to be manually done if you use the pointer notation, but not if you use
the reference notation...
|
|
gabriel at bumpt dot net
13-Jan-2003 04:26 |
|
I know this is not a C++ newsgroup, but I just wanted to get things right,
after the comment of "someone at somewhere dot com".
The
C-like piece of code provided by stlawson DOES use a pointer, and NOT a
C++ reference. "someone at somewhere dot com" got confused
because type declaration was obviously omitted for clarity reasons. A
reference in C++ is almost a type qualifier: a variable is of type
"reference to objects of class MyClass". Therefore, one cannot
assign &myObj to a variable declared as a "reference to objects
of class MyClass". &myObj really identifies a pointer to myObj,
and dereference (using * or ->) is requiered.
|
|
unleaded at nospam dot unleadedonline dot net
15-Jan-2003 01:37 |
|
References are great if you want to point to a variable which you don't
quite know the value yet ;)
eg:
$error_msg =
&$messages['login_error']; // Create a
reference
$messages['login_error'] = 'test'; // Then later on set
the referenced value
echo $error_msg; // echo the 'referenced
value'
The output will be:
test
|
|
phValue at example dot com
04-Mar-2003 01:03 |
|
php shows up a var-name conflict if you use the same name for scalar values
and arrays:
first we want to use a scalar value: $scalarValue =
5; scalarValue holds: 5
But than, you want to use the same name
for an array, too, which seems ok at first glimps: (sometimes, its
hard to find new names for the same purpose..)
$scalarValue =
array(); array_push($scalarValue,3);
scalarValue[0] holds:
3
now, all seems ok, since the array assignment went well.. but,
where is the scalarValue gone, if we ask for it? if you echo for it,
you ask for the array: scalarValue holds: $Array The scalar value
has no longer a reference, since it shares the same name with an
array.. such a bug can give you some hard time to dig for!
|
|
Tom Andrews at cfl dot rr dot com
03-Apr-2003 08:02 |
|
charecter 228 (�) is not in the ascii 0-127 but is in extended ascii, which
is 128-255
|
|
pekka at SPAMphotography-on-theSPAM dot net
29-Apr-2003 07:55 |
|
For us newbies: During developement time you often want to display only
some variables on page for debugging. With this function you can you can
save some typing time.
function printvars ($name,$hidden) { if
($hidden == "1") print "<!-- "; foreach ($name
as $k) { global ${$k}; print " <b>$$k:</b>
" . ${$k}; } if ($hidden == "1") print "
-->"; return; }
Use it by first creating an array of
variable names (without $'s) you want to display:
$track =
array("name","address","car","hobby","favbeer");
and
then run the function (use 1 as second parameter if you want to hide the
output into html source code):
printvars ($track,0);
|
|
daevid at daevid dot com
01-May-2003 12:47 |
|
While this may or may not be obvious, to use references with a function, do
it like this...
function timeStampNotes($notes) { if ($notes
!= "") $notes = "\n\n[".date("m/d/Y
h:iA")."]"\n".$notes; }
timeStampNotes(&$contact_notes);
notice
that you use the & in the passing, not in the function
itself...
If your notes are getting to be large textareas (like
with a database), this may be faster than sending it the normal way.
|
|
|
| |