-2

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?

wscourge
  • 10,657
  • 14
  • 59
  • 80
Gabriel Winkler
  • 315
  • 3
  • 14
  • Include file1 into file2 to by using: include "file1.php"; just below php – Adam Hull Dec 12 '16 at 20:37
  • Possible duplicate of [Passing a variable from one php include file to another: global vs. not](http://stackoverflow.com/questions/4675932/passing-a-variable-from-one-php-include-file-to-another-global-vs-not) – wscourge Dec 12 '16 at 20:50

1 Answers1

0

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.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
wscourge
  • 10,657
  • 14
  • 59
  • 80