PHP require/include file - php

I have the following program Structure:
Root directory:-----index.php
|-------TPL directory
|-------------header.php
|-------------index.php
|-------------footer.php
php file loading structure:
Root directory:index.php ----require/include--->tpl:index.php
|----require/include-->header.php
|----require/include-->footer.php
Root index.php :
<?php
function get_header(){
require('./tpl/header.php');
}
function get_footer(){
require('./tpl/footer.php');
}
$a = "I am a variable";
require('./tpl/index.php');
TPL:index.php:
<?php
get_header();//when I change require('./tpl/header.php'); variable $a can work!!
echo '<br />';
echo 'I am tpl index.php';
echo $a;
echo '<br />';
get_footer();//when I change require('./tpl/footer.php'); variable $a can work!!
TPL:header.php:
<?php
echo 'I am header.php';
echo $a;//can not echo $a variable
echo '<br/>';
TPL:footer.php:
<?php
echo '<br />';
echo $a;//can not echo $a variable
echo 'I am footer';
For some reason when I used function to require header.php and footer.php my variable $a doesn't get echoed. It works fine if I use header.php or footer.php on its own. I do not understand what the problem is. What do you think is the issue here?

Your issue is probably just scope of a function does not automatically include global variables. Try setting $a to global like global $a;, example below for your get_header() function:
function get_header(){
global $a; // Sets $a to global scope
require('./tpl/header.php');
}

Using the include inside a function includes the code directly in that function. Therefore the variable is a locally in that function accessable variable - it is not set outside the function.
If you require/include without a function $a becomes a global variable.

make your variable As Global
global $YourVar;

Related

PHP templating and variable scope

i am trying to find out a way to pass non global variables to included document.
page1.php
function foo()
{
$tst =1;
include "page2.php";
}
page2.php
echo $tst;
How can i make that variable be visible ? and how would i do this php templating so i can split header body and footer for html pages. like in a wordpress it has custom wp functions but i dont see them declaring external files to use them.
big thanks in advance.
I think you are not exactly understanding what is going on. Page 1 should probably be doing the echoing. So you include page 2 and the foo function is now available. You need to call it so that it actually executes. Use the global keyword to bring a global variable into the function scope. Then you can echo it.
page1:
include "page2.php";
foo();
echo $test;
page 2:
function foo()
{
global $test;
$test =1;
}
Variables in a function are not seen outside of them when they are not global. But an include in a function should be seen inside the second file.
$test="Big thing";
echo "before testFoo=".$test;
// now call the function testFoo();
testFoo();
echo "after testFoo=".$test;
Result : *after testFoo=Big thing*
function testFoo(){
// the varuiable $test is not known in the function as it's not global
echo "in testFoo before modification =".$test;
// Result :*Notice: Undefined variable: test in test.php
// in testFoo before modification =*
// now inside the function define a variable test.
$test="Tooo Big thing";
echo "in testFoo before include =".$test;
// Result :*in testFoo before include =Tooo Big thing*
// now including the file test2.php
include('test2.php');
echo "in testFoo after include =".$test;
// we are still in the function testFoo() so we can see the result of test2.php
// Result :in testFoo after include =small thing
}
in test2.php
echo $test;
/* Result : Tooo Big thing
as we are still in testFoo() we know $test
now modify $test
*/
$test = "small thing";
I hope that made the things more clear.

Am I using these global variables correctly?

I'm a newbie to php, and I'm in the process of hacking through some moodle code for a prototype.
Anyhows I have some data Id like passed from file1.php to file2.php, for which Im using global variables. The values are intialized in file2 and I need them for use in file1. Here's how I go about it
file1.php
<?php// top of file 1
global $content; // discussion content // line 3
file2.php
global $content;// line 3379
$content=$post->subject;
Back in file1.php
echo 'global scope'.$content; // this is always empty// line 168
Am I missing something here?
Attached files file1.php and file2.php
You use global to get a global variable
//file 1
$a = "im a global variable";
function foo(){
global $a;
echo $a;
}
//file 2
require "file1.php";
function foo2(){
global $a;
echo $a;
}
Just sharing a thought,
suppose in file1, you have $content = "myID";
and in file2, if you include("file1.php"), you can access by doing,
global $content;
var_dump($content) will output string 'myId' (length=4)

How can I pass the currently defined variables into an included file by a function

I am trying to include a file using a function, and I have several variables defined. I want to access the included file to access the variables, but because I am including it using a function, it is not accessible. The sample scenerio is as follows:
i.e the contents of index is as follows
index.php
<?
...
function include_a_file($num)
{
if($num == 34)
include "test.php";
else
include "another.php"
}
...
$greeting = "Hello";
include_a_file(3);
...
?>
And the contents of test.php is as follows
test.php
<?
echo $greeting;
?>
The test file is throwing a warning saying the $greeting is not defined.
This will not work. include and require act as if the code you're including was literally part of the file at the point the include/require was executed. As such, your external files are going to be in the scope of the include_a_file() function, which means $greeting is OUT OF SCOPE within that function.
You'll have to either pass it in as a parameter, or make it global within the function:
function include_a_file($num, $var) {
^^^^-option #1
global $greeting; // option #2
}
$greeting = 'hello';
include_a_file(3, $greeting);
Are you sure your correctly including? And remember PHP is case sensitive:
$Test = "String";
$TEst = "String";
Both completely different variables..
Furthermore, don't just echo out a variable, wrap it within in a isset condition:
if (isset($greeting)){
echo $greeting;
} // Will only echo if the variable has been properly set..
Or you could use:
if (isset($greeting)){
echo $greeting;
}else{
echo "Default Greeting";
}

Php, global variables on two external scripts

How would I pass a global var in two external scripts?
<div>
<!-- INCLUDEPHP 1.php -->
</div>
<div>
<!-- INCLUDEPHP 2.php -->
</div>
I have tried creating global variables on 1.php and `2.phpp but it didn't work.
1.php:
<?php
global $someVar;
$sql = ...;
$someVar= $db -> sql_query($sql);
?>
2.php:
<?php
global $someVar;
echo "$someVar";
?>
Am I doing something wrong?
I would try including the scripts via PHP:
<div>
<?php require "1.php" ?>
</div>
<div>
<?php require "2.php" ?>
</div>
If both includes are loaded into the same page, and the variables exist already in the global scope, all your functions can access them with the global statement. Since everything is already global, the statement is not required in the global scope, only inside functions. This also permits functions to share variables by casting them onto the global scope.
There are many dangers to this, though, which I'll not pretend to be fully aware of, so one is best advised to make prudent use of the global scope in large complex applications as they can become very volatile if naming conventions are relaxed.
Basically, we're looking at,
function arrow() { global $a; $a = "arrow"; return $a; }
function sky() { global $b; $b = "sky"; return $b; }
echo "I shot an " . arrow() . " into the " . sky() . ".";
echo "I shot an $a into the $b.";
which is child's play, it demonstrates how exposed the variables are, sitting out there with no protection. Now another function can come along and blow the whole thing apart:
function whammo() { global $a, $b; $c = $a; $a = $b; $b = $c;}
echo "I shot an " . arrow() . " into the " . sky() . ".";
whammo();
echo "I shot an $a into the $b.";
See what I mean?
Your solution probably lies in a closure of some sort, wherein all the functions are contained that need this 'global'. It will be much better protected.

Including file A in file B, vs. vice-versa

What are the differences in variable and function scoping, between the "includer" and the "includee"?
For example, these two tests work identically, but are there scoping subtleties I should know about?
Test 1:
File "one.php":
<?php
$a = 5;
include("two.php");
?>
File "two.php:
<?php
function f($x) { return $x * 2; }
echo f($a);
?>
Test 2:
File "one.php":
<?php
$a = 5;
?>
File "two.php:
<?php
include("one.php");
function f($x) { return $x * 2; }
echo f($a);
?>
When you execute a PHP file, it starts off in the global scope. The include documentation states;
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.
Since when you include the second file you're in both cases in global scope, the variable scope will stay global and everything else included will always have global scope. In other words, everything in both files and in both cases ends up in global scope and there is no difference in scoping between the two.
When you are including a file you can think of it like a single file ie:
Test 1 as a single file:
<?php
// execute commands in this file
$a = 5;
// then continue execution of include("two.php");
function f($x) { return $x * 2; }
echo f($a);
?>
Test 2 as a single file:
<?php
// first include include("one.php");
$a = 5;
// then continue execution of two.php
function f($x) { return $x * 2; }
echo f($a);
?>
Hope you can understand the difference between the two. In essence it is similar to copying and then pasting the included commands of the include file.
Variables and function scope is as if it were the same file as long as the included file precedes the variable or function call in the "includee"

Categories