0

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?

1
  • Use the $_SESSION, Luke... Commented Aug 13, 2014 at 12:59

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It worked with the globals, big big thnx! I'll go through your explanation when I have a moment, but it sure seems like it will help me understand it better.
1

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;
    ?>

1 Comment

It seems that this answer would work for me, but I prefer to use globals as I'm not refreshing the page. Thnx anyway, up vote!
0

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.

1 Comment

Please provide some examples rather than a some links alone.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.