简体   繁体   中英

Can i put a PHP variable inside a PHP include?

I am unfamiliar with PHP; I suspect this is an easy question. Our web designer is out of town and I need to fix something on one of our web sites.

Basically, I want to be able to use a variable as part of a URL that is being called into a document.

the code:

<?php
$URL = p2 ; /* p1, p2, p3 or no can be used here */
?>

<?php include('includes/WL_cart_*URL*.php'); // calls the page I need?>

I just want to be able to type the correct page in $URL and that text be added to the url in the include. I tried putting the variable directly into the include but that didn't work.

You can achieve this by using double quotes like so:

<?php include("includes/WL_cart_{$URL}.php"); ?>

or if you insist on using single quotes, exiting them, and adding the $URL variable works, like so:

<?php include('includes/WL_cart_'.$URL.'.php'); ?>

要在字符串中添加变量,请使用.

include('includes/WL_cart_' . $URL . '.php');

Use double quotes " and a $ symbol to pull in your variable.

Also bear in mind that your first variable should also be "quoted":

<?php
    $URL = "p2" ; /* p1, p2, p3 or no can be used here */
?>

<?php include("includes/WL_cart_$URL.php"); // calls the page i need?>
<?php include("includes/WL_cart_{$URL}.php"); ?>

但是您需要注意,例如,如果$ url ='../config/database.php.inc',将打开配置文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM