I have an issue. I have got a template file (template_body.php) being included in an index.php (include_once("include/template_body.php");)
in template_body.php i have got another file included called header.php (include("header.php"))
index.php -> template_body.php --> header.php
Now, in index.php i have got a boolean login check. But i can only access the boolean value in template_body.php and not in header.php .
Any way to achieve that?
You should take a look on this post answer about variable scope in php:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
The below example is working
index.php
<?php
$test = "test";
include_once("file1.php");
?>
template_body.php
<?php
include("header.php");
?>
header.php
<?php
echo $test;
?>
output
test
So again, from what I can read it's a scope issue.
Regards,
Eric
I found a solution to my question:
If I use
include_once("include/template_body.php");
include_once("include/header.php");
it does not work... if I use
include_once __DIR__ ."include/template_body.php";
include_once __DIR__ ."include/header.php";
it does make the variables accessible.
But I have no explanation for that behaviour, so it would be great if somebody could explain this to me :)
(Both versions make the content appear)
Related
I have a php file
index.php and its url is index.php?var=item
I defined get variable in index.php
in index.php
<?php
require "included.php";
$var=$_GET['var'];
?>
I echoed this variable in my included.php like below
in included.php
<?php
echo $var;
?>
When I launch index.php?var=item, its shows an error that var is not defined in included.php?
How to overcome this? I want to define some variables in index.php from url and do some staff in included file.
like Joachim Martinsen posted as a comment (I don't know why he hasn't just answered), you have to set the $var before including your file:
<?php
$var=$_GET['var'];
require "included.php";
?>
inlcuding in PHP basically just works like concatenating one file out of other files. so your original code would result in:
<?php
echo $var;
$var=$_GET['var'];
?>
which obviously doesn't work because the variable is echoed before it is getting a value assigned.
Any variables set up in your PHP code before including another file would work there. The included file won't be aware of any other variable that was declared or set after it was included.
In index.php, do this:
<?php
if(isset($_GET['var'])) {
$var=$_GET['var'];
}
require "included.php";
?>
Then access the $var value like you are already trying to do in included.php.
And as I am writing this answer, I can see that a similar answer has just been posted. Sorry for posting it anyway, I think it includes a bit more explanation.
I'm looking for a way to access an array defined in an included PHP file, from outside this file. I've been Googling on that for a few hours but I didn't really find anything that answered my question.
I've tried using $GLOBAL['varname'] outside my script after using global $varname in the included file but it doesn't seem to work, got to say I'm a bit confused.
Is there any way I can do this in PHP?
Thanks a lot for the answer!
user $_SESSION variables. so you will be able to access your variables anywhere in your php application.. untill you unset it..
try this..
session_start();
$_SESSION['array'] = $array; //your array
now you can use this variable/array in any file of your project..
print_r($_SESSION['array']);
if you include a file in PHP you can access the variable the same way you would if it was in the same file..
File1 (foo.php):
<?php
$var = array('foo','bar');
File2:
<?php
require_once('foo.php');
print_r($var);
does this not work..?
This question already has answers here:
Passing PHP variables to an included file?
(4 answers)
Closed 8 years ago.
In my script, I choose which page to include based on a variable.
Does the included page receive the variables that are defined in the main page? Or so I have to redefine them?
If so, what's the best way to pass the variables to the included page?
I tried include("page.php?var=".$var)
But it seems that actually tries to include a file with that string name.
Advice?
If you define you variable before include page, you don't need any query string. Your variable will be accessed in the included page with just name. for example
$name = "Awais"
include("page.php");
then in page.php
echo $name; //will print Awais
Variables that are already in scope in the first page are already defined in the second.
You are better off setting the variables themselves in the main page. include tries to include a local file, not a HTTP GET request, but just set the variables anyway and you can use them.
If you define $var = 1 and after that include("page.php"); the variable will be accessible in that file, since it's nothing more then an extension of what you already got.
This ... "include("page.php?var=".$var)" won't work
Instead try the following:
page1.php
<?php
$dog_name = "scruff";
include("otherpage.php");
?>
otherpage.php
<?php
echo $dog_name;
?>
This will output on page1.php:
scruff
As midnightlightning said: "Variables that are already in scope in the first page are already defined in the second."
Does the included page receive the variables that are defined in the main page?
Yes, the code you include is within the same scope. That is also the documented behaviour, see include.
$var = 'value';
include('page.php'); # has $var defined now.
unset($var);
include('page.php'); # has $var undefined now.
So as you can see, there is no need to redfine them.
But you might want to separate that because it has side-effects, see:
Is include()/require() with “side effects” a bad practice?
I'm still kinda new to HTML/PHP and wanted to figure out how to streamline my pages a little bit more. I want to try to pass a variable from the page I include another PHP file on.
For example:
<?php include "quotes.php"; $name='tory'?>
I then want to use this variable name, $name='tory', in my quotes.php file. I'm unsure if I'm going about this the correct way because if I try to use the variable $name in my quotes.php file, it says that "name" is undefined.
Question has been answered. Needed to switch the statements. Thank you all!
Assign $name variable before including other files:
<?php
$name='tory';
include "quotes.php";
?>
Reverse it:
<?php $name='tory'; include "quotes.php"; ?>
You cannot use a variable before it was declared.
In your example, you're including quotes.php before the $name variable declaration, it will never work.
You can do
<?php $name='tory'; include "quotes.php"; ?>
Now, $name exists in "quotes.php"
What you're doing isn't necessarily the best way to go about it, but to solve your specific problem, simply define the variable before including the file. Then the variable will be globally scoped and available in the include file.
<?php
$name = 'tory';
include "quotes.php";
?>
You need to define $name first before including quotes.php.
You have to declare the variable $name before including the file.
<?php
$name = 'tory';
include 'quotes.php';
?>
This makes sense because the file you included will get parsed and executed and then move on with the rest.
The statements are executed in sequence. It is as though you had copy-and-pasted the contents of quotes.php at the point in your script where the include statement is found. So at the point where you execute quotes.php, the statement defining $name has not happened yet. Therefore to get the behaviour you want, you should reverse the order of the two statements.
Ok, maybe my brain is just shut off, but I can't get this to work.
Here's the complete code:
Page1.php:
<?php
$something = "hello";
include "Page2.php";
?>
Page2.php:
<?php
echo $something;
?>
Desired output (when navigating to Page1.php):
hello
The real output is blank. I have tried putting the global keyword everywhere, and nothing happens. Am I missing something?
I cannot replicate this error, just tried this on my localhost and copied and pasted your code from here. I suspect you have some sort of syntax error.
Turn on error reports and see if you get any errors.
I know this is a late answer, but I'm trying to do something similar. First of all, when you echo something you still have to put it in " ". Php will recognize it as a variable as long as you put the $.
Second, you're including page2.php in page1. Fantastic, but page2 does not recognize $something. Now, if you do it the other way, declare $something in page2, and then call it from page 1 after including it, it will run.
Modifying the variable would require something else...
I think the output is coming in page2.php . Am i right? this is because you are echoing a unset variable in page2.php you need to change the following data in order to make it work.
page1.php
<?php
include("page2.php");
echo $something;
?>
page2.php
<?php
$something="Hello";
?>
If you will use it and navigate the page 1.php then the output will be Hello
I had a similar problem running on local (Windows) where the values of an array did not follow past the include within the same process.
After switching the include path from http://localhost/www/example.php to C:/www/example.php, it works fine now.