Home > OS >  PHP - Unable to concatenate two variables while generating a php sitemap
PHP - Unable to concatenate two variables while generating a php sitemap

Time:08-15

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

CodePudding user response:

Try this -

$str = $base_url . "category.php?category=" . $subFeaturedPostCatSlug . "&amp;job=" . $subFeaturedPostSlug . "" . PHP_EOL;
echo htmlspecialchars_decode($str);

CodePudding user response:

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;
  • Related