Search code examples
phpincludeinclude-path

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.


Solution

  • 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'); ?>