<?php
if(get_field('member_only_content')){
echo "do something";
}else{
echo "do something else";
}
?>
How do I insert HTML code where it says "do something"? When removing echo "do something"; and pasting the HTML code the Wordpress site crashes.
Close and open the PHP blocks, like this:
if(get_field('member_only_content')){
?>
<html></html>
<?php
} else{
?>
<html></html>
<?php
}
You can also use PHP's alternative syntax for this:
if(get_field('member_only_content')): ?>
<html></html>
<?php else: ?>
<html></html>
<?php endif;
Like this!
<?php
if(get_field('member_only_content')) : ?>
<div>Html stuff</div>
<?php else : ?>
<div>More Html stuff</div>
<?php endif;
Put HTML in place of the string.
<?php
if(get_field('member_only_content')){
echo "<your>HTML here</your>";
}else{
echo "<other>HTML</other>";
}
?>
You can also break out of the PHP tags
<?php
if(get_field('member_only_content')){
?>
<your>HTML here</your>
<?
}else{
?>
<other>HTML</other>
<?
}
?>
That would be because you placed the HTML code within php tags (<?php ?>). Text inside this tag is interpreted as a PHP instruction, thus it won't render HTML. There are 2 general ways render the HTML in a PHP file:
Echo the HTML
<?php
if (get_field ('member_only_content')) {
echo "<span>Put HTML here</span>";
}
else {
echo "<span>Put HTML here</span>";
}
?>
Place the HTML Outside PHP Tags
<?php if (get_field ('member_only_content')): ?>
<span>Put HTML here</span>
<?php else: ?>
<span>Put HTML here</span>
<?php endif;?>
Or you can use the <<< statement:
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
Straight from http://php.net/manual/en/function.echo.php
<?php
if(get_field('member_only_content')){
echo "<div style='background-color: #888888'>This is a simple string between div's, make sure you've to be careful while inserting too many single and double quotes in this string as well as inline styles</div>";
}else{
echo "do something else";
}
?>
Be careful while inserting quotes in your string.
Sample code
echo("<h1>This is a sample</h1>");
Related
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
}
?>
I think it is sometimes easier to use php tags instead of echo for example
<?
if()
echo "<img src='' onclick='alert(\"hello\")'/>";
?>
instead of that I code like this
<?
if(){
?>
<img src='' onclick='alert("hello")'/>
<?}
?>
We got rid of backslashing. But what about strings I want something like this:
<?
$str="?>
<img src='' onclick='alert("hello")'/>
<?";
?>
You should use the PHP heredoc syntax:
<?php
$str = <<<IMGTAG
<img src="" onclick="alert('hello')"/>
IMGTAG;
echo $str;
?>
Enjoy your code.
There is an alternative Syntax specifically for this kind of formation:
<?php if (x): ?>
<div>...</div>
<?php endif; ?>
Also there are short tags:
<?= "hello world" ?>
This directly prints a string and is equal to:
<?php echo "hello world" ?>
For string assignment you can do what Magicianred sugested. You could also do it with output buffering:
<?php ob_start(); ?>
<div>test</div>
<?php
$str = ob_get_contents();
ob_end_clean();
echo $str;
?>
Though output buffering shouldn't be abused for this. Heredoc syntax is the best solution here.
What I would like to do is add a div to the header of my document only if the header is displayed on a certain page, here is what I imagined it might be:
<?php if (is_front_page()) {<div class="spotlight"></div>} ?>
That snippet results in a fatal error Parse error: syntax error, unexpected
Is it possible to use PHP to add a div only on a certain page using PHP, I believe this may be accomplished with JavaScript, but would prefer to use PHP.
Correction:
<?php if (is_front_page()) {?> <div class="spotlight"></div><? } ?>
Or:
<?php if (is_front_page()) {echo '<div class="spotlight"></div>'} ?>
Or:
<?php if (is_front_page()) {echo "<div class=\"spotlight\"></div>"} ?>
<?php if (is_front_page()) print '{<div class="spotlight"></div>}'; ?>
You can do something like this:
<?php if (is_front_page()) {?><div class="spotlight"></div><?php } ?>
OR
<?php if (is_front_page()) { echo "<div class='spotlight'></div>";} ?>
Missing quotes and semi-colon.
Try this
<?php if (is_front_page()) { echo '<div class="spotlight"></div>'; } ?>
<?php do { ?>
<?php echo ""; ?><?php echo $row_pageDetails['name']; ?>(<?php echo $row_pageDetails['profile']; ?>) </br>
<?php } while ($row_pageDetails = mysql_fetch_assoc($rspageDetails)); ?>
This gives a clickable link name(profile) but if the profile is empty It shows () how can I improve it so that when the profile record is empty it shows nothing.
You have many unnecessary opening and closing php tags. You should only use one for this whole thing given your code.
And you have a mis-closed </br> tag, should be <br/> and it would be better if you put it after the closing anchor tag.
You can not show the link at all by putting the whole thing in an if statement
<?php
do {
if(!empty($row_pageDetails['profile'])){
echo "<a href=\"$row_pageDetails[website]\">";
echo $row_pageDetails['name'] . "($row_pageDetails[profile])</a><br/>";
}
} while ($row_pageDetails = mysql_fetch_assoc($rspageDetails));
?>
instead of
(<?php echo $row_pageDetails['profile']; ?>)
use ternary operator
<?php echo ($row_pageDetails['profile']) ? '('.$row_pageDetails['profile'].')' : ''; ?>
Your code must be something like this
<?php do {
echo (isset($row_pageDetails['profile']) && !empty($row_pageDetails['profile']))?
''.$row_pageDetails['name'].'('.$row_pageDetails['profile'].')':''; } while ($row_pageDetails = mysql_fetch_assoc($rspageDetails)); ?>
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 } ?>