PHP: V�ltoz�k - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousB�v�szked�s a t�pusokkalEl�re defini�lt v�ltoz�knext
Last updated: Fri, 30 Aug 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

Fejezet 8. V�ltoz�k

Alapok

PHP-ban a v�ltoz�kat egy doll�rjel ut�ni v�ltoz�n�v jel�li. A v�ltoz�nevek �rz�kenyek kis- �s nagybet�k k�l�nb�z�s�g�re.

A v�ltoz�nevekre a PHP m�s jelz�ivel azonos szab�lyok vonatkoznak. Egy �rv�nyes v�ltoz�n�v bet�vel vagy al�h�z�ssal kezd�dik, amit tetsz�leges sz�m� bet�, sz�m vagy al�h�z�s k�vet. Regul�ris kifejez�ssel kifejezve ez a k�vetkez�t jelenti: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

Megjegyz�s: Ebben az esetben egy bet� lehet az angol abc egy bet�je a-z-ig �s A-Z-ig, valamint a 127-t�l 255-ig terjed� (0x7f-0xff) ASCII k�d� karakterek.

$var = "G�za";
$Var = "J�nos";
echo "$var, $Var";           // ki�rja, hogy "G�za, J�nos"

$4site        = 'ez nem j�'; // nem �rv�nyes, mert sz�mmal kezd�dik
$_4site       = 'ez ok';     // �rv�nyes, al�h�z�ssal kezd�dik
$t�yte        = 'mansikka';  // �rv�nyes, az '�' az ASCII 228-as karaktere
$t�k�rf�r�g�p = "�rv�zt�r�"; // �rv�nyes, ellen�rizheted egy ASCII t�bl�ban

PHP 3-ban a v�ltoz�khoz mindig �rt�kek tartoznak. Vagyis ha egy kifejez�st rendel�nk egy v�ltoz�hoz, az eredeti kifejez�s eg�sz�nek �rt�ke m�sol�dik a c�lv�ltoz�ba. Ez azt jelenti, hogy ha p�ld�ul egy v�ltoz� �rt�k�t egy m�sikhoz rendelj�k, egyik�k megv�ltoz�sa sincs hat�ssal a m�sikra. N�zd �t Kifejez�sek c. fejezetet, ahol az ilyen jelleg� hozz�rendel�sekr�l t�bb inform�ci� tal�lhat�.

PHP 4-ben lehet�s�g van egy m�sik hozz�rendel�si m�dra: v�ltoz� referencia szerinti hozz�rendel�s�re. Ez azt jelenti, hogy az �j v�ltoz� egyszer�en hivatkozik (m�s sz�val "alias lesz", vagy "r� mutat") az eredetire. Az �j v�ltoz�n v�gzett v�ltoztat�sok az eredetit is �rintik �s ford�tva. Ez azt is jelenti, hogy nem t�rt�nik m�sol�s; ekk�ppen a hozz�rendel�s gyorsabban t�rt�nik meg. Igaz ugyan, hogy ez a sebess�gn�veked�s csak "feszes" ciklusokban vagy nagy t�mb�kn�l ill. objektumok �tad�sakor jelentkezik.

Referencia szerinti �rt�kad�shoz egyszer�en & jelet kell az �tadand� v�ltoz� neve el� �rni. Az al�bbi k�d - p�ld�ul - ki�rja k�tszer, hogy 'Nevem Bob':

<?php
$ize = 'Bob';              // 'Bob' hozz�rendel�se $ize-hoz
$bigyo = &$ize;            // Hivatkoz�s $ize-ra $bigyo-ban.
$bigyo = "Nevem $bigyo";   // $bigyo megv�ltoztat�sa...
echo $ize;                 // $ize is megv�ltozott
echo $bigyo;
?>

Fontos megjegyezni, hogy csak megnevezett v�ltoz�kra lehet referenci�t l�trehozni.

<?php
$ize = 25;
$bigyo = &$ize;      // Ez egy �rv�nyes hozz�rendel�s.
$bigyo = &(24 * 7);  // �rv�nytelen referencia egy n�vtelen kifejez�sre.

function test()
{
   return 25;
}

$bigyo = &test();    // �rv�nytelen.
?>

User Contributed Notes
V�ltoz�k
add a note about notes
[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".

add a note about notes
previousB�v�szked�s a t�pusokkalEl�re defini�lt v�ltoz�knext
Last updated: Fri, 30 Aug 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Aug 31 06:19:44 2002 CEST