-1

Please take a look at this:

$order = isset( $_GET['o'] ) ? '?o='.$_GET['o'].'&p=' : '?p=';
echo "<a href='$order.1'>1</a> ";

As its obvious in the fiddle, here is the output:

<a href='?p=.1'>1</a> 
//          ^ this

But as I've commented in the code above, that dot *(which acts as a separator there) should be removed. So this is expected result:

<a href='?p=1'>1</a> 

And when I remove it, the variable name will be combined to that number and PHP cannot find expected variable anymore.

Anyway, is there any workaround?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89

4 Answers4

3

Use the complex string syntax like this.

"<a href='{$order}1'>1</a> ";
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

You have a string concatenation issue. It can be fixed by the following way

<?php
    $order = isset( $_GET['o'] ) ? '?o='.$_GET['o'].'&p=' : '?p=';
    echo "<a href='".$order."1'>1</a> ";
?>
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

You can use this to not concat variable with other string:

echo "<a href='${order}1'>1</a> ";
Mohammad Hamedani
  • 3,304
  • 3
  • 10
  • 22
0

Your problem is here:

echo "<a href='$order.1'>1</a> ";

Try this instead:

echo '<a href="' . $order . '"1>1</a>';

If you can avoid using double quotes with strings, since PHP will always check for variables there. IMO it takes no more time to use single quotes and simple concatenate the values. I actually prefer to use this:

echo sprintf('<a href="%s%d">%d</a>', $order, 1);

For more details check here.

lloiacono
  • 4,714
  • 2
  • 30
  • 46