The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 8 of 8

Thread: Send PhP variables from one file to another

  1. #1

    Send PhP variables from one file to another

    I have two php scripts, alpha.php and beta.php. In alpha.php, how do I run beta.php and pass beta.php a variable from alpha.php. Also, how do I 'retrieve' the variable for use in beta.php?

  2. #2
    quick way:

    on alpha
    <a href="beta.php?varName=varVal">link</a>

    on beta
    echo $_GET['varName'];

    this will display the variable in the header. you can also use forms, there are many ways

  3. #3
    I want to run script beta.php from alpha.php with no user interaction, when alpha.php loads. How do I go about that?

  4. #4
    include beta.php


  5. #5
    And using include, how do I pass the variables from alpha to beta?

    here's alpha:

    $data = 1;
    include 'beta.php';

    and here's beta:

    $data = $_GET['data'];
    if ($data == 1){
    $file = fopen("text1.txt", "r");
    $content = fread($file, filesize("text1.txt"));
    elseif ($data ==2){
    $file = fopen("text2.txt", "r");
    $content = fread($file, filesize("text2.txt"));
    print $content;

    I want to be able to call beta a few times in alpha, but not one call right after another. And with each call I want to send beta the variable data.

  6. #6
    If you do this, beta.php automatically gets the value of $data variable...
    PHP Code:
    // alpha.php
    $data 1;
    PHP Code:
    // beta.php
    echo("Data = $data");
    As for your line in beta.php file...
    PHP Code:
    if ($data == 1){ 
    $file fopen("text1.txt""r"); 
    $content fread($filefilesize("text1.txt")); 
    elseif (
    $data ==2){
    $file fopen("text2.txt""r"); 
    $content fread($filefilesize("text2.txt")); 

    This can be written like this...
    PHP Code:
    $file fopen($dataFile"r"); 
    $content fread($filefilesize($dataFile)); 
    In other words, no need to use if statement if your file name is sequential like that...
    CyanBlue / Jason Je / Flash Developer

  7. #7
    once you have included a file, it is as if the included file and the base file are 1. meaning that you don't have to 'get' any variables

  8. #8
    Ok, I got it working

    Thanks a bunch you two

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012