How to include HTML on PHP function, after to be rendered? - php

I need to include html code inside a php function,after to use the function in another html code.
The HTML code should be in another file, i want to call that code in specified row in another file.
HTML code
<span>
<?php
$register_date = new Moment\Moment($u->get("date"), date_default_timezone_get());
$register_date->setTimezone($AuthUser->get("preferences.timezone"));
$format = $AuthUser->get("preferences.dateformat");
if (!$format) {
$format = "Y-m-d";
}
?>
<?= __("Register date: %s", $register_date->format($format)) ?>
</span>
The function
function thefunction (){
//the html code here
}
The code i want to include the function
<?php if ($u->get("expire_date") < "2050"): ?>
<div class="meta">
<?php ?>
function thefunction ()//The function
?>
</div>
<?php endif ?>
Normal code without the function
<?php if ($u->get("expire_date") < "2050"): ?>
<div class="meta">
<span>
<?php
$register_date = new Moment\Moment($u->get("date"), date_default_timezone_get());
$register_date->setTimezone($AuthUser->get("preferences.timezone"));
$format = $AuthUser->get("preferences.dateformat");
if (!$format) {
$format = "Y-m-d";
}
?>
<?= __("Register date: %s", $register_date->format($format)) ?>
</span>
</div>
<?php endif ?>

One (more elegant) approach, all in php, would be:
<?php
//php code
function sayHello (){
echo '<p>Hello</p>';
}
sayHello();
sayHello();
?>
Another approach would be:
<?php
//php code
function sayHello(){ //leave that bracket open ?>
<!--this is html but you're still inside the function. this won't be printed until you call the function-->
<p>Hello</p>
<?php } //function ends here ?>
Then, in any point:
<?php sayHello(); ?>
and your html will be printed

Related

Hide ACF fields when one of them are empty

i use the following code with custom fields:
But the problem is; when did not put any content in one of custom field it need be hide.
the class style of the other effect it, and that is something I don’t want to show.
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
I want hide one of both custom filed when on of them is empty.
How can i hide it?
<?php if (get_sub_field ('filed2') || get_sub_field('filed1'));{ ?>
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
<?php } ?>
You would want to wrap it in an if statement.
You can also do (if you need the conditional logic on a per-field basis):
<?php if (get_sub_field ('filed2'));{ ?>
<div class = "class2">
<? php the_sub_field ('filed2'); ?>
</div>
<?php }; if (get_sub_field ('filed1'));{ ?>
<div class = "class1">
<? php the_sub_field ('filed1'); ?>
</div>
<?php }; ?>

PHP syntax simplify

IN PHP I noticed that if we have code like below:
<?php if ( function('parameter')):?>
<?php //do something here ?>
<?php endif; ?>
why can't we write this code like:
<?php if ( function('parameter'))
//do something here
endif; ?>
I am new to PHP, Thanks a lot!!
The PHP code has to be inside <?php ?> and the HTML markup needs to be outside. You can also print out the HTML markup with echo.
Here is an example (much cleaner in my opinion, than example 2). The HTML markup is inside a PHP string. The return value of the_field(), a string, is then concated with .:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
echo '<p class="btn">Request a Quote</p>';
}
if(get_field('rfq_pdf_url')) {
echo '<p class="btn">Download PDF</p>';
}
?>
And here is another valid example (2). You can end the PHP part with ?> and output regular HTML markup and then start the PHP part again with <?php:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) { ?>
<p class="btn"><a href="
<?php the_field('quote_url'); ?>
">Request a Quote</a></p>
<?php }
if(get_field('rfq_pdf_url')) { ?>
<p class="btn"><a href="
<?php the_field('rfq_pdf_url');?>
">Download PDF</a></p>
<?php }
?>
It would however be redundant to start with <?php on every line and end it then again with ?>.
Another possibility would be:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
?>
<p class="btn"><a href='<?php echo the_field('quote_url'); ?>'>Request a Quote</a></p>
<?php
}
if(get_field('rfq_pdf_url')) {
?>
<p class="btn">Download PDF</p>
<?php
}
?>

Converting a response to string in PHP

Is there anyway to convert an html response to string in php. let me expalin through an example.
<?php
function printTitle($title="Welcome"){
?>
<div class='mainTitle'>
<div class='titleLogo'>
</div>
<div class='titleString'>
<?php echo $title; ?>
</div>
</div>
<?php
}
?>
Here calling this function anywhere will output html showing title.
Now what I need is to convert this response into string value so that it can be passed as json response like:
$response=array("title"=>printTitle(),"sidebar"=>getSideBar());
echo json_encode($response);
I want to do it like this so that I can fetch title and sidebar via ajax.
One way to do it is like:
<?php
function printTitle($title="Welcome"){
$ret="<div class='mainTitle'>
<div class='titleLogo'>
</div>
<div class='titleString'>
". $title ."
</div>
</div>";
return $ret;
}
?>
but this makes html really a mess.
You can use a HEREDOC:
function printTitle($title="Welcome"){
return <<<HTML
<div class='mainTitle'>
<div class='titleLogo'>
</div>
<div class='titleString'>
$title
</div>
</div>
HTML;
}
Use output buffer:
function printTitle($title="Welcome"){
ob_start();
...
return ob_get_clean();
}
Changed from ob_get_flush() to ob_get_clean();

PHP - Placing html in variable on out of php tag

I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>

PHP Easy way to combine PHP if statement with HTML

This probably might be a silly question, but what I am trying to do is to make an if statement to do the following:
<?php if ($_SESSION['login'] == true) { ?>
Display this HTML code (without converting it to PHP echos
<?php } else { ?>
Display this instead
<?php } ?>
Or will I need to echo, and in turn escape all the required characters in order to do what I am after.
Thanks
Just try it out. But for the record, this works. And is in fact an idiomatic way of solving this.
<?php if ($_SESSION['login']) { ?>
<p>Display this HTML code</p>
<?php } else { ?>
<p>Display this instead</p>
<?php } ?>
Indented for readability (however, this messes with the HTML structure indentation so maybe it’s not appropriate).
Alternatively, the following style is often used because the lone brace at the end gets lonely:
<?php if ($_SESSION['login']): ?>
<p>Display this HTML code</p>
<?php else: ?>
<p>Display this instead</p>
<?php endif; ?>
(In both cases I’ve removed the == true from the conditional because it’s utterly redundant. Don’t write == true.)
you can also use if and endif
<?php if ( expression ) : ?>
<p>some message here</p>
<?php else : ?>
<p>other message</p>
<?php endif ?>
Look into the HEREDOC or NOWDOC syntax
<?php
if ($_SESSION['login']) {
$html =<<<HTML
Add HTML here
HTML;
echo $html;
} else {
$other_html =<<<'OTHERHTML'
Add HTML here
OTHERHTML;
echo $other_html;
?>
Anything not in PHP tags will be outputted as HTML anyway, so your original code will work fine.
<?php if ($_SESSION['login'] == true) { ?>
Log Out
<?php } else { ?>
Login
<?php } ?>
Yes, it works.
<?php if ($_SESSION['login'] == true) { ?>
<span>hello</span>
<?php } else { ?>
<span>going already?</span>
<?php } ?>

Categories