Explain Dynamic Variables and Variable Scope in PHP

PHP provides powerful features for handling variables in flexible ways. Two important concepts are dynamic variables (also called variable variables) and variable scope.
These features allow you to control how variables behave, how they are accessed, and how they are created dynamically during runtime.


Dynamic Variables in PHP

Dynamic variables, also known as variable variables, allow you to use the value of one variable as the name of another variable.
This feature is unique and powerful but should be used carefully to avoid confusion.


How Dynamic Variables Work

A dynamic variable is created when you prefix a variable with another dollar sign:

$var = "name";
$name = "Savanka";

// Dynamic variable
echo $$var; // Outputs: Savanka

Here’s what happens:

  • $var contains the string "name"
  • $$var becomes $name
  • $name contains "Savanka"

When to Use Dynamic Variables

✔ Useful in situations like:

  • Creating variable names dynamically in loops
  • Working with form fields that have dynamic names
  • Mapping array keys to variable names

❌ Avoid using them when:

  • Code readability will suffer
  • Arrays or objects can solve the problem more cleanly

Example: Dynamic Variable Creation in a Loop

for ($i = 1; $i <= 3; $i++) {
    $varName = "item$i";
    $$varName = "Value $i";
}

echo $item1; // Value 1
echo $item2; // Value 2
echo $item3; // Value 3

Variable Scope in PHP

Variable scope determines where a variable can be accessed within a script.
PHP supports four main scopes:

  1. Local scope
  2. Global scope
  3. Static scope
  4. Function parameter scope

1. Local Scope

Variables declared inside a function only exist within that function.

function test() {
    $x = 10; // Local variable
    echo $x;
}

test();
// echo $x; // Error: undefined variable

2. Global Scope

Variables declared outside functions have a global scope.
You cannot access them directly inside a function unless you use the global keyword or $GLOBALS array.

Using global

$site = "Savanka";

function showSite() {
    global $site;
    echo $site;
}

showSite();

Using $GLOBALS

$version = "PHP 8";

function displayVersion() {
    echo $GLOBALS['version'];
}

displayVersion();

3. Static Scope

A static variable inside a function keeps its value between function calls.

function counter() {
    static $count = 0;
    $count++;
    echo $count . "<br>";
}

counter(); // 1
counter(); // 2
counter(); // 3

The variable is initialized once and retains its value.


4. Parameter Scope

Function parameters are also local variables and only accessible within the function.

function greet($name) {
    echo "Hello, $name!";
}

greet("Sagar");  

Best Practices

  • Use dynamic variables sparingly; arrays or objects are usually better.
  • Prefer using $GLOBALS carefully to avoid unexpected overrides.
  • Use static variables for counters or caching inside functions.
  • Avoid polluting global scope; encapsulate logic inside functions or classes.

External Reference:
🔗 https://www.php.net/manual/en/

View Other Articles About PHP:
🔗 http://savanka.com/category/learn/php/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *