Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Variable Scope in Functions | Functions in PHP
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookVariable Scope in Functions

Scorri per mostrare il 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 4
some-alt