run a part of code php in other php file - php

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');
?>

Related

php call variable and put back on while loop

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

Print a value in php file using html [beginner]

Hello i have a php file called a.php that includes a specific value
example : $a = 0;
i just want to print this value (a) in a html file called b.html is that possible ? thanks
Sure, you have to open php tag, include php file, echo variable and close php tag
For example:
<p><?php include('a.php'); echo $a; ?></p>
This will work..
<?php
ob_start();
$a = 1;
echo $a; // echo $a value
file_put_contents('b.html', ob_get_contents()); // save the output to b.html
?>
However, if you already have other content in the existing page all of them will be there in the html file. Therefore you can POST the variable $a to a separate php file as follows..
<?php
ob_start();
$a = $_POST["aVar"];
echo $a;
file_put_contents('b.html', ob_get_contents());
?>

How to print the full PHP code generated right before compile (after includes and requires)?

Is there a way to print full php (in php) code after includes and requires?
For example I have 3 files.
File basic.php
<?php echo 2; include 'basic2.php'; ?>
File basic2.php
<?php echo 3; ?>
and the main file main_file.php
<?php echo 1; include 'basic.php'; echo 4; ?>
I would like to get the full code that would be about to be compiled, the result would be something like this:
<?php echo 1; ?><?php echo 2; ?><?php echo 3; ?><?php echo 4; ?>
Is that possible?

getting variable from another php-file from line

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.

How to pass the require_once output to a variable?

I want to call require_once("test.php") but not display result and save it into variable like this:
$test = require_once('test.php');
//some operations like $test = preg_replace(…);
echo $test;
Solution:
test.php
<?php
$var = '/img/hello.jpg';
$res = <<<test
<style type="text/css">
body{background:url($var)#fff !important;}
</style>
test;
return $res;
?>
main.php
<?php
$test = require_once('test.php');
echo $test;
?>
Is it possible?
Yes, but you need to do an explicit return in the required file:
//test.php
<? $result = "Hello, world!";
return $result;
?>
//index.php
$test = require_once('test.php'); // Will contain "Hello, world!"
This is rarely useful - check Konrad's output buffer based answer, or adam's file_get_contents one - they are probably better suited to what you want.
“The result” presumably is a string output?
In that case you can use ob_start to buffer said output:
ob_start();
require_once('test.php');
$test = ob_get_contents();
EDIT From the edited question it looks rather like you want to have a function inside the included file. In any case, this would probably be the (much!) cleaner solution:
<?php // test.php:
function some_function() {
// Do something.
return 'some result';
}
?>
<?php // Main file:
require_once('test.php');
$result = test_function(); // Calls the function defined in test.php.
…
?>
file_get_contents will get the content of the file. If it's on the same server and referenced by path (rather than url), this will get the content of test.php. If it's remote or referenced by url, it will get the output of the script.

Categories