-2

Possible Duplicate:
php - insert a variable in an echo string

I have a link that when it is clicked calls the next page and should pass two varibles to the page, the first is Manu and the second is fuel_type.

The problem is I can not seem to get the code to identify the variables and instead it passes the dollar sign and the variable name and not the value.

The code is below, i have tried writing this several different ways, but I am just running out of ideas.

 .'<a href=\act/manufacturer.php?manufacturer=$manu&fuel_type=$fuel_type>'.$manu.'</a>'.
Community
  • 1
  • 1
Stan Williams
  • 263
  • 3
  • 14
  • 4
    You forgot to read the PHP documentation. The PHP manual quite clearly explains how variables are interpolated inside strings. In this case the difference between `'` and `"` is key. Please go read the manual... – Lightness Races in Orbit Nov 05 '12 at 01:14

5 Answers5

4

First, be sure that the variables contain what you want (from the superglobal GET variable):

$manu = $_GET['manu'];
$fuel_type = $_GET['fuel_type'];

Then, you have two options:

  1. Use double quotes " instead of single quotes, variables inside of quotes will be expanded

    "My dog is named $dog_name, and is $dog_age years old";
    
  2. Concatenate the strings inside single quotes with variables:

    'My dog is named ' . $dog_name . ', and is ' . $dog_age . ' years old.';
    

Be absolutely certain, however that you sanitize the variables before using or displaying them to the browser. At a minimum, you want any HTML/JS code removed, then some checking to make sure the result is of the type and length that you expect. There are quite a few questions on SO discussing ways to do this, a quick search for [php] sanitize GET variables will turn them up.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
2

Single quotes do not parse variables, while double quotes do. Change your quotes around, and your link should work correctly.

Example:

."<a href=\act/manufacturer.php?manufacturer=$manu&fuel_type=$fuel_type>".$manu.'</a>'.
Daedalus
  • 7,586
  • 5
  • 36
  • 61
2

you have to insert the var outside the '

like this:

.'<a href=\act/manufacturer.php?manufacturer='.$manu.'&fuel_type='.$fuel_type.'>'.$manu.'</a>'.
gabrielem
  • 560
  • 5
  • 13
2

Single quotes dont allow parsing of variables, double-quotes do. So you'd be better off doing something like:

"<a href=\act/manufacturer.php?manufacturer=$manu&fuel_type=$fuel_type>$manu</a>";

-- or in your case --

'<a href=\act/manufacturer.php?manufacturer='.$manu.'&fuel_type='.$fuel_type.'>'.$manu.'</a>'
nickhar
  • 19,981
  • 12
  • 60
  • 73
-1

You can wrap your variables around curly braces to escape variable expressions:

'<a href=\act/manufacturer.php?manufacturer={$manu}&fuel_type={$fuel_type}>'.$manu.'</a>'.
Lock
  • 5,422
  • 14
  • 66
  • 113