-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

5
  • 2
    Why do you say your syntax is incorrect? What happens when you run this code? (I see an obvious syntax error three times in your string values). Commented Feb 18, 2015 at 21:30
  • 1
    No, it's not correct. your echoes are flat-out syntax errors. RTFM: php.net/echo Commented Feb 18, 2015 at 21:32
  • Add error reporting: <?php ini_set("display_errors", 1); error_reporting(E_ALL); ?> at the top of your file(s) and php will give you the answer Commented Feb 18, 2015 at 21:33
  • Hi guys , its breaking the page completely. sorry I'm new to php so errors are a little confusing Commented Feb 18, 2015 at 21:34
  • php.net/manual/en/… Commented Feb 18, 2015 at 21:34

5 Answers 5

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>"; }

?>
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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;
}

Comments

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?

Comments

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>"; }

    ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.