getting variable from another php-file from line
Hello
Is it possible to get a value from another PHP file
Reading variable from specific line
Example
file1.php
<?php
echo 'my variable is';
//reading the variable from line 3
?>
file2.php
<?php
/*this is line 1*/
/*this is line 2*/
$var = 'hello world'; /*this is line 3*/
?>
Use include or require in file1.php :
<?php
include 'file2.php';
//require 'file2.php';
echo 'my variable is '.$var;
//reading the variable from line 3
?>
you can include the file in the page.
Related
sorry for my last question where i try put some live code with ob_start buffer content is not helping me to solve my problem because buffer content just collects output text, it doesn't execute any code. thanks #akrys for your advices
what i want is to put code into while looping like this
$sql = $conn->query("SELECT * FROM `users`");
$var = $row['full_name'];
include('test.php');
after i call test.php contain while code like:
while($row = $sql->fetch_array()) {
echo $var;
}
everything is work if i replace $var with $row['full_name'];
but i get the name of row field from some script on index.php so i should access that file first then i call portable file contain query to fetch_array on test.php
how to make it work when i put it back with $var contain variable field name
thank you very much for your attention guys
you should to include before your code
page
test.php
<?php
$someVariable = 'hello'; // the variable only can access in here
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable $someVariable";
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
of course you can use define too
page test.php
<?php
define( "SOMEVARIABLE", hello );
?>
<?php
include('test.php');
ob_start();
echo "some text with call variable ".SOMEVARIABLE;
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; //
?>
you can use:
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
for more help, use the link below:
enter link description here
I have a.php
<?php
echo 'hello world';
echo 'MY NAME IS ZORO';
echo date('Y-m-d');
?>
and I have b.php and I want to get only code (echo date('Y-m-d');) from a.php.
How can I do?
As I understand it, you want only one line to print from multiple , try below code.
a.php
<?php
$_GET["onlydate"] = true;
include ('b.php');
?>
b.php
<?php
if(isset($_GET["onlydate"]) && $_GET["onlydate"]){
echo date("Y-m-d");
}
else{
echo 'hello world';
echo 'MY NAME IS ZORO';
echo date('Y-m-d');
}
?>
You can use include function of Php.
Put all your contents whatever you wish in b.php(in your case its echo date...) and then include b.php file in a.php.
You have to use the file handling. Then ignore all the code which you does not need in the a.php and then execute the remaining code in b.php
If I understand it right, you want only one line (block of code) from multiple.
Funtions
You can divide the code to functions and then call them as you wish.
File a.php:
<?php
function printDate() {
echo date('Y-m-d');
}
function printAll() {
echo 'hello world';
echo 'MY NAME IS ZORO';
printDate();
}
?>
File b.php:
<?php
include 'a.php';
printDate();
?>
Shared file
Other way is to make another file c.php with shared content and include it to files a.php and b.php.
File a.php:
<?php
echo 'hello world';
echo 'MY NAME IS ZORO';
include 'c.php';
?>
File b.php:
<?php
include 'c.php';
?>
File c.php:
<?php
echo date('Y-m-d');
?>
Suppose I write a line
include Yii::app()->basepath.'/views/email/email_friend.php';
now how can i take the response of this line into a variable?
like
$abc = include Yii::app()->basepath.'/views/email/email_friend.php';
Have a look at the PHP docs for include http://php.net/manual/en/function.include.php
Example #5 is I think what you're looking for
return.php
<?php
$var = 'PHP';
return $var;
?>
noreturn.php
<?php
$var = 'PHP';
?>
testreturns.php
<?php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
?>
All you have to do is the included file had a return with the desired value. It's been quite popular for some time.
so the include.php should like the following:
<?php
return ' World!';
and the including one:
<?php
$a = include('include.php');
echo 'Hello'.$a; // Hello World!
When you include it's like you're copy/pasting the code into your PHP. If it's just inline PHP and there was a variable $abc in the include file 'email_friend.php' then you could access the variable normally after the include.
I know this is an old post. I hope my answer will be useful to someone. I combined the Accepted answer with the answer "PHP/7 you can use a self-invoking anonymous function..."
define( 'WPPATH', dirname(dirname(__FILE__)) . '/public/partials/bla-bla.php' );
$publicDisplayContent = (function () {
// [PHP/7 you can use a self-invoking anonymous function](https://stackoverflow.com/a/41568962/601770)
// https://stackoverflow.com/a/5948404/601770
ob_start();
require_once(WPPATH);
return ob_get_clean();
})(); // PHP/7 you can use a self-invoking anonymous function
error_log( 'activate() >> $publicDisplayContent: ' . print_r( $publicDisplayContent, true ) );
DOT DOT DOT
'post_content' => $publicDisplayContent,
I want to include a file entirely into a variable. So that I can call this var multiple times and keep the code as clean as possible. But when I echo the var it only returns a 1 and when I use the include on itself it output the entire file.
I want to output the included file and run all php code inside it.
So what am I doing wrong here.
default.php
$jpath_eyecatcher = (JURI::base(). "modules/mod_eyecatcher/tmpl/content/eyecatcher.php");
$jpath_eyecatcher_path = parse_url($jpath_eyecatcher, PHP_URL_PATH);
ob_start();
$eyecatcher = include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
ob_end_clean();
echo $eyecatcher . '<br>';
include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
echo output is
1
include output is
eyecatchertype = 2
fontawesome
envelope-o
insert_emoticon
custom-icon-class
128
images/clientimages/research (1).jpg
top
test
Thanks for the help!
Use file_get_contents instead of include()
include() executes the php code given in the file, whereas file_get_contents() gives you the file content.
include is not a function, and normally only returns the status of the include operation:
docs:
Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files.
e.g.
x.php:
<?php
return 42;
y.php
<?php
$y = 'foo';
z.php
<?php
$z = include 'x.php';
echo $z; // outputs 42
$y = include 'y.php';
echo $y; // ouputs 1, for 'true', because the include was successful
// and the included file did not have a 'return' statement.
Also note that include will only execute the included code if it contains <?php ... ?> code block. Otherwise anything included is simply treated as output.
Use file_get_contents or ob_get_clean, like so:
ob_start();
include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
$eyecatcher = ob_get_clean();
The following assigns the return value of include() to the variable $eyecatcher.
$eyecatcher = include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
Because the include() was successful, it returns a boolean value of true, which is presented as "1" when you echo it.
If you wish to load the $eyecatcher variable with the contents of the file as a string, you do:
$eyecatcher = file_get_contents($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
I have this code for index.php:
<!DOCTYPE html>
<html>
<?PHP
if(isset($_GET['page'])){
require dirname(__FILE__).'/modules/'.$_GET['page'].'/main.php';
} else {
require dirname(__FILE__).'/modules/home.php';
}
?>
</html>
main.php:
$title = 'title test';
$description = 'desc test';
$keyword = 'keys test';
echo _is_header_();
header function:
function _is_header_(){
require ABSPATH.'/templates/'.TEMPLATENAME.'/header.php';
}
In header.php I have meta html tag for title and echo $title for show title of page. but I see this error :
<b>Notice</b>: Undefined variable: title in
how do fix this error?!
NOTE: when I replace require ABSPATH.'/templates/'.TEMPLATENAME.'/header.php';
with echo _is_header_(); my code worked true and show my title.
In your script
ABSPATH.'/templates/'.TEMPLATENAME.'/header.php';
write a
global $title;
just befor the line you use it first time.
If your file "header.php" manipulates the variable "$title" or her not exists, then do you have prints her first, before to include this file.