Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP pass variable to include

I'm trying to pass a variable into an include file. My host changed PHP version and now whatever solution I try doesn't work.

I think I've tried every option I could find. I'm sure it's the simplest thing!

The variable needs to be set and evaluated from the calling first file (it's actually $_SERVER['PHP_SELF'], and needs to return the path of that file, not the included second.php).

OPTION ONE

In the first file:

global $variable; $variable = "apple"; include('second.php'); 

In the second file:

echo $variable; 

OPTION TWO

In the first file:

function passvariable(){     $variable = "apple";     return $variable; } passvariable(); 

OPTION THREE

$variable = "apple"; include "myfile.php?var=$variable"; // and I tried with http: and full site address too.   $variable = $_GET["var"] echo $variable 

None of these work for me. PHP version is 5.2.16.

What am I missing?

Thanks!

like image 660
user1590646 Avatar asked Aug 10 '12 15:08

user1590646


People also ask

What is include () in PHP?

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. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

How can you pass a variable by reference in PHP?

Pass by reference: When variables are passed by reference, use & (ampersand) symbol need to be added before variable argument. For example: function( &$x ). Scope of both global and function variable becomes global as both variables are defined by same reference.

What does &$ mean in PHP?

It means you pass a reference to the string into the method. All changes done to the string within the method will be reflected also outside that method in your code. See also: PHP's =& operator.

What is the difference between include and include_once in PHP?

The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again.


2 Answers

You can use the extract() function
Drupal use it, in its theme() function.

Here it is a render function with a $variables argument.

function includeWithVariables($filePath, $variables = array(), $print = true) {     $output = NULL;     if(file_exists($filePath)){         // Extract the variables to a local namespace         extract($variables);          // Start output buffering         ob_start();          // Include the template file         include $filePath;          // End buffering and return its contents         $output = ob_get_clean();     }     if ($print) {         print $output;     }     return $output;  } 


./index.php :

includeWithVariables('header.php', array('title' => 'Header Title')); 

./header.php :

<h1><?php echo $title; ?></h1> 
like image 85
thi3rry Avatar answered Sep 21 '22 04:09

thi3rry


Option 3 is impossible - you'd get the rendered output of the .php file, exactly as you would if you hit that url in your browser. If you got raw PHP code instead (as you'd like), then ALL of your site's source code would be exposed, which is generally not a good thing.

Option 2 doesn't make much sense - you'd be hiding the variable in a function, and be subject to PHP's variable scope. You'ld also have to have $var = passvariable() somewhere to get that 'inside' variable to the 'outside', and you're back to square one.

option 1 is the most practical. include() will basically slurp in the specified file and execute it right there, as if the code in the file was literally part of the parent page. It does look like a global variable, which most people here frown on, but by PHP's parsing semantics, these two are identical:

$x = 'foo'; include('bar.php'); 

and

$x = 'foo'; // contents of bar.php pasted here 
like image 27
Marc B Avatar answered Sep 22 '22 04:09

Marc B