Passing a variable from one php include file to another: global vs. not
The parent file has access to variables in both included files
When including files in PHP, it acts like the code exists within the file they are being included from. Imagine copy and pasting the code from within each of your included files directly into your index.php
. That is how PHP works with includes.
So, in your example, since you've set a variable called $name
in your front.inc
file, and then included both front.inc
and end.inc
in your index.php
, you will be able to echo
the variable $name
anywhere after the include
of front.inc
within your index.php
. Again, PHP processes your index.php
as if the code from the two files you are including are part of the file.
The included file doesn't have access to the other included file
When you place an echo
within an included file, to a variable that is not defined within itself, you're not going to get a result because it is treated separately then any other included file.
In other words, to do the behavior you're expecting, you will need to define it as a global.
Passing variables to included file for use in another file
a.php
$b = "this_from_a";
include("t1.php");
t1.php
echo "<a href=t2.php?var=".$b.">link</a>";
t2.php
echo $_GET['var'];
OUTPUT:
this_from_a
WAY2:
Use session.
PHP, getting variable from another php-file
You can, but the variable in your last include will overwrite the variable in your first one:
myfile.php
$var = 'test';
mysecondfile.php
$var = 'tester';
test.php
include 'myfile.php';
echo $var;
include 'mysecondfile.php';
echo $var;
Output:
test
tester
I suggest using different variable names.
Passing variables from one file using include and to another file using href functions
when using an if statement, make sure you predefine the variable so you know if things go wrong
$myDate = 'bad stuff happened'; //Example.
$someVar = true; //Indicates a true value for the if in this example.
if ($someVar) {
$myDate = new Date('Y-m-d'); //Today.
}
when passing this as $_GET
parameters in a link, you can get a hold of these by using the describing word before the '=' sign, e.g. in index.php, if you want to call the date, you would have to call $myDate
, however if you are in file 2, and lets say your url looks like this inside the link:
<a href="www.mydomain.com/?todayis=<?=$myDate?>&category=<?=$category?>" />click</a>
on the next page, you can access the date by using $_GET['todayis']
since that is the name of the $_GET
variable.
If none of these show up, you know for sure it's not getting passed to your next page.
Passing variables in PHP from one file to another
Include the file you would like the variable to be accessible within, like so
include('somefile.php')
and at the top of that file you might need put something like [depending on server configurations]
global $pubname
But in most cases you would not need to do this.
In regards to security, depending on how $pubname
is set, your query may or may not be prone to sql injection.
Note: There are other means to include()
files such as include_once()
, require()
and require_once()
, from php.net:
The documentation below also applies
to require(). The two constructs are
identical in every way except how they
handle failure. include() produces a
Warning while require() results in a
Fatal Error. In other words, use
require() if you want a missing file
to halt processing of the page.
include() does not behave this way,
the script will continue regardless.
Be sure to have an appropriate
include_path setting as well. Be
warned that parse error in required
file doesn't cause processing halting
in PHP versions prior to PHP 4.3.5.
Since this version, it does.
Not able to pass variable from different php file
What tim said is correct, one other approach without implementing OOP is using the superglobal $GLOBAL
ex.$GLOBALS['link']
reference: https://www.php.net/manual/en/reserved.variables.globals.php
Passing Variable FROM Included PHP File To Parent Script
Make sure you global $variable
the variable everytime you want to use it in a new function, or even within a new script. This will make sure that the variable is available everywhere that you need it.
Related Topics
Simplest Way to Increment a Date in PHP
PHP File Listing Multiple File Extensions
Can't Send Email to Addresses at My Own Domain
Storing Form Data as a Session Variable
How to Use Open Id as Login System
How to Increment Letters Like Numbers in PHP
Upload 1Gb Files Using Chunking in PHP
Laravel App Stopped Working After Upgrading to PHP 8
Convert All Types of Smart Quotes with PHP
Trouble Using Posix_Kill in PHP
Run Shell Command and Send Output to File
Send Cookie with File_Get_Contents
How to Make Number_Format() Not to Round Numbers Up
The Post Method Is Not Supported for This Route. Supported Methods: Get, Head. Laravel
Display the Discounted Percentage Near Sale Price in Single Product Pages for Wc 3.0+