1
<?php
$request_uri = $_SERVER['REQUEST_URI'] ;

$path=explode("?",$request_uri);

$pname=basename($path[0]);

if ($pname == "blood-facts-for-kids.html") { $p1 = 'Human Body Facts'; $p1u = 'https://www.factsjustforkids.com/human-body-facts.html'; $p2 = 'Blood Facts'; $p2u = 'https://www.factsjustforkids.com/human-body-facts/blood-facts-for-kids.html'; }

echo '<script type="application/ld+json">{"@context":"https://schema.org/","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"{$p1}","item":"{$p1u}"},{"@type":"ListItem","position":2,"name":"{$p2}","item":"{$p2u}"}]}</script>';

?>

I'm having issues getting the variables to appear in my echo. Everything works as it should, the variables are set IF the web page name is correct and if I echo out the variables by themselves using

echo "{$p1}, {$p1u}, {$p2}, {$p2u},";

The correct data is shown. I'm obviously doing something wrong in the echo code.

For reference, this is a crude method to inject structured data dynamically.

Stephen
  • 15
  • 3

2 Answers2

0

You can use
echo $variable_name //to display the variable

And if you want to display it in a HTML tag then

<p>Your age is <?php echo $age ?>.</p> 

Im providing a link for more detailed information https://www.dummies.com/programming/php/how-to-display-php-variable-values/

0

Either use echo with double quotes "":

echo "<script type=\"application/ld+json\">{\"@context\":\"https://schema.org/\",\"@type\":\"BreadcrumbList\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"{$p1}\",\"item\":\"{$p1u}\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"{$p2}\",\"item\":\"{$p2u}\"}]}</script>";

or use concatenation:

echo '<script type="application/ld+json">{"@context":"https://schema.org/","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"' . $p1 . '","item":"' . $p1u . '"},{"@type":"ListItem","position":2,"name":"' . $p2 . '","item":"' . $p2u . '"}]}</script>';
  • Notice that with double-quotes, you need to escape any other double quote inside the string.
Prince Dorcis
  • 955
  • 7
  • 7