Can I use variables in an include statement?

Discussion in 'PHP' started by stonemikey, Jul 4, 2007.

  1. #1
    I've been trying to use a variable in an include statement, as follows:

    $x = 5;
    $filename = "file_" . $x . ".php";
    include '$filename';

    This doesn't seem to work. Is there something I'm missing or is this simply not possible?

    Thanks very much for your help.

    Mike
     
    stonemikey, Jul 4, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Yes, this is possible.

    What error are you getting ? Is the file definately there ?

    Brew

    PS Can you use the PHP tags when posting code - it makes it so much more easier to read
     
    Brewster, Jul 4, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Variables are not parsed between single quotes. Do it this way instead.
    
    include $filename;
    
    PHP:
     
    nico_swd, Jul 4, 2007 IP
  4. stonemikey

    stonemikey Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    sorry about the php tags. yes the file is definitely there.

    If I call directly, like this, then it works:

    <?php
    include 'create_array_10.php';
    ?>

    However, the following does not work:

    <?php
    $x = "10";
    $createarrayfile = "create_array_" . $x . ".php";
    include '$createarrayfile';
    ?>

    This is the error message i get:

    Warning: main($createarrayfile): failed to open stream: No such file or directory in /home/fhlinux191/b/...

    Warning: main($createarrayfile): failed to open stream: No such file or directory in /home/fhlinux191/b/...

    Warning: main($createarrayfile): failed to open stream: No such file or directory in /home/fhlinux191/b/...

    Warning: main(): Failed opening '$createarrayfile' for inclusion (include_path='.:/usr/share/pear') in /home/fhlinux191/b/...
     
    stonemikey, Jul 4, 2007 IP
  5. stonemikey

    stonemikey Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks nico_swd... that solved it.
     
    stonemikey, Jul 4, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    also, you might want to add a conditional in there based on file_exists.. this way you avoid some errors if you try to open something that doesn't exist.

    
    <?php
        $x = 10;
        $createarrayfile = "create_array_" . $x . ".php";
    
        if( file_exists($createarrayfile) )
            include($createarrayfile);
    ?>
    
    
    PHP:
     
    ansi, Jul 4, 2007 IP
  7. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just a quick reminder on safety, if the varible you are using is from an external source (ie database / form / user) you should check to make sure it only contains the letters & numbers you want it to (ie not file system stuff like .. / etc etc)
     
    powerspike, Jul 4, 2007 IP
  8. stonemikey

    stonemikey Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you for all your tips. The files are all controlled by myself so checking what it contains shouldn't be necessary, but putting the check that it exists is a good idea, even just to make the whole thing more robust.

    Thanks again.
     
    stonemikey, Jul 4, 2007 IP