Can I use variables of a included file in another included file? - php

Hello let me explain my question. I'm using PHP at the moment and I'm just playing around a bit now im wondering if I have a file for example:
//This is file1.php
<?php
$text = "Hello";
?>
And include it in file2 and also include file3:
//This is file2.php
<?php
include 'file1.php';
include 'file3.php';
?>
Now Comes my question can I use $text in file3 like so?:
//This is file3.php
<?php
echo $text;
?>
Thank you for your response!

Short ans quick: Yes you can, but NOT in your example. If you call $text in your file2 after including it, it works.
Include does the same, as you have the code in the same file. But look at the order, its important.
Build one file you call over the browser and require all you need in this file.

Related

PHP file_get_contents() from another file file_get_contents()

How can i get content from a file with file_get_contents() that includes the function file_get_contents() to get content from another file?
for axample:
a.php -> file_get_contents(b.php)
b.php -> file_get_contents(www.google.com)
i want to have html output from www.google.com in a.php
EDIT: a.php is on a different server than b.php
a.php (in first server)
<?php
echo file_get_contents("http://mysecondserver.com/b.php");
?>
b.php (in second server)
<?php
echo file_get_contents("http://www.google.com");
?>
When you open a.php, it is going to take the html content of b.php, and b.php is taking the html content of google.com, so i guess you are done.
The same way you get content from another file that doesn`t have file_get_contents, you can use file_get_contents() as many times as u want in as many files as you want.
For ex:
1.php
<?php
echo file_get_contents("2.php");
?>
2.php
<?php
echo file_get_contents("3.php");
?>
3.php
<?php
echo "OK";
?>
The output will be - OK

PHP - How to echo a variable that contains php?

If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.

How To add Php code snippest at the begineing of a .php file using php

I have a .php file, say "index.php" . This file is being generated dynamically and I want to add the following code at the beginning of the file.
<?php
session_start();
require_once('login/auth.php');
require_once('config.php');
?>
Intention is to force user to sign up or sign in before they see this file. But I wasted a whole day and turned into nothing as resultant file is blank. what I found is , its facing problem with <?php and ?>
if I replace them with their equivalent entities <?php and ?> then they gets added easily but its not what I wanted.
So , question is how to add this original code to the beginning on file dynamically. Can someone help please?
<?php
$preamble = <<<EOL
<?php
session_start();
yada;
yada;
yada;
?>
EOL;
file_put_contents('index.php', $preamble);
How about the following:
<?php
file_put_contents('index.php', " <?php
session_start();
require_once('login/auth.php');
require_once('config.php');
?>");
Edit: Marc's idea to use heredoc syntax may be better in your situation, given the variety of text you may want to write to a file.

PHP included functions not working in firefox 5

I have 2 files:
create.php:
<html>
<body>
<?php
require("Test.php");
hello();
echo "does this work?";
?>
</body>
</html>
and Test.php:
<?php
function hello(){
echo "hello";
}
?>
But when I open create.php, nothing prints (not even "does this work?". If I call hello() from Test.php it works fine. That is, it doesn't seem to be executing code after the include. What am I doing wrong?
edit: the code seems to work fine in my IE 8 install, but not in my FF 5 install (which, admittedly, has way to many addons).
edit again: the issue was that the page cache needed to be refreshed. There was never a problem. The code works. sorry, all.
Do yourself a favour and turn on error reporting. Place the following code at the beginning of create.php and let us know the error message(s) you receive.
<?php
ini_set('display_errors', 'on');
error_reporting(E_ALL);
require_once('Test.php');
?>
My guess is that it is a path issue.
First of all, you do not need the html tags in your PHP-File. Second: you need to execute your function. Now you only defined it. Try
Test.php
<?php
function hello(){
echo "hello";
}
hello();
?>
And make sure that both files are in the same directory.
use dirname(FILE) instead of a stright include
eg
if i have a dir
/var/www/html/include
and test.php resides in includes and your script is in html then use dirname(__FILE__).'/includes/test.php
if you need to go back in a dir the use dirname(dirname(__FILE__))
depending on how many levels.
it also makes it dynamic so command line and browser will always fin the file
Try the code below for create.php
<?php
require('Test.php');
?>
<html><body>
<?php
hello();
echo 'does this work?';
?>
</body>
</html>

Passing parameter while including PHP script

I want to do this, but it gives error :( for better understanding my problem I'm giving an example:
<?php
include 'script.php?text=hiii';
?>
content of the script.php
<?php
echo $_GET['text'];
?>
So, how can i pass an argument while including the script page?
You could set $_GET['text'] before including the file:
$_GET['text'] = 'hiii';
include 'script.php';
But this obviously won’t affect other variables like $_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING'] etc.
After you include any script, the included script will act as it's in the same page.
For yourpage.php?text=hiii, that include('script.php') will automatically print hiii, as content of script.php will be in your included page.
You could've done something like this:
<?php
$_GET['text'] = 'what you want to do';
include('script.php');
?>
Actually we don't need to add it to $_GET. just create a variable and use it. Example:
script.php
<title><?php $text; ?></title>
<!--- other code goes here -->
index.php
<?php
$text = 'Welcome back';
include 'script.php';
?>

Categories