-1

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.

okyanet
  • 3,106
  • 1
  • 22
  • 16
Penfold0101
  • 87
  • 1
  • 1
  • 9

4 Answers4

4

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'); ?>
Stefan Candan
  • 881
  • 6
  • 10
2

To add a variable inside a string you use .

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

Toon Casteele
  • 2,479
  • 15
  • 25
0

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?>
msturdy
  • 10,479
  • 11
  • 41
  • 52
0
<?php include("includes/WL_cart_{$URL}.php"); ?>

But you need take care, for example, if $url = '../config/database.php.inc', will open the config file.

Bruno Alano
  • 643
  • 1
  • 11
  • 21