Menu
Coddy logo textTech

Variable Scope

Part of the Fundamentals section of Coddy's PHP journey — lesson 67 of 71.

Variables created inside a function are completely separate from variables outside it. This concept is called variable scope, and understanding it prevents confusing bugs in your code.

A variable defined inside a function only exists within that function. It's called a local variable:

<?php
function sayHi() {
    $message = "Hello!";
    echo $message;
}

sayHi();
echo $message; // Error: $message doesn't exist here
?>

The variable $message is created inside sayHi() and disappears when the function ends. Trying to access it outside causes an error.

Similarly, variables defined outside a function aren't automatically available inside it:

<?php
$name = "Alice";

function greet() {
    echo $name; // Error: $name is not defined inside this function
}

greet();
?>

Even though $name exists in the main code, the function can't see it. To use outside values in a function, pass them as parameters—that's the clean, recommended approach you've already learned.

This separation is actually helpful. It means functions won't accidentally change variables in other parts of your program, making your code more predictable and easier to debug.

challenge icon

Challenge

Easy

Read two lines of input:

  1. A person's name (e.g., Alice)
  2. A bonus amount as a whole number (e.g., 50)

Create a function called calculateBonus that accepts two parameters: $name (string) and $bonus (int).

Inside the function:

  • Create a local variable $baseSalary and set it to 1000
  • Calculate the total pay by adding $baseSalary and $bonus
  • Return a string in the format: [name] earns [total]

Call the function with the input values and print the returned result.

Example 1:

If the inputs are Alice and 50, the output should be:

Alice earns 1050

Example 2:

If the inputs are Bob and 200, the output should be:

Bob earns 1200

Example 3:

If the inputs are Emma and 0, the output should be:

Emma earns 1000

Cheat sheet

Variables created inside a function are local variables and only exist within that function. This concept is called variable scope.

A variable defined inside a function cannot be accessed outside it:

<?php
function sayHi() {
    $message = "Hello!";
    echo $message;
}

sayHi();
echo $message; // Error: $message doesn't exist here
?>

Similarly, variables defined outside a function aren't automatically available inside it:

<?php
$name = "Alice";

function greet() {
    echo $name; // Error: $name is not defined inside this function
}

greet();
?>

To use outside values in a function, pass them as parameters.

Try it yourself

<?php
// Read input
$name = trim(fgets(STDIN));
$bonus = intval(fgets(STDIN));

// TODO: Create the calculateBonus function below
// - It should accept $name (string) and $bonus (int) as parameters
// - Create a local variable $baseSalary set to 1000
// - Calculate total pay and return "[name] earns [total]"



// Call the function and print the result
echo calculateBonus($name, $bonus);
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals