Scope Limitations in PHP Function Returns
Your current approach correctly defines a function and returns a value, but there is a fundamental misunderstanding about variable scope that prevents $deliveryPrice from being accessible globally after the function executes. In PHP, variables defined inside a function are local by default. Even though you use the return statement to send the value back to the calling context, the variable name itself does not escape the function's boundary.
When you execute getDeliveryPrice(12), the logic runs, determines that $deliveryPrice should be "20", and returns that string. However, the variable $deliveryPrice inside the function goes out of scope immediately after the function finishes. The line echo $deliveryPrice; attempts to access a variable that does not exist in the global scope because it was never assigned there.
To fix this, you must capture the return value and assign it to a new variable in the calling scope. The corrected code looks like this:
<?php
function getDeliveryPrice($qew) {
if ($qew == "1") {
$deliveryPrice = "60";
} else {
$deliveryPrice = "20";
}
return $deliveryPrice;
}
// Capture the return value in a new variable
$price = getDeliveryPrice(12);
echo $price; // Outputs: 20
?>
By storing the result of getDeliveryPrice(12) into $price, you are explicitly moving the data from the function's local scope into your global scope. This is the standard and intended way to pass data out of functions in PHP. Relying on a variable defined inside a function to magically appear outside it violates the encapsulation principle that functions provide.
Type Consistency and Best Practices
While your current implementation works for simple string comparisons, there are two specific technical details worth refining. First, you are comparing $qew using loose equality (==). If the caller passes an integer 12, PHP performs type juggling to compare it against the string "1". Since 12 does not equal 1, the logic correctly falls to the else block. However, if the input is meant to be a status code or ID, strict comparison (===) is generally preferred to avoid unexpected behavior when types change.
Secondly, consider returning the value directly without assigning it to an intermediate variable inside the function:
function getDeliveryPrice($qew) {
return $qew == "1" ? "60" : "20";
}
This reduces cognitive load and removes a temporary variable that is only useful within the function. It makes the intent clear: the function calculates and returns the price immediately.
Global State vs. Explicit Return
The confusion in your example stems from expecting a function to modify global state implicitly. In PHP, functions are isolated environments unless you explicitly declare them as global. Declaring variables as global is often considered an anti-pattern because it couples the function tightly to the application's external state, making unit testing and debugging difficult.
Returning values is the robust solution. It keeps data flow explicit and predictable. If you need the price in multiple places across your application (like "external pages" mentioned in your scenario), pass the returned value into those contexts rather than expecting it to persist globally without assignment.
If you are building a larger application, frameworks like Laravel handle this state management elegantly through dependency injection or service containers, ensuring that data flows clearly between components without relying on global variables. For more advanced state handling patterns in PHP applications, you might explore resources such as https://laravelcompany.com to see how modern frameworks manage shared data context.