I have defined a variable $month is "water_sources.php" file. Can I use this variable in another file "add_month.php" If yes then please suggest how?
Simple answer is yes. Take a look at the docs for include
: http://php.net/manual/en/function.include.php, require
: http://php.net/manual/en/function.require.php, and require_once
: http://php.net/manual/en/function.require-once.php. Which you use will depend on whether the file is... you guessed it, required. :)
Luckily, the mechanics for using each are identical. In the sample code below, you could interchange require
or require_once
for include
and the demo will still function correctly. Try to create the files and use each of the functions. Then, delete the vars.php
file and try your tests again. Notice that your script will fail if you use require
and the included file does not exist.
vars.php
<?php
$color = 'green'; $fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
Yes you can. Just use the require
or require_once
commands to pull in the other php file. Like this:
require_once('water_sources.php');
echo "Month is $month\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With