PHP include and require losing variables - php

I have the following two files. The first a long php script, the second just a handful of variables:
File1.php:
<?php
...
function abc()
{
...
include "File2.php";
echo "$x $y $z";
...
}
?>
File2.php:
<?php
$x=1;
$y=2;
$z=3;
?>
None of the variables appear inside my function in the echo statement - using xdebug I step through it including the second file, each variable appears correct, but when it leaves File2.php, it loses all the variables. Why would that be?
I've stripped the first file to get rid of everything but the above - and it works fine. I'm not sure where to start looking in the thousands of lines of code for something that could be causing this problem to happen.
I've even tried using require/require_once - same problem...
Has anyone come across this before or have any thoughts how to debug it?

File2.php
<?php
$x=1;
$y=2;
$z=3;
?>
an if you need to call file2.php inside file1.php, you have to use include() function as well
Correct way of using it
include ('File2.php'); //correct
include 'File2.php'; //correct
include File2.php; //wrong
File1.php
<?php
function abc()
{
include ('File2.php');
echo "$x $y $z";
}
?>

Try out this :-
File1.php:
<?php
include ('File2.php');
echo $x;
echo '<br>';
echo $y;
echo '<br>';
echo $z;
?>
File2.php
<?php
$x=1;
$y=2;
$z=3;
?>

Related

Stuck with php function not returning any data

I am expecting the below code to return hi when the project is run.But it's not returning anything .There is no error also.I've started the wamp server. How can i fix this?I'm very new to this. Anyone can tell me this?
functions.php
<?php
function getDate($orderDate, $oderTime) {
return "hi";
}
?>
index.php
<?php
require "vendor/autoload.php";
require "config.php";
require "functions.php";
getDate(var_export($days),var_export($Time));
config.php
<?php
$Time = "1";
$days = ["Sat", "Sun"];
when you use return the return value wil not shown on your screen till you echo it out, like this
echo getDate(var_export($days),var_export($Time));
and by the way you can replace return keyword in your function with echo
in this case you don't need to write echo getDate(var_export($days),var_export($Time));
just type getDate(var_export($days),var_export($Time));

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.

Run some PHP code from other file

I have two files:
In a.php:
<?php echo "one"; include 'b.php'; echo "three"; ?>
In b.php:
<?php echo"one"; echo"two"; echo"three"; echo"four"; echo"five"; ?>
My question is how get only echo"two"; from b.php and what I must write in a.php?
To include other PHP files into another, add this to your code
include('otherfile.php');
You can't just retrieve a port of the code, including the file will execute the whole file. You could use function do this:
// File function.php
<?php
function a() {
echo 'one';
}
function b() {
echo 'two';
}
// File index.php
include('function.php');
a(); // will echo one
Here's a proper way to accomplish what you want.

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";
}

include php file containing function in a web page

I have a php file which contains php function. I have to receive its return value in a part of my web page. Where do I put the include? in the head or in the body? how can I call the function inside my web page?
You can include the php file wherever you want, as long as it is before you call the method contained in it. You call the function between <?php ?> tags. You can use echo to output it to the page.
So if you have myfunc.php that looks like this:
<?php
function myfunc() {
return 'asdf';
}
?>
Then in php that includes it you can do:
<?php
include('myfunc.php');
echo myfunc();
?>
You can also choose to put the include method anywhere above that makes sense. The very top of the file is a common choice.
Also note that if your php file contains functions, you should probably be using require_once instead of include. See: http://php.net/manual/en/function.require-once.php
You can't call php code from a html file. However, you could simply make your html web page into a php web page like this:
<?php
include("yourphpfunction.php");
?>
<html>
<head>...</head>
<body>
....
<!-- put php result here: -->
<?php echo myfunction(); ?>
... more html
</body>
</html>
and save it as .php instead of .html.
That's all the magic.
You can also wrap it all up in one statement:
<html>
<head>...</head>
<body>
....
<!-- put php result here: -->
<?php
include("yourphpfunction.php");
echo myfunction();
?>
... more html
</body>
</html>
as long as your file gets parsed as php.
you can put the include in the top of your html page like
<?php
include 'file.php';
?>
<html>
<head>
.....
and on your HTML, you can do something like:
<div>my stactic text <?php echo myFunction(); ?></div>
You should require (require_once) the file, before you reach the part where your returned output should be echoed.
required file.php:
<?php $test = "hello world"; return $test; ?>
include include.php:
<?php $output = require_once('file.php'); echo $ouput; ?>
You need a php webpage, you can't call a php function or include a php page in html file.
Assuming your webapge is php, You can include that file in the top of the page inside the tag. But be sure to include the file before the function is called.
require_once 'path/to/your/file';
or
include_once 'path/to/your/file';
Then you can call the function in your php file like:/
$test = functionInYourIncludedFile();
echo $test;
Hope this helps you :)
look below :
<?php
function doSomthing() {
return $var
}
?>
<html>
<?php echo doSomthing() ?>
</html>
or
<?php include('functionFile.php') ?>
<html>
<?php echo doSomthing() ?>
</html>

Categories