PHP: �lland�k - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousV�ltoz�k a PHP-n k�v�lr�lEl�re defini�lt �lland�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 9. �lland�k

Az �lland� egy skal�r �rt�k azonos�t�ja (neve). Mint ahogy az elnevez�se is mutatja, a program fut�sa sor�n nem v�ltozik/hat meg az �rt�ke. Ez al�l csak a 'm�gikus' __FILE__ �s __LINE__ �lland�k t�nhetnek kiv�teleknek, �m ezek val�j�ban nem �lland�k. Az �lland�k alapesetben �rz�kenyek a kis- �s nagybet�s ir�sm�dra. Meg�llapod�s szerint �ltal�ban csupa nagybet�s neveket haszn�lunk az �lland�k neveik�nt.

Az �lland�k neveire a PHP m�s jelz�ivel azonos szab�lyok vonatkoznak. Egy �rv�nyes �lland�-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.

Az �lland�k b�rhonnan el�rhet�ek -- az aktu�lis �rv�nyess�gi k�rt�l (scope) f�ggetlen�l b�rhol haszn�lhat�k.

Szintakszis

A define() f�ggv�nnyel lehet �lland�t l�trehozni, amelynek defini�l�sa ut�n k�s�bb nem lehet t�r�lni vagy megv�ltoztatni az �rt�k�t.

Csak skal�ris adat (boolean, integer, float vagy string t�pus�) lehet egy konstans tartalma.

A konstans �rt�k�re a nev�nek megad�s�val lehet hivatkozni. A v�ltoz�kkal ellent�tben nem szabad $ jelet tenned a konstand neve el�. Haszn�lhat� m�g a constant() f�ggv�nyt is, ha p�ld�ul a konstans nev�t egy v�ltoz� adja. A get_defined_constants() f�ggv�nnyel lehet a defini�lt konstansok list�j�t megkapni.

Megjegyz�s: A konstansok �s a (glob�lis) v�ltoz�k k�l�nb�z� n�vt�rben vannak. Ez azt jelenti, hogy a TRUE �s a $TRUE k�t k�l�nb�z� dolgot jelent.

Ha egy defini�latlan konstanst pr�b�lsz meg haszn�lni, a PHP a konstans nev�t veszi karaktersorozatk�nt �rt�k�l. Ilyen esetekben egy notice szint� hiba keletkezik. A defined() f�ggv�ny seg�ts�g�vel vizsg�lhat� a konstans l�tez�se.

A k�vetkez�k a fontosabb k�l�nbs�gek a v�ltoz�khoz k�pest:

  • Az �lland�k nev�t nem kell doll�r jellel $ kezdeni.

  • Az �lland�k ak�rhol defini�lhat�k, �s ak�rhonnan el�rhet�k, a v�ltoz�k k�rnyezeti korl�toz�sait�l f�ggetlen�l.

  • Az �lland�k nem m�dos�that�ak, �s nem t�r�lhet�ek, miut�n egyszer l�trehozt�k azokat.

  • Az �lland�k csak skal�ris �rt�keket tartalmazhatnak.

P�lda 9-1. Konstansok defini�l�sa

<?php
define("KONSTANS", "Hell� vil�g!");
echo KONSTANS; // ki�rja, hogy "Hell� vil�g!"
echo Konstans; // ki�rja, hogy "Konstans" �s hib�t eredm�nyez
?>

User Contributed Notes
�lland�k
add a note about notes
[email protected]
29-Jan-2000 03:13

You can't define a constant that is another constant. In the same versions where define('FOO', TRUE) won't work, neither will define ('BAR', __LINE__). At least one of the DOS versions of php3 reliably demonstrates this behaviour. I'd call it a "no-no" to try and define a constant based on another constant.
[email protected]
04-Aug-2000 11:44

To get a full path (the equivalent of something like "__PATH__") use
dirname($SCRIPT_FILENAME)
to get the directory name of the called script and
dirname(__FILE__)
to get the directory name of the include file.

[email protected]
23-Jan-2001 11:54

It may be useful to note that, in php4 (what version this started I don't know, but it didn't do it before we upgraded to php4) __FILE__ will follow symlinks to the origional file.
[email protected]
09-Feb-2001 01:23

"SID" is defined also. It's the same as "session_name()=session_id()" and usufull for managing session using URL.
It's mentioned in Session reference, but I thought it would appropriate mention this section, too.

"NULL" is also defined and has null type. gettype(null) returns null and is_null($val) returns true for null - PHP4.0.4pl1 or later(?)

(I would like to know ALL constants defined by PHP in one place)

I also think that the class initilizer limitation intoruduced in PHP4 should be noted here.

PHP4 does not allow;

define ('C1',1);
define ('C2',2);
class foo {
var $val = C1 | C2;
}

Following code works.(PHP4.0.4pl1-linux)
define ('C1',1);
define ('C2',2);
class foo {
var $val = C1;
functin foo() {
$this->val = C1 | C2;
}
}

Details are explained

(thanks to hholzgra)

[email protected]
31-Mar-2001 04:38

If you use $PHP_SELF in forms or in places where a script has to reload itself, you will get the full path relative to the web root. This is very useful because it will always work, even if you move your script around on the server. You don't need to mess around with absolute or relative paths. An example would be:

<form method=POST action=$PHP_SELF?do_something> or
<a href=$PHP_SELF?do_something>Make script do something</a>

[email protected]
11-Jun-2001 04:42

The pre-defined constant '__FILE__' does not work in same way at every version of PHP.

Some version of PHP has the relative path, and some other has the absolute path on __FILE__ constant..

Please be carefull in use..

[PS]
I have not tested at all versions of PHP but the version of 4.04pl.. and 4.05 are certainly not working in same way.. If you want to see that bug(?), I can show you an example.

[email protected]
22-Jun-2001 05:52

Constants have the same naming rules as variables (though the docs don't actually say that). So you can't begin a constant name with a number.

if you name the
constant beginning with a numeral, such as:

define ("999CONSTANT", "some value");

whereas
define ("CONSTANT999", "some value");
works fine.

[email protected]
25-Aug-2001 11:10

Major difference between PHP3 and PHP4:
If you define a function that uses some constant C before you
define() C, C will be undefined in the function in PHP3 but not
in PHP4. Thus constants are more pre-processor-ish in PHP3,
but probably more useful in PHP4.

[email protected]
21-Feb-2002 05:28

Also predefined (in PHP 4.1.1 on linux) are a set of LOG_* constants such as LOG_DEBUG, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_CRIT. I hit this when writing an external logging facility, and couldn't work out why some of my constants had the wrong values e.g.

<?php
define("LOG_DEBUG", 10);
print LOG_DEBUG;
?>

results in printing "7" rather than the "10" I had expected. This cost me about 15 minutes of grief. I would have expected some warning about trying to redefine a predefined constant.

[email protected]
25-Feb-2002 05:53

Warning, constants used within the heredoc syntax () are not interpreted!

Editor's Note: This is true. PHP has no way of recognizing the constant from any other string of characters within the heredoc block.

[email protected]
23-Mar-2002 03:08

The __FILE__ constant in 4.2rc1 (CLI) will return the location of script specified to be run, rather than the absolute file.

eg. /usr/bin/phpmole (a softlink to /usr/lib/php/phpmole/phpmole.php)

started like this
bash#/usr/bin/phpmole

the line echo __FILE__ in phpmole.php will output /usr/bin/phpmole - in the CGI it would have returned /usr/lib/php/phpmole/phpmole.php

the workaround is to check for links!!
$f = __FILE__;
if (is_link($f)) $f = readlink($f);

[email protected]
18-Jul-2002 07:27

__FILE__ doesn't work with Zend Encoder 2.0.x (ZE does funky things with __FILE__).

This may be fixed in the future, but we don't know when. In the mean time, Zend has suggested we use the undocumented "zend_loader_current_file()", which seems to do the same thing as __FILE__ for me.

S

add a note about notes
previousV�ltoz�k a PHP-n k�v�lr�lEl�re defini�lt �lland�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