1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
<HTML
><HEAD
><TITLE
>Variable scope</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.html"><LINK
REL="UP"
TITLE="Variables"
HREF="language.variables.html"><LINK
REL="PREVIOUS"
TITLE="Predefined variables"
HREF="language.variables.predefined.html"><LINK
REL="NEXT"
TITLE="Variable variables"
HREF="language.variables.variable.html"><META
NAME="HTTP_EQUIV"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.variables.predefined.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Variables</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.variables.variable.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.variables.scope"
>Variable scope</A
></H1
><P
> The scope of a variable is the context within which it is defined.
For the most part all PHP variables only have a single scope.
This single scope spans included and required files as well. For
example:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2755"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $a = 1;
include "b.inc";
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> Here the <TT
CLASS="varname"
>$a</TT
> variable will be available within
the included <TT
CLASS="filename"
>b.inc</TT
> script. However, within
user-defined functions a local function scope is introduced. Any
variable used inside a function is by default limited to the local
function scope. For example:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2760"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>
$a = 1; /* global scope */
Function Test () {
echo $a; /* reference to local scope variable */
}
Test ();
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> This script will not produce any output because the echo statement
refers to a local version of the <TT
CLASS="varname"
>$a</TT
> variable,
and it has not been assigned a value within this scope. You may
notice that this is a little bit different from the C language in
that global variables in C are automatically available to
functions unless specifically overridden by a local definition.
This can cause some problems in that people may inadvertently
change a global variable. In PHP global variables must be
declared global inside a function if they are going to be used in
that function. An example:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2764"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $a = 1;
$b = 2;
Function Sum () {
global $a, $b;
$b = $a + $b;
}
Sum ();
echo $b;
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> The above script will output "3". By declaring
<TT
CLASS="varname"
>$a</TT
> and <TT
CLASS="varname"
>$b</TT
> global within the
function, all references to either variable will refer to the
global version. There is no limit to the number of global
variables that can be manipulated by a function.
</P
><P
> A second way to access variables from the global scope is to use
the special PHP-defined <TT
CLASS="varname"
>$GLOBALS</TT
> array. The
previous example can be rewritten as:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2771"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> $a = 1;
$b = 2;
Function Sum () {
$GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
}
Sum ();
echo $b;
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> The <TT
CLASS="varname"
>$GLOBALS</TT
> array is an associative array with
the name of the global variable being the key and the contents of
that variable being the value of the array element.
</P
><P
> Another important feature of variable scoping is the
<I
CLASS="emphasis"
>static</I
> variable. A static variable exists
only in a local function scope, but it does not lose its value
when program execution leaves this scope. Consider the following
example:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2777"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> Function Test () {
$a = 0;
echo $a;
$a++;
}
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> This function is quite useless since every time it is called it
sets <TT
CLASS="varname"
>$a</TT
> to <TT
CLASS="literal"
>0</TT
> and prints
"0". The <TT
CLASS="varname"
>$a</TT
>++ which increments the
variable serves no purpose since as soon as the function exits the
<TT
CLASS="varname"
>$a</TT
> variable disappears. To make a useful
counting function which will not lose track of the current count,
the <TT
CLASS="varname"
>$a</TT
> variable is declared static:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2785"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> Function Test () {
static $a = 0;
echo $a;
$a++;
}
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
> Now, every time the Test() function is called it will print the
value of <TT
CLASS="varname"
>$a</TT
> and increment it.
</P
><P
> Static variables also provide one way to deal with recursive
functions. A recursive function is one which calls itself. Care
must be taken when writing a recursive function because it is
possible to make it recurse indefinitely. You must make sure you
have an adequate way of terminating the recursion. The following
simple function recursively counts to 10, using the static
variable <TT
CLASS="varname"
>$count</TT
> to know when to stop:
</P
><DIV
CLASS="informalexample"
><A
NAME="AEN2791"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> Function Test () {
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
Test ();
}
$count--;
}
</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.variables.predefined.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.variables.variable.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Predefined variables</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.variables.html"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variable variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|