Echo variables from another file in php [closed] - php

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.

Related

Getting variable value from another page [closed]

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

Call variable without echo or print [closed]

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

Clean HTML and PHP For Loop Not Displaying Anything [closed]

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 7 years ago.
Improve this question
<?php for($x = 0; $x <= count($slides); $x++):?>
<li data-target="#main-carousel" data-slide-to="<?php echo($x);?>'" class="active"></li>
<?php endforeach; ?>
Not exactly sure what the error is everything seems right.
You start yourself with a for loop, while closing it with a foreach loop. These are two very different things and cannot be matched like that.
You simply need to replace
<?php endforeach; ?>
with
<?php endfor; ?>
Alternatively, you can use curlybrackets { instead, having something like
<?php for($x = 0; $x <= count($slides); $x++) { ?>
<!-- do HTML here -->
<?php } ?>

How do i assign a variable with the output of an echo? [closed]

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";
?>

Switch Statment Give Always The default [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
I have a problem in my script in wampserver. It runs well. But in my hosting it doesn't display
the page which is named classed.php?cat=[category_Name_Example]
<?php header("Content-type: text/html; charset=utf-8");
?>
<?php include_once("analyticstracking.php") ?>
<?php
include 'includis/html_codes.php';
include 'includis/config.php';
$catID= mysql_real_escape_string($_GET['cat']);
switch ($catID)
{
case 'javascript' :
$catName = "javascript";
$PageTitle = "Javascript ";
$img = "img/javascript.png";
break;
case 'htmlandcss' :
$catName = "htmlandcss";
$PageTitle = "html ";
$img = "img/html2.png";
break;
default:header('location: /404');
}
if (!isset($catID)){
header ('Location 404.php');
}
if (empty($catID)){header ('Location 404.php');}
include 'includis/db.php';
?>
please help thanx :)
Most probable reason is that you do not have an open database connection while calling to this function. In this case mysql_real_escape_string just returns false.

Categories