[Solved] How to nest php inside if statement


I like this syntax myself ( when working in mixed HTML/PHP )

<?php 
    $blogname = get_bloginfo('name');
    if ($blogname == 'Wobbling Willy'):  //-note- colon not semi-colon
?>
<meta name="description" content="<?= bloginfo('description'); ?>" />
<meta name="keywords" content=”keyword1, keyword2, keyword3” />
<?php endif; ?>

OR

<?php 
    $blogname = get_bloginfo('name');
    if ($blogname == 'Wobbling Willy'){  //open bracket
?>
<meta name="description" content="<?= bloginfo('description'); ?>" />
<meta name="keywords" content=”keyword1, keyword2, keyword3” />
<?php } // close bracket?>

It’s shorter and you don’t have to worry about quoting. Or even use a heredoc
but that involves setting bloginfo('description') to a variable and a few other quirks…

<?php 
    $blogname = get_bloginfo('name');
    if ($blogname == 'Wobbling Willy'){  //open bracket
        $description = bloginfo('description');
        echo <<<HTML
            <meta name="description" content="$description" />
            <meta name="keywords" content=”keyword1, keyword2, keyword3” />
HTML; //nothing can go here no space ( before or after ) and not even this comment, nothing but HTML; litterally
    } 
?>

HEREDOC is my preferred way, but I never mix HTML and PHP in the same file. Because I use a template system.

7

solved How to nest php inside if statement