-2

How can i echo a h1 depending on the variable set on the page? The script below is breaking my page. Looks like my syntax is incorrect

<header  role="banner"> 

     <?php

       if($page =="home_page")    {echo "<h1>" 'Mountain ' "</h1>"; }                               
    else if($page =="parts")    {echo "<h1>" 'parts ' "</h1>"; }

    else if($page =="cars")    {echo "<h1>" 'cars ' "</h1>"; }

          ?>

</header>

Many thanks, P

PtoK
  • 15
  • 3

5 Answers5

0

You have wrong closing and reopening quotes in your strings. Try this

<header  role="banner"> 

     <?php

    if($page =="home_page")    {echo "<h1> Mountain </h1>"; }                               
    else if($page =="parts")    {echo "<h1> parts </h1>"; }

    else if($page =="cars")    {echo "<h1> cars </h1>"; }

    ?>
Maximo
  • 186
  • 5
Tanjima Tani
  • 580
  • 4
  • 18
0
<?php

   if($page =="home_page")    {echo "<h1> 'Mountain ' </h1>";   }                               
else if($page =="parts")    {echo "<h1> 'parts ' </h1>"; }

else if($page =="cars")    {echo "<h1> 'cars ' </h1>"; }

?>
Maximo
  • 186
  • 5
0

You do have errors in your syntax.

Try it this way:

if($page =="home_page"){ echo "<h1>Mountain</h1>"; }    
elseif($page =="parts"){ echo "<h1>parts</h1>"; }
elseif($page =="cars"){ echo "<h1>cars</h1>"; }

It is not a very elegant way for a view, but it should work.

Remember to always read, understand and search the errors that appear in your browser. If they are not evident you can find them moving the code to a more evident place or by doing an element inspection.

0

I've got a way to clean up your code:

switch($page)
{
    case "home_page":
        echo '<h1>Mountain</h1>'
        break;
    case "parts":
        echo '<h1>parts</h1>';
        break;
    case "cars":
        echo '<h1>cars</h1>';
        break;
     default:
        //Add default code, or more cases.
        break;
}
James Spence
  • 2,020
  • 1
  • 16
  • 23
0

You are breaking out of the string in the echo and not handling it. In fact, in this case, there's no reason to break out of the string. Look:

echo "<h1>" 'Mountain ' "</h1>";

The second " is breaking out of the string. PHP is then trying to read 'Mountain ' "</h1>"; as PHP code, which it won't.

echo "<h1>Mountain</h1>";

You see how the string is contained now?

Bitwise Creative
  • 4,035
  • 5
  • 27
  • 35