0

Currently, as an example, I have this php code on one of my pages.

<?php include("codes/games/scripts/titles/angrybirds.php"); ?>

How would I change this to make my variable:

$gametitle = "angrybirds";

work like this:

<?php include("codes/games/scripts/titles/$gametitle.php"); ?>

Thanks in advance for your help!

4 Answers 4

6

That code should work as-is, since the . won't be interpreted as part of the variable name. You can see the output of it here: http://codepad.org/ZbtiOPgB

However, for readability, I would encourage you to either use clear concatenation:

<?php include("codes/games/scripts/titles/".$gametitle.".php"); ?>

Or wrap your variable name(s) in { and }:

<?php include("codes/games/scripts/titles/{$gametitle}.php"); ?>
Sign up to request clarification or add additional context in comments.

Comments

3
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>

Comments

0

Or:

<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>

Comments

0
<?php 
$file = 'codes/games/scripts/titles/' . $gametitle . '.php';

if (file_exists($file))
    require_once($file);
?>

1 Comment

I'm assuming that the game title is dynamic user input, which is why I suggest using file_exists(), so that if the game title is phony it won't throw errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.