How to nest php inside if statement - php

Probably a newbie thing but I can't get the meta name="description" row to work. You should close and open PHP-code if its nested, right?
Thanks!
/Robert
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy') {
echo '<meta name="description" content="?><?php bloginfo('description'); ?><?php" />'."\n";
echo '<meta name="keywords" content=”keyword1, keyword2, keyword3” />'."\n";
}
?>

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.

Try this:
<?php
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy') {
echo '<meta name="description" content="'.bloginfo('description').'"/>\n';
echo '<meta name="keywords" content="keyword1, keyword2, keyword3"/>\n';
}
?>

In reply to the OP's question about nested PHP tags, the nesting of PHP opening and closing tags only applies to doing so within HTML code, as follows:
<div id="<?php echo $id; ?>">
Note: this style of mixing HTML and PHP together was far more popular years ago; nowadays this is not considered the best style. Instead, the emphasis is on a separation of concerns, as reflected in the model-view-controller design pattern. And, nesting PHP code within PHP is not the thing to do and will likely produce a parse error.
Of the various ways to fix the code, you may wish to view my live demo which makes use of heredoc syntax.
What is interesting about the OP's snippet is that it actually does not have any PHP tags nested within PHP code. In fact, if you remove the superfluous tags, allowing for only one open tag, the code just needs minor tweaking to work, as follows:
// these function implementations are just for demo purposes:
function get_bloginfo($n) {
return ("Wobbling Willy");
}
function bloginfo($d){
return("some lovely description ...");
}
$blogname = get_bloginfo('name');
if ($blogname == 'Wobbling Willy') {
echo '<meta name="description" content="';
echo bloginfo('description'),'"/>',"\n";
echo '<meta name="keywords" content="keyword1, keyword2, keyword3" />'."\n";
}
See demo
The return value of bloginfo() will not appear unless provided as a parameter to a function or construct, such as echo, which will output it. Splitting up the display of the "description" meta tag while awkward will work provided that one uses the single quotes as needed. Lastly, curly quotes can be problematic and in this case I replaced those enveloping the second meta tag's content with straight ones to enclose the keywords.

Related

How to fix html in php echo

I'm having trouble using the echo function in php. I don't fully understand when double quotes, single quotes, and escaping quotes are necessary. I've tried several variations and I can't seem to get it to show up. I know how to echo basic html, but I've not used classes before, I think that might be the dealbreaker here.
Edit: flagged for being a duplicate, I'm new here, not really sure what to do. But it's not duplicate post, the other post uses elements that I'm not familiar with using, this was just straight html. Thank you.
<?php echo "<div id=\"resume-links\" style='display:flex;float:right;''>
<a class='button button--type-action--size-medium' href='/myaccount' style='margin-right:10px;''>Dashboard →</a>
<a class='button button--type-action--size-medium' href='#popmake-1301'>Get Featured →</a>
</div>
<div id=\"resume-link\" style='clear:both;float:right;padding-top:10px;font-style:italic;'><p><a href='/article-submissions'>Learn More</a></p></div>";?>
You can use
<<<START
htmlHere
START;
And after margin-left you hav 2 ' but must 1
you can wrap single quotes inside double quotes
echo " <div id='resume-id'>qwerty</div> ";
you can wrap double quotes inside single quotes
echo '<div id ="resume-id">qwerty</div>';
you can use escape character when your wrapper quotes are same as inner quotes
echo "<div id =\"resume-id\">qwerty</div>";
or
echo '<div id ="resume-id">qwerty\'s</div>';
output : qwerty's
Use Heredoc syntax.
Example :
$str = <<<EOD
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
</head>
EOD;
Documentation

PHP script not working for meta description? Update for twitter cards

I'm just trying to make this random script to make meta, open graph and twitter cards eisier together.
This is my PHP Code
<?PHP
$Title = "This is the Open Grah script";
$Description = "This is the one and only Description, or better yeh the first 150 charecters of the main content on your page. What ever you feel like really!";
echo "<html>
<head>
<title> $Title </title>";
echo '<meta name="description" content=" ';
echo '$Description';
echo '" />';
?>
But this what happens in the source code when in the browser:
<html>
<head>
<title> This is the Open Grah script </title><meta name="description" content=" $Description" />
So basically it's not printing the $Description variable.
Update
<?PHP
$Title = "This is the Open Grah script";
$Description = "This is the one and only Description, or better yeh the first 150 charecters of the main content on your page. What ever you feel like really!";
$Twitter = "#obama";
echo "<html>
<head>
<title> $Title </title>";
echo '<meta name="description" content=" ';
echo $Description;
echo '" />';
echo "<meta name="twitter:card" content="summary" />";
echo '<meta name="twitter:site" content"';
echo $Twitter
echo '"/>';
echo '<meta name="twitter:title" content"';
echo $Title;
echo '" />';
echo '<meta name="twitter:description" content" ';
echo $Description;
echo '" />';
?>
So it worked at first but it's not working now, don't really see the error.
print a php variable with single quote '$Description' it assume as string not a variable so you should do wrap it in double quotes "$Description".
Change
echo '$Description';
to
echo "$Description";
Or simple echo the variable
echo $Description;
instead of using like this echo '$Description'; use like this echo $Description;
In single quotes in PHP the dollar sign isn't interpretted, you double-quotes, or nothing:
echo $Description;
OR
echo "$Description";

how to echo contents from body to header for meta tag

I am trying something not sure how to go about it
I have a header.php file
I included it in the pages so now i want to add meta tag but don't want the same description and keyword.
If I use a div tag where I place the contents in the body I want to show from the description in search engine of that page then echo it in the header file would that work and what is the coding to use?
Thanks hope you understand
In your header.php file, define a function that creates the header:
function header($description, $keywords) {
...
echo "<meta name='description' content='$description' />";
echo "<meta name='keywords' content='$keywords' />";
...
}
Then the pages that use this will do:
require 'header.php';
header($this_description, $these_keywords);
It can later uses these same variables when it creates the DIV.
in your header.php:
echo '<meta name="description" content = "'.$description.'">';
echo '<meta name="keywords" content = "'.$keywords.'">';
in your index.php or other pages that use the header.php file:
$description = "This is a cool site about simplicity of PHP :)";
$keywords = "PHP,Specify, THE, $variables, before, Include";
include('header.php');
//other elements :)
in your page put this
$pageTitle = 'Services';
$keyword = "Books a Party Hall and Room Rentals in Toronto";
$description = "Books a Party Hall and Room Rentals in Toronto. Halls rental for Events, Adult & Children's Birthday Parties, Photography, Wedding, Music and Fashion Show Shoots, Corporate Presentations, Baby Showers, Going Away and Engagement Parties.";
include 'header.php';
in your header put this code
<title><?php echo $pageTitle; ?></title>
<meta name="keywords" content="<?php echo $keyword; ?>">
<meta name="description" content="<?php echo $description; ?>">

Return big HTML block with some php without make string

How can i return big html block with some php by using <<<HTML HTML; .
return <<<HTML
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php } ?>
<?php echo $whatever; ?>
HTML;
I can't understand what will work and what will not! how should i use this kind of return <<<HTML HTML; block with some php variable that i need to echo and some function that echo some thing (not return)
You can use 'capture output' for this task. see Output Control Functions
i has some example code that i have just tested. It captures the output of the div tag in $out1 and shows it again later.
This technique is used in many 'templating' libraries and in 'views' in the 'frameworks'.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test of Output control functions</title>
</head>
<body>
<?php ob_start(); // capture the buffer ?>
<div style="border: 4px solid red">
<p>This is a test paragraph</p>
<p>This is test PHP code: <?php echo time(); ?></p>
</div>
<?php $out1 = ob_get_contents(); // end capture ?>
</body>
</html>
<?php echo $out1; // output now or save for later. ?>
<?php var_dump($out1, strlen($out1)); ?>
<?php exit; ?>
Okay, google heredoc syntax for PHP.
but this is how it works (which I think you are trying to do.
$html = <<<HTML
<div>
<h1>$phpVariableTitle</h1>
<div>
{$thisFunctionEchosomthingNotReturn()}
</div>
</div>
HTML;
return $html;
Try that. IMPORTANT! heredoc syntax requires your closing tag be left aligned with no tabs. So make sure there are no spaces or tabs to the left of your heredoc tags, in this example my heredoc tags are called HTML. Also, wrapping your php variables/functions with curly braces is optional but good practice for this method. NO PHP tags in side heredoc block.
Hope that helps.
To make a conditional statement work inside you need to use a function:
class My_Class {
public function myCondition($param) {
if($param === true) {
return '<p>True</p>';
} else {
return '<p>False</p>';
}
}
}
$object =new My_Class();
$html = <<<HTML
<div>
<h1>Conditional Statement</h1>
<div> {$object->myCondition(true)} </div>
</div>
HTML;
something like that should work. But I haven't tested it.
I am unable to understand your question properly may be this may help:
<HTML>
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn();
if($isflag){?>
<span>DO not do this</span>
<?php }//Closing If if it ends here.
echo $whatever; ?>
</HTML>
You cannot write control structures / functions logic inside of HEREDOC syntax.
Alternate way..
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php echo $whatever; }?>

PHP function value from html text into META tags

Hey I have a somewhat simple, i think, question... I am trying to pass html text into a PHP variable so that I can use the value over and over again without having to change the META tags, title and description for every single page entity which would all be different. I want to keep it all condensed into one header.php page which is always called, but the Meta tags must change according to the pages title and description.
so for example,
page 1 title = Chicken
index.php:
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/test/general.php');
include("header.php");
?>
<ul>
<li class="title"><?php $title = '?>Chicken<?php ';?> </li>
</ul>
</body>
</html>
general.php:
<?php
function siteinfo( $show='' ) {
echo get_siteinfo( $show, 'display' );
}
function get_siteinfo( $show = '', $filter = 'raw' ) {
switch( $show ) {
case 'title':
$output = $title;
break;
return $output;
}
?>
header.php:
<html>
<head>
<meta name="title" content="<?php siteinfo( 'title' ); ?>" />
</head>
<body>
i know this part is really jacked up, not sure how it should work... essentially, i want the HTML to be the function value: <?php $title = '?>Chicken<?php ';?> but of course, the ' and second enclosing ' kill the whole function.
thanks!
You're overthinking the problem a bit. Something like this should work for your purposes:
index.php:
<?php
$title = 'Chicken';
include("header.php");
?>
<ul>
<li class="title"><?php echo $title; ?></li>
</ul>
</body>
</html>
header.php:
<html>
<head>
<meta name="title" content="<?php echo $title; ?>" />
</head>
<body>

Categories