Issue: https://github.com/php/php-src/issues/11760
Author: cmanley
### Description
Because most PHP developers are familiar with javascript, see for example let and const in ES6:
https://www.geeksforgeeks.org/what-are-block-scoped-variables-and-functions-in-es6/
Even Perl has supported this for ages. Just in case it's not obvious what the advantages are to
those who have not programmed in any other language besides PHP and Pascal from the 80's, I
asked ChatGPT to list them in order to save time, so here goes.
Block-scoped variables are variables that are limited in scope to a specific block of code, such as
within a loop, condition, or function. Implementing block-scoped variables in PHP could bring
several advantages:
**Improved Code Clarity**: Block-scoped variables allow developers to limit the scope of variables
to specific blocks of code. This can make the code more readable and maintainable as variables are
only accessible where they are needed, reducing potential confusion and bugs.
**Avoiding Variable Pollution**: In PHP, variables declared within a function have function scope,
meaning they are accessible throughout the entire function. If a large function contains many nested
blocks, the risk of variable name clashes and unintended variable overwriting increases.
Block-scoped variables can mitigate this issue by limiting the variable's visibility to a
specific block.
**Encouraging Best Practices**: Introducing block-scoped variables can encourage developers to
follow best practices, such as declaring variables as close to their usage as possible. This
practice helps minimize the potential for errors and aids in understanding the intent of the code.
**Memory Management**: PHP has an automatic garbage collection mechanism, and block-scoped variables
can help manage memory more efficiently. When a block of code finishes execution, the variables
declared within it can be garbage-collected sooner, freeing up memory for other operations.
**Closures and Callbacks**: PHP has closures, anonymous functions that can be assigned to variables.
Block-scoped variables could make it easier to manage variables within closures, potentially leading
to cleaner and more concise code.
**Compatibility with Modern PHP Practices**: Many modern programming languages support block-scoped
variables, and developers familiar with these languages might find it more intuitive to have this
feature in PHP as well. This can improve the overall development experience and attract developers
from other language backgrounds to work with PHP.