Need some quick help with passing variables

Discussion in 'PHP' started by vetting, Oct 22, 2007.

  1. #1
    So I have a index.php file that has all of my html code in it.

    It includes store.php in the same directory.
    store.php just serves to grab the folder name the store.php is in.
    $variable1=basename(getcwd());
    It also includes process.php in another directory.

    I need to automatically pass $variable1 to the process.php but it isnt working right.

    Any ideas what I'm doing wrong?
     
    vetting, Oct 22, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Post your actual code.

    Are you defining the variable before including the other file?
     
    nico_swd, Oct 22, 2007 IP
  3. vetting

    vetting Peon

    Messages:
    193
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes...actually I just figured it out.

    $currentdirectory= basename(getcwd());
    $_SESSION['sellerID']= $currentdirectory;
    $sellerID= $_SESSION['sellerID'];
    include /home/test.php


    then retrieved it in the test.php
    $sellerID= $_SESSION['sellerID'];
     
    vetting, Oct 22, 2007 IP
  4. FFMG

    FFMG Well-Known Member

    Messages:
    1,091
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    160
    #4
    This does not look right.
    although this will technically work, it is not the right way of doing it.

    I am guessing that you are using '$sellerID' in a function?

    if you do

    
    <?php
    // in file1.php
    $sellerID = 'This is my seller id';
    include( 'file2.php' );
    ?>
    ...
    
    // file2.php
    <?php
    echo $sellerID;
    ?>
    
    PHP:
    and that should work, (you should see 'This is my seller id').
    But if $sellerID is in a function in either file then you will need to add
    
    global $sellerID;
    
    PHP:
    in front.

    FFMG
     
    FFMG, Oct 22, 2007 IP
  5. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #5
    arent globals bad?

    if you need to pass stuff from a function cant you just add it like

    exampleFunction($sellerID)
     
    Noddegamra, Oct 23, 2007 IP
  6. FFMG

    FFMG Well-Known Member

    Messages:
    1,091
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    160
    #6
    It really depends on the code.
    Without seeing the OPs code we cannot tell.

    But using a super global, ($_SESSION), to pass data around is even worse.

    FFMG
     
    FFMG, Oct 23, 2007 IP