PHP if / else statement

I am a complete newbie to PHP, but found it can be very useful. How to write the following statement in PHP:

If the identifier is body = "home", then insert some html, for example.

<h1>I am home!</h1>

Otherwise, paste this html:

<p>I'm not home.</p>

Thank!

+3
source share
5 answers

Doing this with the built-in PHP template:

<?php if ($bodyID==='home') { ?>
    <h1>I am home!</h1>
<?php } else { ?>
    <p>I'm not home!</p>
<?php } ?>
+5
source

You can try:

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

echo $html;

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

+1
source

:

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

default , $body case, , default .

, , if/else, / :

<?php if ($body == 'home'):?>
    <h1>I am home!</h1>
<?php else:?>
    <p>I'm not home!</p>
<?php endif; ?>
+1

, $bodyID :

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

, (, $body - ) - javascript, "" JS PHP .

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

, body.id $_GET... , , body.id.

, .

0

Source: https://habr.com/ru/post/1773211/


All Articles