1

Whats wrong with this HTML / PHP - just doesnt look right - Is there a better way to concatenate html and php variables into url string ???

<a href="movie_night_del.php?id=<?php echo $id ?>&mov=<?php echo $mov ?>">Click here to delete</a>
2
  • 1
    no need of concatenation here, use as <?php echo "movie_night_del.php?id=$id&mov=$mov"; ?> Commented Jan 21, 2016 at 15:12
  • there's nothing wrong with it. you've just chosen to echo each varaible separately. you can choose to build bigger strings in php and output the whole thing, instead of small chunks. how you do that is up to you, it's purely a code readability issue, which boils down to opinion. Commented Jan 21, 2016 at 15:18

2 Answers 2

2

I prefer to do it in "more php" way, for example:

echo '<a href="movie_night_del.php?id=' . $id . '&mov=' . $mov . '">Click here to delete</a>'
//or
echo "<a href='movie_night_del.php?id={$id}&mov={$mov}'>Click here to delete</a>"
//or escaping "
echo "<a href=\"movie_night_del.php?id={$id}&mov={$mov}\">Click here to delete</a>"
Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in the comments, there is nothing wrong doing it like that but your values could break the query string if they contain characters that need to be encoded.

For example the & and the = have special meanings, so if they could appear in your variable values, they would break the query string.

You can escape individual values using:

... &mov=<?php echo urlencode($mov) ?> ....

Or you could have php build and encode your string automatically using http_build_query:

$data = array(
  'id' => $id,
  'mov' => $mov
);
$url = 'movie_night_del.php?' . http_build_query($data);

Perhaps the last option is what you were looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.