Example 1 — Listing Local Variables
Inside a function, get_defined_vars() returns only that function's own variables.
<?php
function showVars() {
$name = "Asha";
$age = 25;
$vars = get_defined_vars();
echo implode(", ", array_keys($vars)), PHP_EOL;
}
showVars(); How It Works
- Two local variables, $name and $age, are created inside the function.
- get_defined_vars() collects the variables that exist at that moment into an array.
- array_keys() extracts the variable names, and implode() joins them with commas.
- $vars is not in the list, because it did not exist yet when the function ran.

