php - Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors? -
php - Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors? -
note: reference question dealing variable scope in php. please close of many questions fitting pattern duplicate of one.
what "variable scope" in php? variables 1 .php file accessible in another? why "undefined variable" errors?
what "variable scope"?
variables have limited "scope", or "places accessible". because wrote $foo = 'bar';
1 time somewhere in application doesn't mean can refer $foo
everywhere else within application. variable $foo
has scope within valid , code in same scope has access variable.
very simple: php has function scope. that's kind of scope separator exists in php. variables within function available within function. variables outside of functions available anywhere outside of functions, not within function. means there's 1 special scope in php: global scope. variable declared outside of function within global scope.
example:<?php $foo = 'bar'; function myfunc() { $baz = 42; }
$foo
in global scope, $baz
in local scope within myfunc
. code within myfunc
has access $baz
. code outside myfunc
has access $foo
. neither has access other:
<?php $foo = 'bar'; function myfunc() { $baz = 42; echo $foo; // doesn't work echo $baz; // works } echo $foo; // works echo $baz; // doesn't work
scope , included files file boundaries not separate scope:
a.php
<?php $foo = 'bar';
b.php
<?php include 'a.php'; echo $foo; // works!
the same rules apply include
d code applies other code: function
s separate scope. purpose of scope, may think of including files re-create , pasting code:
c.php
<?php function myfunc() { include 'a.php'; echo $foo; // works } myfunc(); echo $foo; // doesn't work!
in above example, a.php
included within myfunc
, variables within a.php
have local function scope. because appear in global scope in a.php
doesn't mean are, depends in context code included/executed.
every new function
declaration introduces new scope, it's simple.
function foo() { $foo = 'bar'; $bar = function () { // no access $foo $baz = 'baz'; }; // no access $baz }
classes $foo = 'foo'; class bar { public function baz() { // no access $foo $baz = 'baz'; } } // no access $baz
what scope for? dealing scoping issues may seem annoying, limited variable scope essential writing complex applications! if every variable declare available everywhere else within application, you'd stepping on variables no real way track changes what. there many sensible names can give variables, want utilize variable "$name
" in more 1 place. if have unique variable name 1 time in app, you'd have resort complicated naming schemes create sure variables unique , you're not changing wrong variable wrong piece of code.
observe:
function foo() { echo $bar; }
if there no scope, above function do? $bar
come from? state have? initialized? have check every time? not maintainable. brings to...
function foo($bar) { echo $bar; homecoming 42; }
the variable $bar
explicitly coming scope function argument. looking @ function it's clear values works originate from. explicitly returns value. caller has confidence know variables function work , homecoming values come from:
$baz = 'baz'; $blarg = foo($baz);
extending scope of variables anonymous functions $foo = 'bar'; $baz = function () utilize ($foo) { echo $foo; }; $baz();
the anonymous function explicitly includes $foo
surrounding scope. note not same global scope.
global
as said before, global scope special, , functions can explicitly import variables it:
$foo = 'bar'; function baz() { global $foo; echo $foo; $foo = 'baz'; }
this function uses , modifies global variable $foo
. do not this! (unless really really know you're doing, , then: don't!)
all caller of function sees this:
baz(); // outputs "bar" unset($foo); baz(); // no output, wtf?! baz(); // outputs "baz", wtf?!?!!
there's no indication function has side effects, yet does. becomes tangled mess functions maintain modifying and requiring global state. want functions stateless, acting on inputs , returning defined output, many times phone call them.
you should avoid using global scope in way much possible; should not "pulling" variables out of global scope local scope.
php scope
Comments
Post a Comment