how do I get a variable from one file to another in php

html, php

Solution

The issue is that in file b.php, you are not sending the value of age to c.php.

If, in b.php, you did this:

 session_start();
 $_SESSION["age"] = $_POST["age"];

Then, in c.php, you did this:

 session_start();
 $age = $_SESSION["age"];
 echo $age;

It will work properly.

Problem

I have 3 files. I will give you the exact example: a.php ``` <form action="b.php" method="POST"> Enter age: <input type="text" name="age"><br> <input type="submit" value="Save"> </form> ``` b.php ``` <?php $age=$_POST["age"]; if (is_numeric($age)) { header("Location: c.php"); exit(); } else { echo "Age invalid!"; } ?> ``` c.php ``` <?php //i want to use the $age variable here echo $age; ?> ``` How can I use the `$age` variable from `b.php` in `c.php`? I also tried to session_start(); at file `b.php` and use `$_SESSION["age"]=$_POST["age"];` in `b.php` and then `$_SESSION["age"]` in `c.php` in stead of `$age` and it still didn't work. I also tried include but didn't get me anywhere either... maybe I didn't use it correctly.

Original source