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'; once somewhere in application doesn't mean can refer $foo everywhere else inside application. variable $foo has scope within valid , code in same scope has access variable.

how scope defined in php?

very simple: php has function scope. that's kind of scope separator exists in php. variables inside function available inside function. variables outside of functions available anywhere outside of functions, not inside 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 inside myfunc. code inside 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 included code applies other code: functions separate scope. purpose of scope, may think of including files copy , 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 inside myfunc, variables inside a.php have local function scope. because appear in global scope in a.php doesn't mean are, depends in context code included/executed.

what functions inside functions , classes?

every new function declaration introduces new scope, it's simple.

(anonymous) functions inside functions

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 inside application, you'd stepping on variables no real way track changes what. there many sensible names can give variables, want use variable "$name" in more 1 place. if have unique variable name once in app, you'd have resort complicated naming schemes make 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...

crossing scope boundaries

the right way: passing variables in , out

function foo($bar) {     echo $bar;     return 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 , return values come from:

$baz   = 'baz'; $blarg = foo($baz); 

extending scope of variables anonymous functions

$foo = 'bar';  $baz = function () use ($foo) {     echo $foo; };  $baz(); 

the anonymous function explicitly includes $foo surrounding scope. note not same global scope.

the wrong way: 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 keep modifying and requiring global state. want functions stateless, acting on inputs , returning defined output, many times call them.

you should avoid using global scope in way as possible; should not "pulling" variables out of global scope local scope.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -