user2812904
user2812904

Reputation: 33

Using a variable in file_get_contents php

I'm trying to make a dynamic webpage. I have the title for each page in its own file and I was trying to use file_get_contents() to get the title, but I'm not sure how to use a variable in the path. This is what I've tried.

<?php
         $movie= $_GET["film"];
         $title= file_get_contents('/movies/moviefiles/.$movie./info.txt');

 ?>

 <h1><?= ($title) ?> </h1>

Upvotes: 1

Views: 11908

Answers (5)

Your code should be like this:

<?php
$movie= $_GET["film"];
$title= file_get_contents('/movies/moviefiles/'.$movie.'/info.txt');
?>
<h1><? echo $title; ?> </h1>

Upvotes: 1

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

$title= file_get_contents("/movies/moviefiles/$movie/info.txt");

Upvotes: 0

Donovan Charpin
Donovan Charpin

Reputation: 3397

You can't work with variable with simple quote.

if you want to use variable, use double quote

$title= file_get_contents("/movies/moviefiles/$movie/info.txt");

More information on these quotes : http://www.php.net/manual/fr/language.types.string.php

Upvotes: 0

kero
kero

Reputation: 10638

You need to use double quotes " when you want to use variables inside a string or concatenate using the ..

$foo = "world";
print "hello $foo";
print 'hello '.$foo;

Upvotes: 1

Amal Murali
Amal Murali

Reputation: 76666

You aren't concatenating the strings properly.

Try:

$title= file_get_contents('/movies/moviefiles/'.$movie.'/info.txt');

The same can be done with double-quotes, too:

$title= file_get_contents("/movies/moviefiles/$movie/info.txt");

The difference is that variables aren't interpolated within single quotes. If they're in double-quotes, the actual value of the variable will be used.

And read more about string concatenation here: http://php.net/manual/en/language.operators.string.php

Upvotes: 8

Related Questions