How to Copy a Variable From One PHP File to Another

How to Copy a Variable From One PHP File to Another thumbnail
Use PHP to pass variables from one file to another.

PHP (Hypertext Preprocessor), a versatile language commonly used for web development, can be used to transfer variables between files. Many programs written in PHP for the Web, such as a blog platform, typically store variables in a master file and then pull those variables from other files. When this happens, the data pulled from other files can be used in any way desired.

Things You'll Need

  • Programming tool (such as Notepad or Dreamweaver)
Show More

Instructions

    • 1

      Open a program to write PHP code into, such as Notepad or Dreamweaver. Create a new file called "variables.php" and copy the following code into the file:

      <?

      //make 2 variables

      $variableOne = "My info.";

      $variableTwo = "Some other info.";

      ?>

      Save the "variables.php" file and put it on your website.

    • 2

      Create another file and call it "seevariables.php" and copy the following code into it:

      <?

      //include the other file so you can use its variables

      include ("variables.php");

      //next, print out the two variables from the file

      echo $variableOne;

      echo "<BR><BR>";

      echo $variableTwo;

      ?>

      Save the "seevariables.php" file and put it on your website.

    • 3

      Go to the "viewvariables.php" file on your website in a web browser and you'll see the variables being passed from "viewvariables.php" to "seevariables.php."

Tips & Warnings

  • If this code doesn't work, the website might not have the ability to use PHP code. Contact the site's hosting company.

Related Searches:

Comments

You May Also Like

Related Ads

Featured