I'm trying to use PHP variables into echoed file, and couldn't get where is a trouble at first using that script:
$head = new mod_head("head.php");
$id="ASDSSgdfsfsdfS";
echo $head;
mod_head class:
class mod_head
{
private $out="";
function __construct($arg)
{
$this->out=$this->parts($arg);
}
public function __toString()
{
return $this->out;
}
private function parts($file)
{
return fread(#fopen(PATH . "parts/".$file, 'r'), filesize(PATH . "parts/".$file));
}
}
and the file is "head.php"
<h1><center style="background:orange; border-radius:15px;">LOGO</center></h1>
<br><?php print_r($id)?>
<div>BANNER <div>$id <?php echo $id ;?></div></div>
i dont want to create global vars, why it doest echo $id var?
First, you're going to need to parse the file, not just read it. The second problem you'll have is a scope issue. $id is outside of the scope of the parts() function. In order to return the contents of the required file instead of just printing it I've used the output control functions
Try changing your parts function to this:
private function parts($file)
{
ob_start();
require(PATH . "parts/".$file);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
To fix the scope issue try changing $id="ASDSSgdfsfsdfS"; to $head->id = "ASDSSgdfsfsdfS";, then change head.php to be the following:
<h1><center style="background:orange; border-radius:15px;">LOGO</center></h1>
<br><?php print_r($this->id)?>
<div>BANNER <div>$id <?php echo $this->id ;?></div></div>
Simply reading a file with fread will not parse any PHP contained inside. Perhaps you are looking for something like:
http://php.net/manual/en/function.require.php
Using require() is basically like copying and pasting the required file directly where your require() statement is. This means that the required file would only be able to use variables that are within the scope of where the require() statement is.
Because you are reading the contents of head.php and echoing them verbatim; nowhere do you make PHP compile and run that file as code. You could do that using include('head.php'), but that would not work blindly because you also have to make sure that $id is in scope at the point you do the include.
However that's not as easy as it sounds because it is not possible to automatically "pack the whole local scope" for later use because automatically implies code inside a function, and the very act of calling that function causes the scope to change.
My thought is that you are probably using mod_head is passing in a php file so it can be used as a view, right?
if thats the case, usually there is also a way to include a variable so that way it exists in the other file, head.php.. since there isn't one, you'd need to create a method for that. and then make that fread an include or require instead.
If not, and you're simply loading the file, the other comments about fread not parsing is totally correct and you will not be able to simply access $id from the other file.
Related
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!
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>
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
Considering that an include statment in php at the most basic level takes the code from a file and pastes it into where you called it and the fact that the manual on include states the following:
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.
These things make me think that there is a diffrent problem alltogether. Also Option number 3 will never work because you're not redirecting to second.php you're just including it and option number 2 is just a weird work around. The most basic example of the include statment in php is:
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
?>
Considering that option number one is the closest to this example (even though more complicated then it should be) and it's not working, its making me think that you made a mistake in the include statement (the wrong path relative to the root or a similar issue).
I have the same problem here, you may use the $GLOBALS array.
$GLOBALS["variable"] = "123";
include ("my.php");
It should also run doing this:
$myvar = "123";
include ("my.php");
....
echo $GLOBALS["myvar"];
Have a nice day.
I've run into this issue where I had a file that sets variables based on the GET parameters. And that file could not updated because it worked correctly on another part of a large content management system. Yet I wanted to run that code via an include file without the parameters actually being in the URL string. The simple solution is you can set the GET variables in first file as you would any other variable.
Instead of:
include "myfile.php?var=apple";
It would be:
$_GET['var'] = 'apple';
include "myfile.php";
OPTION 1 worked for me, in PHP 7, and for sure it does in PHP 5 too. And the global scope declaration is not necessary for the included file for variables access, the included - or "required" - files are part of the script, only be sure you make the "include" AFTER the variable declaration. Maybe you have some misconfiguration with variables global scope in your PHP.ini?
Try in 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;
?>
It should work... if it doesn't just try to reinstall PHP.
In regards to the OP's question, specifically "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)."
This will tell you what file included the file. Place this in the included file.
$includer = debug_backtrace();
echo $includer[0]['file'];
I know this is an old question, but stumbled upon it now and saw nobody mentioned this. so writing it.
The Option one if tweaked like this, it should also work.
The Original
Option One
In the first file:
global $variable;
$variable = "apple";
include('second.php');
In the second file:
echo $variable;
TWEAK
In the first file:
$variable = "apple";
include('second.php');
In the second file:
global $variable;
echo $variable;
According to php docs (see $_SERVER) $_SERVER['PHP_SELF'] is the "filename of the currently executing script".
The INCLUDE statement "includes and evaluates the specified" file and "the code it contains inherits the variable scope of the line on which the include occurs" (see INCLUDE).
I believe $_SERVER['PHP_SELF'] will return the filename of the 1st file, even when used by code in the 'second.php'.
I tested this with the following code and it 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.
An alternative to using $GLOBALS is to store the variable value in $_SESSION before the include, then read it in the included file. Like $GLOBALS, $_SESSION is available from everywhere in the script.
Pass a variable to the include file by setting a $_SESSION variable
e.g.
$_SESSION['status'] = 1;
include 'includefile.php';
// then in the include file read the $_SESSION variable
$status = $_SESSION['status'];
You can execute all in "second.php" adding variable with jQuery
<div id="first"></div>
<script>
$("#first").load("second.php?a=<?=$var?>")
</scrpt>
I found that the include parameter needs to be the entire file path, not a relative path or partial path for this to work.
This worked for me: To wrap the contents of the second file into a function, as follows:
firstFile.php
<?php
include("secondFile.php");
echoFunction("message");
secondFile.php
<?php
function echoFunction($variable)
{
echo $variable;
}
Do this:
$checksum = "my value";
header("Location: recordupdated.php?checksum=$checksum");
Been struggling to make some simple code work, but face a problem with the global/local reach of one variable.
Here is the code I try to get to work. This code is contained in a PHP file called by an AJAX GET procedure from Javascript. None of the GET variables appears in the below chunks of code.
$location = "./Treewindow/tree_structure.xml";
function openXML($url) {
if (file_exists($url)) {
$xml = simplexml_load_file($url);
}
else {
echo("Failed to open XML at ".$url);
exit;
}
}
function cubicleAvailableSpace() {
openXML($location);
}
When I call the last function:
cubicleAvailableSpace();
I get:
Failed to open XML at
Why is the variable $location not recognized in the function cubicleAvailableSpace()?! I thought it would be considered as "visible" from all functions within this PHP code...
Now, I am sure this is easy, but I tried the whole afternoon to make this work... Looked all around the place, but could not find any reply which helped me (though there are many such cases in this website)
Of course, when I replace the variable by its actual value ("./Treewindow/tree_structure.xml"), everything works: the XML file is at the right place :-)
Can you help me find what's wrong and make this $location variable visible in both functions ?
Thanks
try this
function cubicleAvailableSpace() {
global $location;
openXML($location);
}
you declared the variable outside the function so it is not readable inside.
I face a strange problem including php files. Let me show you the code:
// constants.php
$MYSQL_HOST_PORT = 'localhost:3306';
// functions.php
include 'constants.php';
function getVar() {
echo $MYSQL_HOST_PORT;
}
// doSth.php
include 'functions.php';
echo $MYSQL_HOST_PORT; // The variable is visible and echoed normally as expected!
echo getVar(); // The variable is not echoed! its "".
Any ideas ?
For one, the echo in echo getVar(); won't ever print anything, because getVar doesn't return a value.
Secondly, if you (for some reason) want getVar() itself to work correctly, you need to add a global $MYSQL_HOST_PORT; line, to make it look for $MYSQL_HOST_PORT in the global scope.
Rather than globalising the $MYSQL_HOST_PORT variable, why not simply make it a constant?
// constants.php
define('MYSQL_HOST_PORT', 'localhost:3306');
Provided constants.php is included, you can reference the MYSQL_HOST_PORT constant anywhere.
As indicated in zerocrate's answer, the issue is a scoping one. The enclosed scope of the getVar() function does not include $MYSQL_HOST_PORT.
One thing that I can see wrong is that with the line echo getVar(); you are not getting a return value from the function so you can simply write getVar(); by itself.
What is the difference between include() and calling a function in PHP?
For example :
1-
<?php
$foo = '<p>bar</p>';
return $foo;
?>
<html><body><?php echo $foo; ?></body></html>
2-insert above php code in a php file and include()
thanks in advance
include() simply takes the full contents of the file and inserts it in, replacing the include() with the contents of the file.
If you have HTML in the included file, it will be output. If you only have PHP in it, the PHP will be run.
To call a function, the function must be available. If the function is in another file, you will still need to include() or require() that file to have it available.
Generally, including is used to get a set of functions or objects into your running script, so that they can be used, although it can also be used as a standalone page or some bit of HTML, like you posted. In reality, it depends on whether you'd rather have another function on the same script or in a remote script, for aesthetics or organization, whatever your reason.
Functions will usually run a bit faster, as server response time and parsing time may make the include function run a bit slower, but for all intents and purposes you wont notice much. Most of the lag will be due to the fact that a local function will be executed with the page, whereas the include function must execute the page, load another page, and then execute that page as well. If that makes sense.
Just as an addition to the existing answers, you can also do this:
sample.php:
<?php
$foo = include('include_with_return_value.php');
?>
<html><body><?php echo $foo; ?></body></html>
and include_with_return_value.php:
<?php
return '<p>bar</p>';
So, include() files can also have a return value, just like functions.
I have the following dilemma. I have a complex CMS, and this CMS is to be themed by a graphic designer. The templates are plain HTML, with several nested inclusions. I'd like to make it easier for the designer to locate the file to be modified, by looking at the HTML of the page.
What I thought in the first place was to build something stupid like this:
function customInclude($what) {
print("<!-- Including $what -->");
include($what);
print("<!-- End of $what -->");
}
but, guess what? Variables obviously come out of scope in the included file :-) I can't declare them as global or as parameters, as I don't know how they are called and how many are there.
Is there any possibility to implement some kind of "macro expansion" in PHP? An alternative way to call it: I'd like to modify each call of the modify function, in an aspect-oriented style.
I have thought about eval(), is it the only way? Will it have a big impact on performance?
I know this is an old question, but I stumbled upon it and it reminds me of something I used to do it too.
how about if you create the function using a very weird variable?
<?php
function customInclude($___what___) {
echo '<!-- Including '.$___what___.' -->';
include($what);
echo '<!-- End of '.$___what___.' -->';
}
?>
I usually suggest to add a possible variable to display those tags only when necessary, you do not want other people to know...
<?php
function __printIncludeInfo($info, $dump = false){
//print only if the URL contains the parameter ?pii
//You can modify it to print only if coming from a certain IP
if(isset($_GET['pii'])){
if($dump){
var_dump($info);
} else {
echo $info;
}
}
}
function customInclude($___what___) {
__printIncludeInfo('<!-- Including '.$___what___.' -->');
include($what);
__printIncludeInfo('<!-- End of '.$___what___.' -->');
}
?>
in this way you can use the function to print any other information that you need
Not sure if I entirely understand the question, but if you're just trying to make life easier for the designer by showing them the underlying filename of the included file, then you can probably just use this within the template files:
echo '<!-- Start of '.__FILE__.' -->';
....content...
echo '<!-- End of '.__FILE__.' -->';
__FILE__ is just one of several Magic Constants.
Also there's the get_included_files() function that returns an array of all the included files, which might be of use (you could output a list of all the included files with 'tpl' in their name for example).
This is my 100% harcoded solution to custom include problem. It's about using a global var to point the next include filename and then include my custom proxy-include-file (wich replace your custom proxy-include-function)
1 - Add this code to a global include (wherever your customInclude function is defined)
$GLOBALS['next_include'] = "";
$GLOBALS['next_include_is_once'] = false;
function next_include($include_file) {
$GLOBALS['next_include_is_once'] = false;
$GLOBALS['next_include'] = $include_file;
}
function next_include_once($include_file) {
$GLOBALS['next_include_is_once'] = true;
$GLOBALS['next_include'] = $include_file;
}
2 - Create some include proxy-include-file, by example "debug_include.php"
<?php
if(empty($GLOBALS['next_include'])) die("Includes Problem");
// Pre-include code
// ....
if($GLOBALS['next_include_is_once']) {
include_once($GLOBALS['next_include']);
} else {
include($GLOBALS['next_include']);
}
// Post-include code
// ....
$GLOBALS['next_include'] = "";
3 - Perform a search and replace in all your files: (except debug_include.php)
search: 'include((.*));' as a reg.exp
replace with: '{next_include($1);include('debug_include.php');}'
and
search: 'include_once((.*)); as a reg.exp
replace with: '{next_include_once($1);include('debug_include.php');}'
Maybe you should need another search-and-replaces if you have some non-standard includes like
include (.... include (.... include (....
I think you can find some better search-and-replace patterns, but I'm not a regular expression user so I did it the hard way.
You should definitely use objects, namespaces and MVC model. Otherwise there is no pure and clean solution to your problem. And please, don't use eval, it's evil.