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>

6 Answers 6

6

Doing it with native PHP templating:

<?php if ($bodyID==='home') { ?>
    <h1>I am home!</h1>
<?php } else { ?>
    <p>I'm not home!</p>
<?php } ?>
Sign up to request clarification or add additional context in comments.

7 Comments

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() ?
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 ===.
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?
What are you getting as output? What does $bodyID actually contain? Is it even being executed?
The body ID is <body id="home"> with all the HTML in it. Other pages have <body class="internal"> 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".
|
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.

6 Comments

depending on how newbie the OP is, you probably want to use the "<?php" at the beginning of the file.
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
@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.
@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.
@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 pastebin.com/01TCMebV if you check $body_id == 'home' for all intense and purposes $body_id must be a string
|
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; ?>

4 Comments

Thanks, the last piece of code looks like the cleanest, but I can't get it to work. I'm inserting it into a header.php file... Is there something I'm missing?
Update with what code you have at the moment, and echo out $body to see what it contains ?
Sorry Robert, I don't get you?
If you update your quest with the code that your trying to fix, we can take a look at it
1

Assuming $bodyID is a variable:

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

Comments

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.

1 Comment

Thanks iDiot, I wish I could use javascript! This client I've got is strictly no js, so it's a tough one!
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.