Variable scope

Contents
Local scope
The global keyword
Static variables
Pass by value and reference

Local scope

Variables created inside a function are local: they exist only while the function runs and stay invisible outside it. An outer variable with the same name is a different variable, so the two never overwrite each other by accident.

$name = "Ada"; function show(): void { $name = "Grace"; echo $name; } show(); echo $name;

The global keyword

The global keyword imports an outer variable into the function, so reads and writes touch the shared copy. It works, but it hides the data flow: prefer parameters for input and return for output, and reserve global for small scripts and legacy code.

$total = 0; function addBonus(int $points): void { global $total; $total = $total + $points; } addBonus(5); echo $total;

Static variables

A static variable inside a function keeps its value between calls. It initializes once and then remembers, which suits counters, caches of expensive results, and one-time setup flags.

function counter(): int { static $n = 0; $n++; return $n; } echo counter(); echo counter();

Pass by value and reference

Scalar arguments pass by value: the function works with a copy, and the caller variable stays unchanged. An & before a parameter passes by reference instead, so assignments inside the function modify the caller variable directly. Use references sparingly and document them, because hidden writes surprise readers.

function bump(int &$n): void { $n++; } $x = 1; bump($x); echo $x;

Next: include and require

Article author: Arthur Isaev

Related articles
Development with PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Install PHP 8.5 on Windows 11
Install PHP 8.5 on Ubuntu
Install PHP 8.5 on Rocky Linux
Install PHP 8.5 on macOS
Install PHP 8.5 on FreeBSD
PHP 8.5 in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: