Okay, so let's say I have a webpage with a defined php $variable, and then I use include("variableEchoed.php), which just echos $variable.
How do I accomplish this? Simply doing what I said above doesn't work.
Again, I want to use an included php file to echo a variable onto another webpage where that variable is defined.
Index.php
<?php
$variable = 'test';
include('variableEchoed.php');
?>
variableEchoed.php
<?php
echo($variable);
?>
The result will be test.
yes its easily doable.
$ cat 1.php
<?
echo $x;
?>
$ cat 2.php
<?
$x="hello world";
include("1.php");
?>
$ php 2.php
hello world
Write a function in the file to echo that $variable.. Call the function just after including the file.
Related
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.
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
I am using joomla 3.2 with jumi 3, here is a sample of how I call my script via jumi
{jumi [myscript.php] [foo] [bar]}
here is myscript.php
<?php
echo $jumi[0];
echo $jumi[1];
?>
above script is successfully able to print the values as foo & bar
but if I modify the same to include another script ans try to get the variables in that script then it does not have any value
here is new myscript.php
<?php
include 'foobar.php';
?>
foobar.php
<?php
echo $jumi[0];
echo $jumi[1];
?>
this fails to print the values. help me find if I am missing something
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>
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';
?>