Home php What is the correct definition from scope (scope)

What is the correct definition from scope (scope)

Author

Date

Category

often come across such a thing as scope but I do not understand it. Say the exact definition What Scope and please apply 2-3 examples if possible on PHP .


Answer 1, Authority 100%

Variable scope is a context in which this variable is defined. In most cases, all PHP variables have only one scope. This single scope covers also included (Include) and the required (Require) files. For example:

& lt;? php
$ a = 1;
include 'b.inc';
? & gt;

Here the $ a variable will be available inside the B.INC script. However, the definition (body) of the user function sets the local scope of this function. Any default variable used inside the function is limited to the local visibility area of ​​the function. For example:

& lt;? php
$ a = 1; / * Global scope * /
Function Test ()
{
  echo $ a; / * Reference to a variable local visibility area * /
}
test ();
? & gt;

This script will not generate any output, since the expression ECHO indicates the local version of the variable $ a , and within this area of ​​visibility, it was not assigned to the value. Perhaps you noticed that it is slightly different from the C language is that the global variables in C are automatically available to functions, unless they were overwritten by the local definition. This can cause some problems as people can inadvertently change the global variable. In PHP, if the global variable will be used inside the function, it must be declared global within the definition of the function.

source

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions