Tom
Tom

Reputation: 6004

What is the simplest way to display content in a PHP/HTML website based on an if statement?

What is the simplest way to display content in a PHP/HTML website based on an if statement? In my example, the goal is to show the follow button only if the referer is Twitter. I thought about the way below, is that a good way to do it?

   <?php
    if($referer == "twitter"){
    ?>
   <a href="https://twitter.com/Example" class="twitter-follow-button">Follow</a>    
    <?php
    }
    ?>

Upvotes: 0

Views: 78

Answers (2)

Eduardo La Hoz Miranda
Eduardo La Hoz Miranda

Reputation: 2060

You can set a variable in php and then use the echo statement. You can send a ajax get request from your html using javascript. What you're doing also works.

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

The more "Smarty-style" way of doing it (which is a good idea, even if you're not using templates) is:

<?php if( $referrer == "twitter") { ?>
    <a href="..." class="...">Follow</a>
<?php } ?>

In other words, exactly what you're currently doing but with less whitespace. I believe it's much easier to read this way too, but that's personal preference.

Upvotes: 1

Related Questions