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.
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 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.
I am trying to add some variables at the end of an "include". Is this possible?
Here is the code that DOES NOT WORK:
include('login.php?' . "message=used");
You don't need to include a querystring. Includes/requires essentially put the included code in place. Just declare whatever variables the includes needs before you include the file:
$message = 'used';
include('login.php);
No it's not, and you really have no reason to do so, you see. Every variable you define before the include will be available from the include.
$var = "Hello include";
include("login.php");
//Include.php
echo $var;
Will output "Hello include"
No, the include() takes the parameter as a filename, so when you use login.php?message=used it's looking for login.php?message=used.php, which obviously doesn't exist.
You could alter the .ini file setting allow_url_include but this poses a potential security issue, but otherwise just declare your variables before including the file.
On one of my pages I have a require_once('../path/to/url/page.php'); which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test'); it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?
Thanks,
Ryan
By using require_once('../path/to/url/page.php?var=test');, php will not make a new request to page.php, it will actually search for the file named page.php?var=test and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test" and it will be available for use in that script.
require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.
If you need to pass data into the file, you can simply define a standard php variable.
Example
<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>
Note, the variable must be set before the include/require is called, otherwise it won't be available.
All answes true. But most importantly: since $_GET is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.
require only accepts paths it would be pointless to add any request since it doesn't make any
it simple includes the required code into the current one
I need to use include Function with variable.
but,when I try to do it I faced some errors .
Code :
$year=$_POST['year'];
$month=$_POST['month'];
$day=$_POST['day'];
include "Event.php?year=".$year."&month=".$month."&day=".$day;
so,can U help me ? : )
When you include files, you can't pass them any arguments. However they DO inherit any variables in the current scope (global or method).
I'm guessing at what your code does, but I assume you can just do this:
include "Event.php";
You dont need to use that kind of sintax.
The $year, $month and $day variables will be perfectly visible and accessible on the Event.php file.
To get along with what you want, check PHP manual on http://php.net/manual/en/function.include.php to see some examples.
I'm not sure what you're trying to achieve, but I have a couple of guesses handy.
1 - You are trying to redirect the user, with those parameters appended to the url, in which case you will probably need to utilise header:
header("Location: http://example.com/Event.php?year=$year&month=$month&day=$day");
2 - You are trying to capture the output of that page given the presence of certain GET parameters, in which case you can utilise file_get_contents:
$output = file_get_contents("http://localhost/Event.php?year=$year&month=$month&day=$day");
Include the Event.php directly and access the $_POST or $_GET variables from there.
include('Event.php');
// Event.php:
echo $_POST['year']; // works