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
}
?>
Related
I have a while loop and I have a HTML code inside the while loop. Inside this HTML code there is PHP code reading a variable that depends on the condition of the while loop.
This variable should be evaluated by PHP. I wrote the code below to solve this but it doesn't work.
$row[0] depends on the condition of while. My code only outputs an empty space for this variable.
<?php
while ($row = mysql_fetch_row($stt)) {
$html='<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0 <span>likes</span>
</p>
<p>
_____________________
</p>
</div>';
echo $html;
}
?>
If you want to display something row by row, do this:
<?php
while($row = mysql_fetch_row($stt)){
$html="<div class='data'>" . nl2br($row[0]) . "\n<p>comment<a href=''>like</a><a href=''>share</a></p><p>0<span>likes</span></p><p></p></div>";
echo $html;
}
?>
> <?php echo nl2br($row[0]."\n"); ?>
this part should be outside of $html.
The interpreter on server side couldnt run.Accordin to php, this part is not a code block. It is just a text.
I found the answer of my question!!!
This is the solution:
<?php
while($row = mysql_fetch_row($stt)){ ?>
<div class="data">
<?php echo nl2br($row[0]."\n"); ?>
<p>comment
like
share</p>
<p>
0
<span>likes</span>
</p>
<p>
_____________________
</p>
</div>
<?php
}
?>
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
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>");
<?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 } ?>