Pass and print a $_GET variable through an included file - php

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.

Related

Php Variable doesnt reach included in included file

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)

How to use a variable from another php file

I know similar questions have been asked, but unfortunately I didn't manage to solve the problem after going through them.
Assuming this situation: In one.php I'm retrieving some data from an input field and saving it as a variable and later on I require two.php
one.php
$rejon = $_POST['rejon'];
require 'two.php';
two.php
--here I would like to use $rejon ---
When I try to use $rejon in two.php, it doesn't work (I try to insert it into a database to be exact). On the other hand, if I don't require the two.php but instead paste the code into one.php, it works.
I don't understand why this happens. W3schools claims that "The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement." - http://www.w3schools.com/php/php_includes.asp
but it doesn't seem to work like a copy as manualy copying gives other results (variable $rejon is accesible).
1) What exactly does require do and what are its limitations?
2) Most importantly - how do I retrieve that $rejon variable in two.php?
Works for me:
$ cat one.php
<?php
$rejon = 'this value came from POST';
require 'two.php';
$ cat two.php
<?php
echo $rejon . PHP_EOL;
$ php one.php
this value came from POST
The W3Schools description is, unsurprisingly, misleading. Nothing is copied. The contents of the file are read, evaluated, and inserted at the point of the require. With respect to variables, the PHP manual says this:
When a file is included, the code it contains inherits the variable
scope of the line on which the include occurs. Any variables available
at that line in the calling file will be available within the called
file, from that point forward. However, all functions and classes
defined in the included file have the global scope.
Which is why my example above works as it does.
To diagnose this issue, continue to simplify your code to reduce it to the simplest possible example. If you're trying to use $rejon in a function inside of two, then you need to make the variable global. Like:
global $rejon;
$rejon = $_POST['rejon'];
// now you have $GLOBALS['rejon'] everywhere
Side note, $_SESSION should not be necessary unless you're crossing a page refresh boundary.
You can do this by the following two ways
1.Using Session
You can store the data in the session and than you can use it in the another file two.php
one.php
<?php
session_start();
$rejon = $_POST['rejon'];
$_SESSION['rejon'] = $rejon;
?>
two.php
<?php
session_start();
echo $_SESSION['rejon'];
?>
2. Including that file
I have found that you are including the file two.php in your script. If you are including this file than you can directly use the variable $rejon
two.php
<?php
echo $rejon;
?>
You can use sessions http://php.net/manual/en/book.session.php or a constant http://php.net/manual/en/language.constants.php.
Utkarsh already provided some examples for sessions.
To define a constant you use the define() function.
define('CONSTANT_NAME', CONSTANT_VALUE);
As long as your constant is defined and included (if in another file) before you use it you can then call CONSTANT_NAME in your code to get the value.

Included pages receive variables? [duplicate]

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?

HTML/PHP Variable passing

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.

PHP global variables across files

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.

Categories