1

while trying to generate dynamic sitemaps, I tried adding two variables in url path, and the line is giving me error

this is my sample line:

echo "<loc>" . $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . "$subFeaturedPostSlug" . "</loc>" . PHP_EOL;PHP_EOL;

I tried doing it like this also:

echo "<loc>{$base_url}category.php?category={$subFeaturedPostCatSlug}&job={$subFeaturedPostSlug}</loc>" . PHP_EOL;

error screenshot attached; error screenshot attached

Any help will be appreciated, thanks in advance

aynber
  • 22,380
  • 8
  • 50
  • 63
Yaser
  • 15
  • 3
  • The problem appears to be that you neglected to escape the characters that have special meaning in XML - the `&` being one of them. This is not a PHP error to begin with (not actually _"Unable to concatenate two variables"_), it is an issue of creating incorrect output. – CBroe Aug 15 '22 at 12:15
  • thanks for answering so quickly @CBroe any idea how to fix this? because if I remove this & variable the xml is generating just fine – Yaser Aug 15 '22 at 12:19
  • The `&` needs to be `&` in the resulting XML. You can use `htmlspecialchars` for this, same as you would for HTML. – CBroe Aug 15 '22 at 12:23

2 Answers2

0

Try this -

$str = $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&amp;job=" . $subFeaturedPostSlug . "" . PHP_EOL;
echo htmlspecialchars_decode($str);
Manik
  • 264
  • 2
  • 8
0

You should be able to fix this using the urlencode() function as mentioned in your comments.

So,

echo "<loc>" . $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . "$subFeaturedPostSlug" . "</loc>" . PHP_EOL;PHP_EOL;

becomes

echo "<loc>" . urlencode($base_url) . "category.php?category=" . urlencode($subFeaturedPostCatSlug) . "&job=" . urlencode($subFeaturedPostSlug) . "</loc>" . PHP_EOL.PHP_EOL;

More details at PHP Documentation for urlencode()

Also, I found out that there is error in your code:

echo "<loc>" . $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&job=" . "$subFeaturedPostSlug" . "</loc>" . PHP_EOL;PHP_EOL;

Towards the end of the echo, you have written:

...PHP_EOL;PHP_EOL;

which should ideally have been

...PHP_EOL.PHP_EOL;
Shammi Shailaj
  • 445
  • 5
  • 14