Skip to content
PHP Logo

How can a PHP variable value be passed from one function to another in a separate script?

To pass a PHP variable value from one function to another in a separate script, you can use session variables. By storing the variable in a session, it can be accessed across different scripts and functions. Simply assign the variable to the session in the first function and retrieve it from the session in the second function.

php
// script1.php
session_start();
$variable = "Hello, World!";
$_SESSION['variable'] = $variable;

// script2.php
session_start();
$variable = $_SESSION['variable'];
echo $variable; // Output: Hello, World!