CakeFest 2024: The Official CakePHP Conference

Öntanımlı Değişkenler

PHP çalıştırdığı herhangi bir betiğe çok sayıda önceden tanımlı değişken sağlar. Ancak, bu değişkenlerin çoğu hangi sunucunun çalıştığına, sunucunun sürümüne ve kurulumuna ve başka bir çok şeye bağlı olduğu için tam olarak belgelendirilememektedir. Bu değişkenlerden bazıları PHP komut satırından çalıştığında mevcut olmayacaktır. Ayrıntılar için, öntanımlı değişkenler listesine bakınız.

PHP, ayrıca, ortam değişkenleri ve kullanıcı girdi değişkenleri ile HTTP sunucusundaki değişkenleri (eğer uygulanabiliyorsa) içeren bir takım önceden tanımlı diziler sağlamaktadır. Bu diziler özel olmaktan ziyade otomatik olarak küreseldir, yani, otomatik olarak her etki alanında bulunurlar. Bu nedenle, "Süper küreseller" olarak bilinirler. (PHP'de kullanıcı-tanımlı süper küreseller için bir mekanizma yoktur.) Ayrıntılar için bkz, list of süper küresellerin listesi.

Bilginize: Değişken değişkenler

İşlevler veya sınıf yöntemleri içinde süper küreseller değişken değişkenleri olarak kullanılamazlar.

Eğer variables_order yönergesine uygun değişkenler atanmamışsa, ilgili oldukları PHP önceden tanımlı dizileri de boş bırakılır.

add a note

User Contributed Notes 4 notes

up
113
johnphayes at gmail dot com
17 years ago
I haven't found it anywhere else in the manual, so I'll make a note of it here - PHP will automatically replace any dots ('.') in an incoming variable name with underscores ('_'). So if you have dots in your incoming variables, e.g.:

example.com/page.php?chuck.norris=nevercries

you can not reference them by the name used in the URI:
//INCORRECT
echo $_GET['chuck.norris'];

instead you must use:
//CORRECT
echo $_GET['chuck_norris'];
up
11
DD32=theonly_DD32[&]yahoo.com.au
18 years ago
I have this function in my main files, it allows for easier SEO for some pages without having to rely on .htaccess and mod_rewrite for some things.
<?php
function long_to_GET(){
/**
* This function converts info.php/a/1/b/2/c?d=4 TO
* Array ( [d] => 4 [a] => 1 [b] => 2 [c] => )
**/
if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != ''){
//Split it out.
$tmp = explode('/',$_SERVER['PATH_INFO']);
//Remove first empty item
unset($tmp[0]);
//Loop through and apend it into the $_GET superglobal.
for($i=1;$i<=count($tmp);$i+=2){ $_GET[$tmp[$i]] = $tmp[$i+1];}
}
}
?>

Its probably not the most efficient, but it does the job rather nicely.

DD32
up
11
lopez dot on dot the dot lists at yellowspace dot net
21 years ago
- Security Issue and workaround -
If You use "eval()" to execute code stored in a database or elsewhere, you might find this tip useful.

Issue:
By default, all superglobals are known in every function.
Thus, if you eval database- or dynamically generated code (let's call it "potentially unsafe code"), it can use _all_ the values stored in _any_ superglobal.

Workaround:
Whenever you want to hide superglobals from use in evaluated code, wrap that eval() in an own function within which you unset() all the superglobals. The superglobals are not deleted by php in all scopes - just within that function. eg:

function safeEval($evalcode) {
unset($GLOBALS);
unset($_ENV);
// unset any other superglobal...
return eval($evalcode);
}

(This example assumes that the eval returns something with 'return')

In addition, by defining such a function outside classes, in the global scope, you'll make sure as well that the evaluated ('unsafe') code doesn't have access to the object variables ($this-> ...).
up
1
LouisGreen at pljg dot freeserve dot co dot uk
21 years ago
It seems that when you wish to export a varible, you can do it as return $varible, return an array(), or globalise it. If you return something, information for that varible can only travel one way when the script is running, and that is out of the function.

function fn() {
$varible = "something";

return $variable;
}

echo fn();
OR
$newvariable = fn();

Although if global was used, it creates a pointer to a varible, whether it existed or not, and makes whatever is created in the function linked to that global pointer. So if the pointer was global $varible, and then you set a value to $varible, it would then be accessible in the global scope. But then what if you later on in the script redefine that global to equal something else. This means that whatever is put into the global array, the information that is set in the pointer, can be set at any point (overiden). Here is an example that might make this a little clearer:

function fn1() {

global $varible; // Pointer to the global array
$varible = "something";
}

fn1();
echo $varible; // Prints something
$varible = "12345";
echo $varible; // Prints 12345

function fn2() {

global $varible; // Pointer to the global array
echo $varible;
}

fn2(); // echos $varible which contains "12345"

Basically when accessing the global array, you can set it refer to something already defined or set it to something, (a pointer) such as varible you plan to create in the function, and later possibly over ride the pointer with something else.
To Top