When i tried to pass a value $abc to the IncludedFile.php, it doesnt go through. Neither does the $session work. Is there a way to pass a value to the IncludedFile.php? My ultimate aim is to get the calculation processed at the IncludedFile.php and then echo it on the Main.php
Main.php
<?php $abc = 'abc';
include_once ('IncludedFile.php');
echo $answer; ?>
IncludedFile.php
<?php if($abc=='abc') {$answer = 5;} ?>
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');
?>
I'm still learning php and I still haven't figured out when to use ' or ". I'm guessing thats the problem with this code. It redirects me to the right page but the $loc variable isn't carried over.
<?php header("Location: roomdata.php?loc=$loc"); ?>
on the page that has the header() commaned I also have an include command...
<?php include 'include/globalscripts.php'; ?>
and in the globalscripts.php is...
<?php if( isset($_GET['loc']))?>
<?php $loc = $_GET["loc"];?>
I would personally use:
<?php
header('Location: roomdata.php?loc='.$loc);
?>
<?php if( isset($_GET['loc']))?>
<?php $loc = $_GET["loc"];?>
TRY
<?php
if( isset($_GET['loc'])){
$loc = $_GET["loc"];
}
?>
Your code should work, are you sure $loc is defined at this point?
Regarding ' and ":
$value = "derp";
echo "the value is:\t$value";
//output: the value is: derp
echo 'the value is:\t$value';
//output: the value is:\t$value
I have a simple function, which I'm trying to turn turn into a query. The idea is to grab the number of facebook likes (which I've done), and pass it into a simple equation (which is ready) to work out the percentage of likes against a target number. I know that the php controling the percentage will return 0.0 at the moment - that's fine for now :) I just can't work out how to get from the function to a variable...
Code as follows:
<?php
function bfan() {
$pageID = 'VisitTywyn';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
echo $info->likes;
} ?>
<?php bfan(); ?>
<?php
echo number_format(($num_amount/$num_total)*100, 1);
?>
Thanks for your help!
What you want to do is return your result so it can be passed to a variable.
<?php
function bfan() {
$pageID = 'VisitTywyn';
$info = json_decode(file_get_contents('http://graph.facebook.com/' . $pageID));
return $info->likes;
} ?>
<?php $something = bfan(); ?>
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.