Using a variable in file_get_contents php - 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>

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

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;

Your code should be like this:
<?php
$movie= $_GET["film"];
$title= file_get_contents('/movies/moviefiles/'.$movie.'/info.txt');
?>
<h1><? echo $title; ?> </h1>

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

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

Related

What's wrong with this line ? i'm trying to figure out since hours (php)

The following line of code is supposed to echo the current season and a data in a php echo:
<?php echo "<h5>" $_SESSION['username'] '<span class="chat_date">'Dec 25"</span></h5>" ?>
Is the session call true? And how can I fix this code?
if you want echo a string you should use concatenation operator ('.')
<?php echo '<h5>' .$_SESSION['username']. '<span class="chat_date">Dec 25</span></h5>' ?>
or use double-quotes (") to passing data
$usename=$_SESSION['username'];
<?php echo '<h5>"$usename"<span class="chat_date">Dec 25</span></h5>' ?>
There is no need to concatenate a string before echoing it in PHP.
The parameters can be passed individually to echo as multiple arguments for a slight performance (speed) increase. In other words, you can use , instead of ..
For example:
<?php echo '<h5>' , $_SESSION['username'] , '<span class="chat_date">Dec 25</span</h5>'; ?>
I would recommend reading the Official Documentation.
<?php
echo "<h5> $_SESSION[username] <span class=\"chat_date\">Dec 25</span></h5>";
?>
Variables are evaluated inside double quoted strings.
To echo double quotes inside double quotes, you add a backslash before the double quote so PHP knows it is not the end of the quoted segment.
For arrays, do not quote the key inside a string.

Not able to access appended variable in URL in PHP

I am trying to pass a variable from one page to another by appending it to the URL like this-
<a href='single.php?name=".$m['Title']." ' class='movie-beta__link'>
and in the next page(single.php), I am getting the url as-
$title= $_GET['name'];
echo $title;
But nothing is displayed and my URL looks like this-
http://localhost/mdb/single.php?name=
Can someone please point out if I am missing something here.
Try this
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
You're getting your single and double quotes mixed up. With single quotes you have to append variables and with double quotes you can put them inside. But when you're outside of <?php tags you can use <?= to echo a variable.
<a href="single.php?name=<?= $m['Title'] ?>" class="movie-beta__line">
This will work above:
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
if all this is in php echo, use <a href='single.php?name='".$m['Title']."' class='movie-beta__link'>Title</a>
And its good to avoid underscores on you class names

Run PHP and HTML code from a string

I have a string stored in a variable in my php code, something like this:
<?php
$string = "
<?php $var = 'John'; ?>
<p>My name is <?php echo $var; ?></p>
";
?>
Then, when using the $string variable somewhere else, I want the code inside to be run properly as it should. The PHP code should run properly, also the HTML code should run properly. So, when echoing the $string, I will get My name is John wrapped in a <p> tag.
You don't want to do this. If you still insist, you should check eval aka evil function. First thing to know, you must not pass opening <?php tag in string, second you need to use single quotes for php script You want to evaluate (variables in single quotes are not evaluated), so your script should look something like:
<?php
$string = '
<?php $var = "John"; ?>
<p>My name is <?php echo $var; ?></p>
';
// Replace first opening <?php in string
$string = preg_replace('/<\?php/', '', $string, 1);
eval($string);
However, this is considered very high security risk.
So if I'm right, you have the $var = 'John'; stored in a different place.
Like this:
<?php $var = 'John'; ?>
And then on a different place, you want to create a variable named $String ?
I assume that you mean this, so I would suggest using the following:
<?php
$String = "<p>My name is ".$var." </p>";
echo $String;
?>
You should define $var outside the $string variable, like this
<?php
$var="John";
$string = "<p>My name is $var</p>";
?>
Or you can use . to concatenate the string
<?php
$var="John";
$string = "<p>My name is ".$var."</p>";
?>
Both codes will return the same string, so now when doing
<?php
echo $string;
?>
You will get <p>My name is John</p>

Replace simple quote and double quote

I have a variable in my MySQL database. I want this variable to handle both simple and double quotes.
For example:
$variable = "I'm happy" or $variable = I'm happy or $variable = "I am happy"
In my DB, the first example is this: "i'm happy" and that works for me. The problem is now on my JS function because I want to call my data:
nameFolder = "<?php echo $variable ; ?>"
But, if $variable = "I'm happy", I've got double "" so I get a JS error. And if I put single quote, the problem is the same with another case.
Any ideas?
The best way to echo data to JavaScript is to use json_encode:
var nameFolder = <?php echo json_encode($variable); ?>;
N.B.: There shouldn't be any surrounding quotes, function will handle that for you.
Use
nameFolder = "<?php echo str_replace('"', '\"', $variable); ?>"

Is this possible to use a variable for an url in php?

I'm calling a function like this:
<?php print get_thumbnail('http://url.com/?skin=rss'); ?>
Being a php newbie, I'm wondering if there is a way to change the http://url.com part based on a custom metadata I have set up in Wordpress. So I guess it would look something like this:
<?php print get_thumbnail('<?=$video_src?>/?skin=rss'); ?>
Is something like this possible?
Yes, you have the right idea, you just don't need to re-open PHP tags since you're already inside some. You can use . to concatenate (join together) the value of $video_src and "?skin=rss".
<?php print get_thumbnail($video_src . "?skin=rss"); ?>
Try this:
<?php print get_thumbnail($video_src . '/?skin=rss'); ?>
Keep in mind that <?= $foo ?> is shorthand for <?php echo $foo; ?>. <?= ?> won't be expanded in strings, but you can achieve something similar using double quoted strings:
<?php print get_thumbnail("$video_src/?skin=rss"); ?>
Yes, except within PHP, you don't need to enter the PHP tags again.
<?php print get_thumbnail($video_src . '/?skin=rss'); ?>

Categories