Parse error: syntax error, unexpected '{' in if() - php

I'm getting Parse error: syntax error, unexpected '{'
Here is my code (used in a wordpress comment loop)
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) { ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php } ?>

Try:
<?php if (current_user_can('edit_comment',$comment->comment_ID)) : ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php endif;?>

I'm a big fan of sticking to one language at a time. Context switches are confusing.
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
printf( "<a href='%s' title='Edit Comment'><img src='%s/images/edit-link.php'/></a>",
get_edit_comment_link(),
get_template_directory_uri()
);
}
?>
Though this is personal preference, and I don't see anything technically wrong with the code in your question.

Try this
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"get_edit_comment_link()\" title=\"Edit Comment\">
<img src=\"get_template_directory_uri()/images/edit-link.png\"/>
</a>";
} ?>
also why not
<?php $a=get_edit_comment_link(); $b=get_template_directory_uri();
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"$a\" title=\"Edit Comment\">
<img src=\"$b/images/edit-link.png\"/>
</a>";
} ?>

Related

How to add else into PHP Code line?

I have this following code
<td>
<?php if($rec->telegram){ ?><img class="social" src="<?php echo base_url(); ?>social/telegram.jpg" /><?php } ?>
<?php if($rec->google_pluse){ ?><img class="social" src="<?php echo base_url(); ?>social/g.jpg" /><?php } ?>
<?php if($rec->instagram){ ?><img class="social" src="<?php echo base_url(); ?>social/insta.jpg" /><?php } ?>
<?php if($rec->facebook){ ?><img class="social" src="<?php echo base_url(); ?>social/f.jpg" /><?php } ?>
</td>
I would like to add "else" into this code that if ALL those icons facebook, instagram, google plus and facebook weren't available then it will show another image file like "nophoto.jpg"
Note: If all those icons (4 icons) weren't available then it will show nophoto.jpg
I highly appreciate if someone guide me how to add else into above code.
Regards
It'll be easier to just add an extra if:
if (!($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus)) { //etc
But really you want to attach that kind of logic to the $rec object, so that it's neatly separated and it's easier to add new types of social media in the future. Hopefully your $rec object is indeed backed by a class and not just a stdClass from a database result or cast.
public function hasNoSocialIcon() {
return !($rec->telegram || $rec->instagram || $rec->facebook || $rec->google_plus);
}
And then use that in your template:
<?php if ($rec->hasNoSocialIcon()) { ?>
<img class="social" src="<?php echo base_url(); ?>social/nophoto.jpg" />
<?php } ?>
You should take the use of PHP's conditional operators like this:
<?php
$icons_available = (
!empty($rec->telegram) &&
!empty($rec->google_plus) &&
!empty($rec->instagram) &&
!empty($rec->facebook)
);
?>
<td>
<?php if(!$icons_available) { ?>
<a href="default-pic.jpg">
<img class="social" src="default-pic.jpg" />
</a>
<?php } else { ?>
<?php $url = echo base_url() . 'social/telegram.jpg'; ?>
<a href="<?php echo $url; ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/google_plus.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/instagram.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php $url = echo base_url() . 'social/facebook.jpg'; ?>
<a href="<?php echo $url ?>">
<img class="social" src="<?php echo $url ?>" />
</a>
<?php } ?>
</td>
Hope this helps!

PHP WordPress if else not working unexpected syntax

I have a PHP if else statement however it does not seem to work and I'm not sure why.
Here is my code
<?php
if (has_post_thumbnail()) {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php
the_post_thumbnail('full', array('class' => 'img-responsive alignleft'));
?>
</a>
<?php
} else () {
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php
}
?>
And here is the error that I get syntax error, unexpected ')'
I'm not sure where the unexpected ) is coming from.
I am basing my PHP on this structure, however editing it as I would like to be able to put mine into HTML without using echo
<?php if ( '' != get_the_post_thumbnail() ) {
// some code
}
else {
// some code
}
?>
Let's try with this:
<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?>
</a>
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php endif; ?>
I'm not sure why you wrote your code this way instead of concatenating it, but your if else statement has two parenthesis after else "()". You might want to get rid of these to fix your issue.
In PHP else is not a function, so you shouldn't call it like you did.
PHP's elseif () needs conditions,
try else instead:
<?php
if ( has_post_thumbnail() ) {
?>
<?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive alignleft' ) ); ?>
<?php
} else { //instead of elseif () here, use else
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />
<?php
}
?>
Please also refer to the WordPress Coding Standards to make your code easier readable and maintainable. And try not to edit the original code in your question without marking the edits clearly, otherwise SO users won't be able to understand your problem/help you further.

Echo mix of HTML and PHP giving an error

I'm trying to echo this long statement, tried looking at other answers here, nothing is fixing it.
Error:
Parse error: parse error, expecting `','' or `';''
Errors parsing -
.
<?php
$videoEmbed = get_post_meta( get_the_id(), 'ctslider_videoembedcode', true );
$postThumb = the_post_thumbnail();
echo '<div onclick="thevid=document.getElementById('thevideo'); thevid.style.display='block'; this.style.display='none'"><img src="'.echo $postThumb.'" style="cursor:pointer" /></div><div id="thevideo" style="display:none">"'.echo $videoEmbed.'"</div>';?>
You have to escape your '
So it gives:
echo '<div onclick="thevid=document.getElementById(\'thevideo\'); thevid.style.display=\'block\'; this.style.display=\'none\'"><img src="'.$postThumb.'" style="cursor:pointer" /></div><div id="thevideo" style="display:none">"'.$videoEmbed.'"</div>';
And you don't need echo inside the first echo
Do you realy need to echo a div?
Can't you do that :
<div onclick="thevid=document.getElementById('thevideo'); thevid.style.display='block'; this.style.display='none'">
<img src="<?php echo $postThumb ?>" style="cursor:pointer" />
</div>
<div id="thevideo" style="display:none">
<?php echo $videoEmbed ?>
</div>
Can you try this,
echo '<div onclick="thevid=document.getElementById(\'thevideo\'); thevid.style.display=\'block\'; this.style.display=\'none\'">
<img src="'.$postThumb.'" style="cursor:pointer" />
</div>
<div id="thevideo" style="display:none">"'.$videoEmbed.'"</div>';
You have to add <?php ?> around any php commands you are typing. try <?php echo "whatever you want here"?>

Output image src with SimpleXML

I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.
Can anyone help me outpout this image:
Here's my XML and code:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->location; ?></p>
</a>
</li>
<?php } ?>
Your issue is here:
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing
Your if statement is incorrect. It should be:
if ($event->Media['url'] != null)

PHP unexpected elseif

I'm filtering some content on my website via country specific code, I'm trying to add else statements so it doesn't have to run each piece as individual code except whatever I try gives an error:
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
The above gives the following error: Parse error: syntax error, unexpected T_ELSEIF in and refers to the first elseif
As you're combining PHP with direct HTML / OUTPUT. During your code you're printing whitespace in between the ending bracket and the elseif keyword.
PHP's Interpreter looks directly after the } for the elseif keyword but what it finds is a block of outputted data, so it raises an error.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } ?>
<!--PHP Finds space here which it does not expect.-->
<?php elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
What you need to do is to remove the whitespace like so.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "ca"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php}}?>
You will notice the lack of space OUTSIDE the PHP blocks.
This should resolve your problem.
Else cannot evaluate a condition, it will be executed if all other conditions are false, think of it in terms of the default statment of a switch.
This:
<?php else(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
Needs to be this:
<?php else if(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }} ?>
Or even better to use switch-case for many conditions.
For example:
switch($x) {
case 1:
?> some html <?php
break;
case 2:
?> more html <?php
break;
}
Instead of closing the tags you can echo/print the html.
This will work:
<?php if (function_exists('isCountryInFilter')) {
if(isCountryInFilter(array("us", "ca"))) {
?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("au"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php } elseif(isCountryInFilter(array("nz"))) { ?>
<a rel="nofollow" class='preloading gallery_image' href="#" target="_blank">
<?php }
}
?>
instead of that format use
<?php if($foo == 1): ?>
html code
<?php else if($foo == 2): ?>
other html
<?php else: ?>
more html code
<?php end; ?>
FrEaKmAn's answer will probably work, but I prefer the curly braces myself. Try changing
<?php } ?>
<?php elseif(isCountryInFilter(array("au"))) { ?>
to
<?php } elseif(isCountryInFilter(array("au"))) { ?>
Also, in general, it's good to minimize the number of times you break in and out of php blocks...
(Once this piece is fixed, you'll need to deal with the problem pointed out by DamienL.)

Categories