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
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
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/...
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:
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)
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.