Error on if else statement - php

I am getting the below error -
Parse error: syntax error, unexpected $end.
This is driving me nuts! All Im trying to do is:
If there is a URL in the database to their website, hyperlink their name.
If not just echo their name. Simple stuff, what am I missing?
<?php
if ($row_clientsP['website']){
?>
<h2><? echo $row_clientsP['customerName']; ?></h2>
<?
}
else
{
?>
<h2><? echo $row_clientsP['customerName'];
}
?></h2>

You likely have an unclosed {.
The could be from elsewhere in the code or from using short tags <?, as noted by Mark Baker. You can check the value (short_open_tag)[http://www.php.net/manual/en/ini.core.php#ini.short-open-tag] in your php.ini. As a rule though, I discourage them.

If you don't have short tags <? enabled these lines will cause that error -
<?
}
AND
<h2><?
It is always best to use <?php tags, as most php installations turn off short tags by default.
see also - http://php.net/manual/en/ini.core.php#ini.short-open-tag

It's almost certainly an issue with the short open tags. They are "on" on my server and the script parses correctly. This also parses correctly and uses the full php tags.
<?php // RAY_temp_smitty.php
error_reporting(E_ALL);
if ($row_clientsP['website']){
?>
<h2><?php echo $row_clientsP['customerName']; ?></h2>
<?php
}
else
{
?>
<h2><?php echo $row_clientsP['customerName'];
}
?></h2>

Try to Use (full php tags)
<?php
if ($row_clientsP['website']){
?>
<h2><?php echo $row_clientsP['customerName']; ?></h2>
<?php
}
else
{
?>
<h2><?php echo $row_clientsP['customerName'];
}
?></h2>

Related

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
}
?>

Using PHP to display a div only if on a certain page

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 echo inside echo

I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>

Get author meta only if filled out in WordPress, basic if statement

I have a WordPress site in which I have added a few extra fields for Author contact fields. Some people are going to use Google plus but some will not. I don't want to have an empty paragraph tag etc. I can't seem to figure out how to write an if statement that works. This is what I was working with. Thanks for your time.
<?php if(get_the_author_meta('google')) ?> { ?>
<p><?php the_author_meta('google'); ?></p>
<?php }
else {
// do nothing
} endif; ?>
<?php if(get_the_author_meta('google')): ?>
<p><?php the_author_meta('google'); ?></p>
<?php
else:
// do nothing
endif; ?>
Did you try this? Also, your code has the opening brace for 'if' without a php tag.
try this - and put a class of some sort on there to ensure it's not showing up in the spots where it's not filled in - a border or something?
<?php if (get_the_author_meta('google')) { ?>
<p class="error"><?php the_author_meta('google') ?></p>
<?php }
else {
// do nothing
} ?>
This seems to work. Maybe it's the endif ?

Insert HTML in if statement

<?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>");

Categories