Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


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 kirupa.com Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012