• I’m not a coder, but trying my best at this. I cannot figure out how to fix my else statement. It won’t print the variable or a wordpress template tag. Does anyone know what I’m doing wrong?

    <?php $postrating = get_bloginfo(‘stylesheet_directory’); ?>

    <?php // if there’s a rating image
    if($rating !== ”) { ?>
    <img src=”<?php bloginfo(‘stylesheet_directory’); ?>/images/<?php echo $rating; ?>”>
    <?php }
    else { echo ‘<img src=”$postrating/images/rate.gif”>’; } ?>

    Thanks
    Don

Viewing 3 replies - 1 through 3 (of 3 total)
  • it’s not clear in your code where $rating is coming from – I am assuming it already exists earlier in your script.
    Try this

    <?php // if there's a rating image
    if ($rating !== '') { ?>
      <img src="<?php bloginfo('template_directory'); ?>/images/<?php echo $rating; ?>">
    <?php } else { ?>
      <img src="<?php bloginfo('template_directory'); ?>/images/rate.gif">
    <?php } ?>

    Try if($rating != '') {. The way you have it also checks the variable type and you probably don’t want that. What sets the $rating variable? I see $postrating but not $rating.

    Also, your final echo statement isn’t going to work. The single quotes mean it will literally print <img src="$postrating/images/rate.gif"> but you want that $postrating to be substituted for the variable value. What you need is echo '<img src="',$postrating,'/images/rate.gif">'; or echo "<img src='$postrating/images/rate.gif'>";.

    Friendly suggestion: If you are going to be doing much of this, get yourself a text editor built for code– one that will do syntax highlighting for you. It makes spotting this stuff much easier. If you use Windows, Crimson is pretty decent. So is PSPad. A Java based editor called jEdit is pretty good and it runs on Windows and Mac and should run on Linux too. TextWrangler is good on a Mac too. I do most of my work in Kate on a Linux box. And these are all free– at least last time I looked they were. What are you using to edit your code, by the way?

    Thread Starter keyquest

    (@keyquest)

    Wow, thanks for your help. I did apljdi’s meathod and it works!!

    Thanks for the tip too on the editor. I’ve been using notepad.

    Don

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Printing variable in else statment’ is closed to new replies.