2

I have a piece of code:

foreach (glob('mov/$context.mov') as $filename){ 
    $theData = file_get_contents($filename) or die("Unable to retrieve file data");
}

Is that the correct way to add a variable within that glob? $context

Also, in my new file, I want to replace a word with $context, so would I write

$context = "word"; // so it adds that word to the glob function when the script is included in a different file.
ValleyDigital
  • 1,460
  • 4
  • 21
  • 37

3 Answers3

3

PHP variables are not interpolated inside single quotes. Use double quotes or put the variable out of quotes

foreach (glob("mov/$context.mov") as $filename){ 

or

foreach (glob('mov/'.$context.'.mov') as $filename){ 

If you do $context = "word"; before your foreach then glob will look for mov/word.mov

Reference

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I wanted to put $context ="word"; in a different file, so when I include the file that runs the foreach (glob(... I can use that as my main file and run the for each on whatever filename I want. However, in my other script, when I include('test.php') // the script that has foreach (glob('mov/'.$context.'.mov') as $filename) and then call out $context ="word"; it doesn't work. Any idea on that? – ValleyDigital Oct 07 '13 at 15:19
  • That should work, can you post the relevant code of both files? – Hanky Panky Oct 07 '13 at 15:24
1

You should use the double-quotes in the first parameter of the glob function.

glob("mov/$context.mov")

or, if you want, use brackets

glob("mov/{$context}.mov")

In this way the variable name will be replaced with the value.

EDIT:
For the other question:
the script with the glob function can be executed multiple times changing the value of the $context variable before the script inclusion. Example:

$context = "word";
include("test.php");

$context = "foo";
include("test.php");
mcont
  • 1,749
  • 1
  • 22
  • 33
  • how would I replace the value between files. If I include this script in a separate php file? I want to replace $context with a word, but have $context="word"; in the file that has the script included in it. – ValleyDigital Oct 07 '13 at 15:25
  • Just change the value of the variable `$context`? It's not clear what you're trying to achieve... what's the problem when you execute your script? – mcont Oct 07 '13 at 15:27
  • Sorry, let me try to explain it a little better. The php script that uses 'foreach' is labeled test.php - I want to include that file in another php script labeled movies.php in movies.php I'd like to just say $context ="word"; so it will replace the variable $context in test.php and work as planned. Basically, having one main script I can edit, that is included in several scripts each replacing $context with a different word so the filename can run on whatever file it's named. @Matteo – ValleyDigital Oct 07 '13 at 15:35
  • Ok, but where's the problem? You are doing something like this, right? `$context = 'word'; include('test.php');`. Then you can change `$context` every time you include a script that requires the variable. – mcont Oct 07 '13 at 17:04
  • I figured it out. Thank you for your help @Matteo you definitely pointed me in the right direction. I had forgotten to put the variable before the include on my separate php file. It's working perfectly now. – ValleyDigital Oct 07 '13 at 17:51
  • :) I edited the answer with the new part, you should choose the answer as the best. – mcont Oct 07 '13 at 19:32
1

You can use either of the following ways-

1.glob("mov/$context.mov")

2.glob("mov/".$context.".mov")

Note: Unlike the double-quoted syntax, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

For reference: Read More Here

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57