PHP pass variable to enable

I am trying to pass a variable to an include file. My host has changed the version of PHP, and now any solution I'm trying to make does not work.

I think I tried everything I could find. I am sure this is the easiest thing!

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

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 them work for me. The PHP version is 5.2.16.

What am I missing?

Thank!

+66
variables include php global-variables global
Aug 10 '12 at 15:50
source share
12 answers

Option 3 is not possible - you will get the result of a .php file in the same way as if you typed this URL into your browser. If instead you received raw PHP code (as you wish), then ALL the source code of your site will be displayed, which is usually not very good.

Option 2 doesn't make much sense - you would hide the variable in the function and possess the scope of the PHP variable. You must also have $var = passvariable() somewhere to get this "internal" variable "outside" and you will return to the square.

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. This is similar to a global variable that most people frown here, but with the help of PHP parsing semantics, the two are identical:

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

and

 $x = 'foo'; // contents of bar.php pasted here 
+39
Aug 10 2018-12-12T00:
source share

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

Here it is a rendering function with the argument $variables .

 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> 
+29
Aug 08 '17 at 8:48 on
source share

Given that the included statment in php at the most basic level takes the code from the file and inserts it where you called it, and the fact that the on manual includes the following:

When the file is included, the code that it contains inherits the scope of the line in which the inclusion occurs. Any variables available on this line in the calling file will be available in the called file from this point forward.

These things make me think that there is a diverse problem. Also option number 3 will never work, because you are not redirected to second.php , you just turn it on, and option number 2 is just a weird job. The simplest example of including stat in php:

 vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> 

Given that parameter number one is closest to this example (although more complicated than it should be) and it doesn't work, it makes me think that you made a mistake in the include statement (wrong path relative to the root or a similar problem) .

+9
Aug 10 '12 at 15:59
source share

I have the same problem, you can use the $ GLOBALS array.

 $GLOBALS["variable"] = "123"; include ("my.php"); 

It should also run this:

 $myvar = "123"; include ("my.php"); .... echo $GLOBALS["myvar"]; 

A good day.

+5
Nov 24 '15 at 9:17
source share

Regarding the OP question, in particular: "This variable must be set and evaluated from the first calling file (it is actually" $ _SERVER ['PHP_SELF "], and it needs to return the path to this file, and not including second.php)."

This will tell you which file is included in the file. Put this in the included file.

 $includer = debug_backtrace(); echo $includer[0]['file']; 
+4
Aug 10 '12 at 15:57
source share

According to php docs (see $ _ SERVER ) $ _SERVER ['PHP_SELF'] is the file name of the current executable script.

The INCLUDE statement includes and evaluates the specified β€œfile” and β€œthe code contained in it inherits the region of the variable of the line on which the inclusion occurs” (see INCLUDE ).

I believe that $ _SERVER ['PHP_SELF'] will return the file name of the 1st file, even if it is used by the code in 'second.php'.

I tested this with the following code and works as expected ($ phpSelf is the name of the first file).

 // In the first.php file // get the value of $_SERVER['PHP_SELF'] for the 1st file $phpSelf = $_SERVER['PHP_SELF']; // include the second file // This slurps in the contents of second.php include_once('second.php'); // execute $phpSelf = $_SERVER['PHP_SELF']; in the secod.php file // echo the value of $_SERVER['PHP_SELF'] of fist file echo $phpSelf; // This echos the name of the First.php file. 
0
Jul 08 '14 at 18:06
source share

I ran into this problem when I had a file that sets variables based on GET parameters. And this file could not be updated because it worked correctly in another part of the large content management system. However, I wanted to run this code through an include file with no parameters that really are in the URL string. A simple solution - you can set the GET variables in the first file, like any other variable.

Instead:

 include "myfile.php?var=apple"; 

It would be:

 $_GET['var'] = 'apple'; include "myfile.php"; 
0
Jan 11 '18 at 19:20
source share

OPTION 1 worked for me in PHP 7, and it certainly works in PHP 5. And the declaration of a global scope is not required for the included file to access the variables, the included - or "required" - files are part of the script, just make sure that you included "AFTER" after the variable declaration. Maybe you have some kind of incorrect configuration with a global variable in your PHP.ini?

Try in the first file:

  <?php $myvariable="from first file"; include ("./mysecondfile.php"); // in same folder as first file LOLL ?> 

mysecondfile.php

  <?php echo "this is my variable ". $myvariable; ?> 

This should work ... if it is not just trying to reinstall PHP.

0
Jun 22 '19 at 23:23
source share

I found that the include parameter should be the whole file path, not the relative or partial path for this.

-one
Jul 21 '16 at 19:23
source share

This worked for me: To wrap the contents of the second file in a function, follow these steps:

firstFile.php

 <?php include("secondFile.php"); echoFunction("message"); 

secondFile.php

 <?php function echoFunction($variable) { echo $variable; } 
-one
Nov 19 '17 at 17:07 on
source share

PHP 5.2.0 adds the allow-url-include directive (default is 0 , you must change it to 1 in php.ini ), which means that you can pass the full http: URL to the include statement.

By adding your variable as a query string to this URL, you can pass the variable to the included file, albeit with some performance loss:

 include 'http://www.myservername.com/includes/second.php?variable=apple'; 

Of course, you also need to configure second.php to handle $_GET['variable'] and apply it to the output.

-3
Jul 08
source share

Do it:

 $checksum = "my value"; header("Location: recordupdated.php?checksum=$checksum"); 
-3
Feb 27 '15 at 0:18
source share



All Articles