57

So I wonder if it is possible to get a variable from a specific php-file when the variable-name is used in multiple php-file. An example is this:

<header>
 <title>
  <?php echo $var1; ?>
 </title>
</header>

page1.php has $var1 = 'page1' page2.php has $var1 = 'page2'

footer.php should have <a href="">$var1 from page1</a><a href="">$var1 from page2</a>

Ok the example is a bit abstract, but as short as I can make it. I think you get what I am getting at! So it is the in the footer I am after! Got any solutions?

3
  • unless the global variable that you're accessing is in an already-included file, you can't get its value. Commented Oct 30, 2012 at 8:28
  • 1
    look into scoping and globals - php.net/manual/en/language.variables.scope.php Commented Oct 30, 2012 at 8:29
  • you are looking for "namespaces" Commented Aug 26, 2019 at 22:25

4 Answers 4

105

You can, but the variable in your last include will overwrite the variable in your first one:

myfile.php

$var = 'test';

mysecondfile.php

$var = 'tester';

test.php

include 'myfile.php';
echo $var;

include 'mysecondfile.php';
echo $var;

Output:

test

tester

I suggest using different variable names.

Sign up to request clarification or add additional context in comments.

3 Comments

So in other words this is not really possible? For this thing it's not necessary, just would give me neater code!
The issue is probably your design. What exactly are you trying to do?
Well I am trying to echoing the same text at two different locations, one which is within the actual page, and one that is on a universal footer (footer.php). Basically I am using the variable for easy update!
55

You could also use a session for passing small bits of info. You will need to have session_start(); at the top of the PHP pages that use the session else the variables will not be accessable

page1.php

<?php

   session_start();
   $_SESSION['superhero'] = "batman";

?>
<a href="page2.php" title="">Go to the other page</a>

page2.php

<?php 

   session_start(); // this NEEDS TO BE AT THE TOP of the page before any output etc
   echo $_SESSION['superhero'];

?>

2 Comments

I don't really see why this is getting down votes. It is a perfectly valid alternative to includes that OP may not have considered
It requires visiting 'page1.php' first. Using sessions as a pseudo global namespace could get very painful.
6

using include 'page1.php' in second page is one option but it can generate warnings and errors of undefined variables.
Three methods by which you can use variables of one php file in another php file:

  • use session to pass variable from one page to another
    method:
    first you have to start the session in both the files using php command

    sesssion_start();
    then in first file consider you have one variable
    $x='var1';

    now assign value of $x to a session variable using this:
    $_SESSION['var']=$x;
    now getting value in any another php file:
    $y=$_SESSION['var'];//$y is any declared variable

  • using get method and getting variables on clicking a link
    method

    <a href="page2.php?variable1=value1&variable2=value2">clickme</a>
    getting values in page2.php file by $_GET function:
    $x=$_GET['variable1'];//value1 be stored in $x
    $y=$_GET['variable2'];//vale2 be stored in $y

  • if you want to pass variable value using button then u can use it by following method:

    $x='value1'
    <input type="submit" name='btn1' value='.$x.'/>
    in second php
    $var=$_POST['btn1'];

Comments

1

You could also use file_get_contents

 $url_a="http://127.0.0.1/get_value.php?line=a&shift=1&tgl=2017-01-01";
 $data_a=file_get_contents($url_a);

 echo $data_a;

3 Comments

This reads the entire file in as a string... to get a specific variable's value would require additional processing.
It would have been interesting to see some of the "additional processing"
maybe not echo $data_a; but you could definitely get the variable from $url_a. Either as XML or array. Use var_dump to view it first then parse it your own way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.