3

How can I write the following statement in PHP:

If body ID = "home" then insert some html, e.g.

<h1>I am home!</h1>

Otherwise, insert this html:

<p>I'm not home.</p>
Dharman
  • 30,962
  • 25
  • 85
  • 135
lnvrt
  • 732
  • 3
  • 11
  • 23

6 Answers6

5

Doing it with native PHP templating:

<?php if ($bodyID==='home') { ?>
    <h1>I am home!</h1>
<?php } else { ?>
    <p>I'm not home!</p>
<?php } ?>
bobince
  • 528,062
  • 107
  • 651
  • 834
  • 2
    At 92K you should know your comparison operators sir, should always use `==` as a Equal check rather then an `Identical`, you don'y know if the OP is using objects that uses `__tostring()` ? – RobertPitt Nov 05 '10 at 10:02
  • Why would I *want* an object that uses `__tostring()` to be equal? That would seem to cause more bugs that it would solve. (Personally, I would prefer an exception if I compare a string to a non-string, as it would almost certainly mean I've made a mistake.) Meanwhile, the equality operator brings with it some seriously bad baggage (like string-as-number comparison) that I definitely *don't* want. The more-often-likely-to-be-correct comparator is `===`. – bobince Nov 05 '10 at 10:18
  • Thanks bobince. I have tried this but it is not working for me either. I am inserting it into a header.php template file. Is there anything I'm missing? – lnvrt Nov 05 '10 at 10:27
  • What are you getting as output? What does `$bodyID` actually contain? Is it even being executed? – bobince Nov 05 '10 at 10:43
  • The body ID is with all the HTML in it. Other pages have and they are all reading from the same header.php file which I am inserting your code into. The output I'm getting from your code is the same on all pages, "I'm not home". – lnvrt Nov 05 '10 at 11:03
  • Oh, you can't read the value of an `` attribute that's being generated by a PHP template, from another PHP template. You will need to explicitly set an ID in a variable, `$bodyID= 'home';`, before including the header template. – bobince Nov 05 '10 at 11:10
  • OK, pardon my ignorance! Now if I can get these php IDs to work I'm sure this method should work. Thanks for your help! – lnvrt Nov 05 '10 at 11:28
1

You can try using this :

$html = '';
if ( $body_id === 'home' )
{
    $html .= '<h1>I am home!</h1>';
}
else
{
    $html .= '<p>I\'m not home.</p>';
}

echo $html;

This will echo the html code depending on the $body_id variable and what it contains.

Etienne Marais
  • 1,660
  • 1
  • 22
  • 40
  • depending on how newbie the OP is, you probably want to use the " – Oxyrubber Nov 05 '10 at 09:45
  • Its always good practice to assign the HTML code to a variable and dump it all at once when execution is done, but thats debatable for different types of output required – Etienne Marais Nov 05 '10 at 09:47
  • @Hannes Its good practice to always type check in an if-statement. It keeps your code to a standard and does not affect performance in any way what so ever for a script this small. – Etienne Marais Nov 05 '10 at 09:49
  • @Hannes: the double-equals operator is crazy and quite unsatisfactory. `'123'==' 123'`. @etbal: I would avoid putting output HTML in strings. You just give yourself an extra layer of escaping to worry about. PHP is a templating language, might as well use it. – bobince Nov 05 '10 at 09:56
  • @etbal @bobince but in this particular case the typecheck serves no purpose and in fact under certain conditions can even break the code, please consider this example http://pastebin.com/01TCMebV if you check `$body_id == 'home'` for all intense and purposes $body_id must be a string – Hannes Nov 05 '10 at 11:01
  • @Hannes: I specifically **do not want** custom class instances to compare successfully against the string. If I have received a custom object in what I expect to be a string variable, it is a mistake and I want it to fail. What I want 99% of the time when I compare a string value is a simple string comparison, like in the vast majority of other programming languages. PHP's `==` is not a string comparison, it is a crazy schizophrenic thing that may not go wrong in this particular case but definitely will in others. It is unreliable and it use should be the exception, not the rule. – bobince Nov 05 '10 at 11:43
1

You can use a switch command like so:

switch($body)
{
    case 'home': //$body == 'home' ?
        echo '<h1>I am home!</h1>';
    break;

    case 'not_home':
    default:
        echo '<p>I'm not home.</p>';
    break;
}

The default means that if $body does not match any case values, then that will be used, the default is optional.

Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:

<?php if ($body == 'home'):?>
    <h1>I am home!</h1>
<?php else:?>
    <p>I'm not home!</p>
<?php endif; ?>
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
1

Assuming $bodyID is a variable:

<?php 
    if ($bodyID==='home') {
        echo "<h1>I am home!</h1>";}
    else {
        echo "<p>I'm not home!</p>";}
?>
Leah
  • 336
  • 4
  • 14
0

Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.

<script language="javascript">
<!--
if( document.body.id === "home" ){
    window.document.write("<h1>I am home!</h1>") ;
}
else{
    window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>

otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.

Hope this will be usefull & clear.

iDiot
  • 5
  • 1
  • 3
  • Thanks iDiot, I wish I could use javascript! This client I've got is strictly no js, so it's a tough one! – lnvrt Nov 05 '10 at 11:47
0

you can try in the following way:

$body_id = "home";

if ($body_id == "home") {
    echo "I am home!";
} else {
    echo "I am not home!";
}

or

$body_id = "home"; 

if (strcmp($body_id, "home") !== 0) { 
    echo 'I am not home!'; 
} 
else { 
    echo 'I am home!'; 
} 

Reference:

https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/