Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Variable Scope in Functions | Functions in PHP
Quizzes & Challenges
Quizzes
Challenges
/
PHP Core Concepts

bookVariable Scope in Functions

Note
Definition

Variable scope refers to the part of a program where a variable can be accessed or used. It determines whether a variable is available inside functions, outside functions, or across multiple function calls.

To write reliable functions in PHP, it's important to understand how variable scope works. A variable's scope defines where it can be accessed and how long it remains available during execution. When working with functions, PHP uses three main types of scope: local, global, and static.

A local variable is declared inside a function and can only be used within that function. After the function finishes running, the variable is destroyed and its value is no longer available.

variable_scope.php

variable_scope.php

copy
12345678910111213141516171819202122232425262728
<?php // Global variable $globalVar = "I am global"; function testScope() { // Local variable $localVar = "I am local"; echo "Inside function, localVar: $localVar\n"; // Accessing global variable using 'global' keyword global $globalVar; echo "Inside function, globalVar: $globalVar\n"; // Static variable static $staticVar = 0; $staticVar++; echo "Inside function, staticVar: $staticVar\n"; } testScope(); testScope(); echo "Outside function, globalVar: $globalVar\n"; // The following will cause errors if uncommented, because $localVar and $staticVar are not accessible here // echo $localVar; // echo $staticVar; ?>

There are two useful variable types in PHP. Global variables, which are defined outside functions and accessed inside them using the global keyword, and static variables, which are defined inside functions with static and keep their value between calls.

Variable scope affects both the accessibility and the lifetime of variables in your PHP programs. Using the right scope helps you avoid unexpected behavior and keeps your code organized and secure.

question mark

Which type of variable in PHP retains its value between multiple calls to the same function?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you give examples of how to use global and static variables in PHP?

What are some common mistakes when working with variable scope in PHP?

How does variable scope impact the security of my PHP code?

bookVariable Scope in Functions

Glissez pour afficher le menu

Note
Definition

Variable scope refers to the part of a program where a variable can be accessed or used. It determines whether a variable is available inside functions, outside functions, or across multiple function calls.

To write reliable functions in PHP, it's important to understand how variable scope works. A variable's scope defines where it can be accessed and how long it remains available during execution. When working with functions, PHP uses three main types of scope: local, global, and static.

A local variable is declared inside a function and can only be used within that function. After the function finishes running, the variable is destroyed and its value is no longer available.

variable_scope.php

variable_scope.php

copy
12345678910111213141516171819202122232425262728
<?php // Global variable $globalVar = "I am global"; function testScope() { // Local variable $localVar = "I am local"; echo "Inside function, localVar: $localVar\n"; // Accessing global variable using 'global' keyword global $globalVar; echo "Inside function, globalVar: $globalVar\n"; // Static variable static $staticVar = 0; $staticVar++; echo "Inside function, staticVar: $staticVar\n"; } testScope(); testScope(); echo "Outside function, globalVar: $globalVar\n"; // The following will cause errors if uncommented, because $localVar and $staticVar are not accessible here // echo $localVar; // echo $staticVar; ?>

There are two useful variable types in PHP. Global variables, which are defined outside functions and accessed inside them using the global keyword, and static variables, which are defined inside functions with static and keep their value between calls.

Variable scope affects both the accessibility and the lifetime of variables in your PHP programs. Using the right scope helps you avoid unexpected behavior and keeps your code organized and secure.

question mark

Which type of variable in PHP retains its value between multiple calls to the same function?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 4
some-alt