- PHP Variable Scopes
- Introduction to PHP variable scopes
- Local variables
- Global variables
- Superglobal variables
- Static variables
- Function parameters
- Summary
- What are the differences between PHP constants and variables ?
- PHP Variable Scope
- Local variable
- Global variable
- Static variable
- PHP Variables, Types, Constants
- 15 php — variable types and quotes
- How to declare variables in PHP with rules and getting
- What are the differences between PHP constants and variables ?
- PHP
- PHP
- Php variable declaration with an interrogation point before the type [duplicate]
- What are the different scopes of variables in PHP ?
PHP Variable Scopes
Summary: in this tutorial, you’ll learn about PHP variable scopes, which specify the part of code that can access a variable.
Introduction to PHP variable scopes
The scope of a variable determines which part of the code can access it. The locations where the variable can be accessible determine the scope of the variable.
In PHP, variables have four types of scopes:
Local variables
When you define a variable inside a function, you can only access that variable within the function. And it’s said that the variable is local to the function.
The following example defines the say() function that displays the ‘Hi’ message:
function say() < $message = 'Hi'; echo $message; >
Code language: PHP (php)
Inside the say() function, we define the $message variable. The $message variable is a local variable. And you cannot access it from the outside of the say() function.
Also, the $message variable only exists during the execution of the say() function. Once the say() function ends, the $mesage variable won’t exist anymore.
Global variables
When you declare a variable outside of a function, the variable is global. It means that you can access the variable anywhere within the script except inside a function. For example:
$message = 'Hello'; function say() < $message = 'Hi'; echo $message; > echo $message; // Hello
Code language: PHP (php)
In this script, we have two variables with the same name $message .
The first variable is the global variable because we define it outside of a function. The $message variable that we define inside the function is the local variable. Even though these variables have the same name, they’re two different variables.
PHP allows you to access a global variable within a function by using the global keyword. For example:
$message = 'Hello'; function say() < global $message; echo $message; // Hello > say();
Code language: PHP (php)
- First, define a global variable called $message .
- Second, reference the global variable $message inside the say() function.
It’s important to note that it’s not a good practice to use global variables.
Superglobal variables
PHP has a list of built-in variables, which are known as superglobal variables. The superglobal variables provide information about the PHP script’s environment.
The superglobal variables are always available in all parts of the script. The following table shows the list of PHP superglobal variables:
Superglobal Variables | Meaning |
---|---|
$GLOBALS | Returns an array that contains global variables. The variable names are used to select which part of the array to access. |
$_SERVER | Returns data about the webserver environment. |
$_GET | Return data from GET requests. |
$_POST | Return data from POST requests. |
$_COOKIE | Return data from HTTP cookies |
$_FILES | Return data from POST file uploads. |
$_ENV | Return information about the script’s environment. |
$_REQUEST | Return data from the HTTP request |
$_SESSION | Return variables registered in a session |
Static variables
A static variable retains its value between function calls. Also, a static variable is only accessible inside the function. To define a static variable, you use the static keyword. For example:
function get_counter() < static $counter = 1; return $counter++; > echo get_counter() . '
'; // 1 echo get_counter() . '
'; // 2 echo get_counter() . '
'; // 3
Code language: PHP (php)
1 2 3
Code language: plaintext (plaintext)
- First, define the get_counter() function with a static variable named $counter .
- Second, call the set_counter() function three times. As you notice that the value of the $counter variable is increased by one after each function call.
Function parameters
Function parameters are local to the function. Therefore, function parameters can only be accessible inside the function. For example:
function sum($items) < $total = 0; foreach($items as $item) < $total += $item; >return $total; > // $items cannot be accessible here echo sum([10,20,30]);
Code language: PHP (php)
In this example, the $items is the parameter of the sum() function. It can only be accessible within the sum() function.
Summary
What are the differences between PHP constants and variables ?
Local variables: The variables declared within a function are called local variables to that function and have their scope only in that particular function. Let’s understand the local variables with the help of an example: File: local_variable1.php Output: File: local_variable2.php Output: Global variable The global variables are the variables that are declared outside the function.
PHP Variable Scope
The scope of a variable is defined as its range in the program under which it can be accessed. In other words, «The scope of a variable is the portion of the program within which it is defined and can be accessed.»
PHP has three types of variable scopes:
Local variable
The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.
A variable declaration outside the function with the same name is completely different from the variable declared inside the function. Let’s understand the local variables with the help of an example:
Local variable declared inside the function is: 45
mytest(); //using $lang (local variable) outside the function will generate an error echo $lang; ?>
Web development language: PHP Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28
Global variable
The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.
Let’s understand the global variables with the help of an example:
Example:
"; > global_var(); echo "Variable outside the function: ". $name; ?>
Variable inside the function: Sanaya Sharma Variable outside the function: Sanaya Sharma
Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate an error that the variable is undefined.
Example:
Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6 Variable inside the function:
Using $GLOBALS instead of global
Another way to use the global variable inside the function is predefined $GLOBALS array.
Sum of global variables is: 18
If two variables, local and global, have the same name, then the local variable has higher priority than the global variable inside the function.
Note: local variable has higher priority than the global variable.
Static variable
It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. Therefore, another important feature of variable scoping is static variable. We use the static keyword before the variable to define a variable, and this variable is called as static variable .
Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:
Example:
"; echo "Non-static: " .$num2 .""; >//first function call static_var();//second function call static_var(); ?>
Static: 4 Non-static: 7 Static: 5 Non-static: 7
You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This is why because $num1 is not a static variable, so it freed its memory after the execution of each function call.
Declaring Variable Types in PHP?, EDIT: As of PHP 7.4, you can declare member variables of a class/interface/trait as a specific type like public int $a; , and variables that are
PHP Variables, Types, Constants
In this particular video we will learn what are variables, how we can create them and what data Duration: 11:14
15 php — variable types and quotes
Learn PHP in 15 minutes · HTML, Validating your code · The principles of design (03) · PHP Duration: 9:19
How to declare variables in PHP with rules and getting
04:29 gettype() , getting the type of variable 07:34 undefined variables in PHPDeclaring Duration: 9:09
What are the differences between PHP constants and variables ?
PHP Constants : PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. They are case-sensitive. By default, constant identifiers are always uppercase. Usually, a Constant name starts with an underscore or a letter which is followed by a number of letters and numbers. They are no need to write a constant with the $ sign. The constant() function is used to return the value of a constant.
PHP
Welcome to geeksforgeeks.com!
PHP Variables: A PHP variable is a name given to a memory address that holds data. The basic method to declare a PHP variable is by using a $ sign which is followed by the variable name. A variable helps the PHP code to store information in the middle of the program. If we use a variable before it is assigned then it already has a default value stored in it. Some of the data types that we can use to construct a variable are integers, doubles, and boolean.
PHP
Hello from geeksforgeeks! 5 10.5
Difference between PHP Constants and PHP Variables
The data type of PHP constant cannot be changed during the
The data type of the PHP variable can be changed during the
We can not define a constant using any simple assignment operator
rather it can only be defined using define().
PHP Variable Handling Functions, PHP Variable Handling Functions ; get_defined_vars(), Returns all defined variables, as an array ; get_resource_type(), Returns the type of a resource ; gettype()
Php variable declaration with an interrogation point before the type [duplicate]
Variable $str can only be of type string :
Variable $str can be of type string or null :
15 php — variable types and quotes, Learn PHP in 15 minutes · HTML, Validating your code · The principles of design (03) · PHP Duration: 9:19
What are the different scopes of variables in PHP ?
Variable Scopes: The scope of a variable is defined as its extent in the program within which it can be accessed, i.e. the scope of a variable is the portion of the program within which it is visible or can be accessed. Depending on the scopes, PHP has three variable scopes.
Local variables: The variables declared within a function are called local variables to that function and have their scope only in that particular function. In simple words, it cannot be accessed outside that function. Any declaration of a variable outside the function with the same name as that of the one within the function is a completely different variable. For now, consider a function as a block of statements.