如何包含 php 变量网址


How to include php variable url?

目前,作为一个例子,我的一个页面上有这个php代码。

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

我将如何更改它以创建我的变量:

$gametitle = "angrybirds";

像这样工作:

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

提前感谢您的帮助!

该代码应按原样工作,因为.不会被解释为变量名称的一部分。你可以在这里看到它的输出:http://codepad.org/ZbtiOPgB

但是,为了可读性,我鼓励您使用清晰的串联:

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

或者将您的变量名称包装在 { 中并}

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

或者:

<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
<?php 
$file = 'codes/games/scripts/titles/' . $gametitle . '.php';
if (file_exists($file))
    require_once($file);
?>