PHP/Language Basics/GLOBALS — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:05, 26 мая 2010
Содержание
Accessing Global Variables with the global Statement
<html>
<head><title>The global Statement</title></head>
<body>
<div>
<?php
$life=42;
function meaningOfLife() {
global $life;
print "The meaning of life is $life<br />";
}
meaningOfLife();
?>
</div>
</body>
</html>
Create global variables
<?php
$somevar = 15;
function addit() {
GLOBAL $somevar;
$somevar++;
print "Somevar is $somevar";
}
addit();
?>
$GLOBALS with property
<?php
$a = 7;
function test() {
$GLOBALS["a"] = 20;
}
test();
echo "\$a = $a\n";
?>
Global vs function level
<?php
$a = 7;
function test() {
global $a;
$a = 20;
}
test();
echo "\$a = $a\n";
?>
Looping Through the $GLOBALS Array
<html>
<head>
<title>Looping through the $GLOBALS array</title>
</head>
<body>
<?php
foreach ( $GLOBALS as $key=>$value ){
print "\$GLOBALS[\"$key\"] == $value<br>";
}
?>
</body>
</html>
Modifying a Variable with $GLOBALS
<?
$dinner = "Curry";
function hungry_dinner() {
$GLOBALS["dinner"] .= " and Fried";
}
print "Regular dinner is $dinner";
print "\n";
hungry_dinner();
print "Hungry dinner is $dinner";
?>
Overriding Scope with the GLOBALS Array
function foo( ) {
$GLOBALS["bar"] = "wombat";
}
$bar = "baz";
foo( );
print $bar;
Set new value into GLOBALS values
<?php
$somevar = 15;
function addit() {
$GLOBALS["somevar"]++;
}
addit();
print "Somevar is ".$GLOBALS["somevar"];
?>