Nick
Nick

Reputation: 9373

PHP if statement with links

I am trying to write an IF statement so that when a user is on a certain page there will be additional content displayed. I have written this:

<?php
if (strpos($_SERVER['PHP_SELF'], 'about.php')){
        <ul class="vertical-list">
        <li><a href='/' class='button' onclick="return false;">Work Experience</a></li>
        <li><a href='/' class='button' onclick="return false;">Education</a></li>
        <li><a href='/' class='button' onclick="return false;">Skills</a></li>
        <li><a href='/' class='button' onclick="return false;">Portfolio</a></li>
        </ul>;
}
?>

So when a user is on the about page a navigation list will populate in the side bar but will be hidden if a user navigates away from the about page. The current code gives me this error: Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\includes\sidebar.php on line 54

Upvotes: 1

Views: 573

Answers (3)

Jemaclus
Jemaclus

Reputation: 2376

You need to close the PHP tag after the if statement, like so:

  <?php
  if (strpos($_SERVER['PHP_SELF'], 'about.php')) { ?>
       <ul>
           ...
       </ul>

  <?php
  }
  ?>

You can't put regular HTML inside PHP tags unless you echo it out or close the PHP tags. I personally prefer to close the PHP tags, since it makes reading the HTML easier.

Hope that helps. Good luck.

Upvotes: 1

VettelS
VettelS

Reputation: 1224

You have forgotten to actually display the HTML. Either use echo or drop out of PHP to display the HTML.

Upvotes: 2

Matt H
Matt H

Reputation: 6532

PHP's strpos returns a 0-base successful result, so you need to properly test for the "none found" result, AND you need to close and re-open your PHP tags:

<?php
if (strpos($_SERVER['PHP_SELF'], 'about.php') !== false){ 
?>
        <ul class="vertical-list">
        <li><a href='/' class='button' onclick="return false;">Work Experience</a></li>
        <li><a href='/' class='button' onclick="return false;">Education</a></li>
        <li><a href='/' class='button' onclick="return false;">Skills</a></li>
        <li><a href='/' class='button' onclick="return false;">Portfolio</a></li>
        </ul>;
<?php
}
?>

Upvotes: 2

Related Questions