Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I was trying to get variable value from a page and echo it out in another page
for example I have 2 pages pg1.php and pg2.php:
On pg2.php I have:
<?php
$vr = "Hello";
?>
Now I want to echo this out on pg1.php, I have tried this:
<?php
require "pg2.php";
echo $vr;
?>
It works, but the problem is whatever else I have on pg2.php will be displayed on pg1.php.
It would be best to restructure your code so that variables are defined in one file that then includes another file with output etc.
<?php
// vars.php
$vr = "Hello";
?>
<?php
// pg1.php
require "vars.php";
echo $vr;
?>
<?php
// pg2.php
require "vars.php";
// other stuff
// ...
// ...
?>
But for this issue in general, buffer output and then delete the buffer:
<?php
// pg1.php
ob_start();
require "pg2.php";
ob_end_clean();
echo $vr;
?>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to call variable with in variable and below attempts are working fine
Attempt 01:
<?php
$var1 = "Hello";
echo $var1;
?>
Attempt 02:
<?php
$view = '$var1 = "Hello";
echo $var1;';
echo $view;
?>
But I'm trying to call variable without using "echo" or "print" command, as mentioned below
<?php
$view = '$var1 = "Hello";
echo $var1;';
$view;
?>
Is there any other way to achieve this? Please advise.
<?php
$view = '$var1 = "Hello";
echo $var1;';
eval($view);
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a PHP file containing some variables and need to echo them multiple times in another file.
file1:
<?php
//some calculating going on
$var1 = 5;
$var2 = 2;
?>
file2:
<?php
echo $var1;
echo $var2;
?>
<?php
echo $var1;
echo $var2;
?>
How could this be achieved?
Your file2.php should include("file1.php"), like:
file2.php
<?php
include("file1.php");
echo $var1;
echo $var2;
?>
Assuming they're both in the same directory.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Currently I make my query like this:
<?php
function Data() {
$db = DB::getInstance();
$query = $db->query("SELECT * FROM users");
$results = $query->results();
return ($results);
}
$listdata = Data();
?>
And when I want to see my information I have something like this:
<div class="container">
<h1>Title</h1>
<hr>
<div class="row">
....
...
..
.
<?php foreach ($listdata as $v1) { ?>
<p><?=$v1->username?></p>
<?php } ?>
</div>
</div>
But if I want to put information see for example in the footer of the page I have to open a new foreach
As I can transform this code to open the query to the top of the page and simply llabar variables where I want without opening another foreach?
Even better would defeat the function, I just want to put the query and data wherever.
Thank you!
You could try echo $listdata[0]->column_name thou this will print only the first row......so to be precise use a where clause in your sql statment
Edit: It will print only the first index in the array that contains the Fetched Data
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In my code i would like to define a function / variable / ... .
It should look like something like this:
<?php
//define something here
{
"html code inside here"
}
// some php code
if(...)
{
"output the html
}
How can I do this?
You would need to put it in a variable/function first, or echo out the html below.
Option #1:
$html = "This is test html <a href='http://google.com'>google</a>";
if ( !empty($_POST) )
{
echo $html;
}
Option #2:
if ( !empty($_POST) )
{
echo "This is test html <a href='http://google.com'>google</a>";
//Or simply:
?>
This is test html google
<?php
}
Option #3:
function outputHtml() {
?>
This is test html google
<?php
}
if ( !empty($_POST) )
{
outputHtml();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got an echo of an class value but would like to assign this value to an Variable:
<p class="<?echo $test="listprice"?>"></p>
I thought about something like this:
<?$testing = echo $test="listprice"?>
But this doesnt seem to work. Is it possible to get the class echo?
When you $test = "listprice"; you are already assigning it to the variable $test. To assign it to the variable $testing if you really want to then you would do this:
$testing = $test;
// display the new variable
echo $testing;
try to separate assignment and output:
<?
$test="listprice";
?>
<p class="<? echo $test; ?>"></p>
Use short open tag of php <? ?>
<p class="<?=$test="listprice"?>">Something</p>
More about
it should be
<?php
echo $test = "listprice";
echo $name = "ankit";
?>