0

I have an IF/ELSE statement and I would like to print out some images that I am getting from my Drupal site. I can't figure out how to print those IMG tags without getting errors.

This is what I have so far:

<?php
   $field = field_get_items('node', $node, 'field_visitor_image');
 if($field){

        <img src="<?php print image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']); ?>">

            }
        else        
        { 

   <img src="<?php print image_style_url('lead_teaser', $node->field_banner_image['und'][0]['uri']); ?>">

        }        
?>
5
  • 2
    Oh no, php tags within the strings strike again. Commented Apr 9, 2014 at 22:08
  • print ? real coders use echo Commented Apr 9, 2014 at 22:10
  • 1
    I just love the peanut gallery around here. I am learning. So sue me if I don't act like a "real coder". Commented Apr 9, 2014 at 22:11
  • 1
    What's the difference between what you're printing in the if and the else? They look the same to me. Commented Apr 9, 2014 at 22:12
  • sue me if levity is a sueable offence. Commented Apr 9, 2014 at 22:17

3 Answers 3

5

You have to break out of PHP mode when you start outputting HTML.

if($field){
?>
    <img src="<?php print image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']); ?>">
<?php
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use echo and string concatenation:

if ($field) {
    echo '<img src="' . image_style_url('lead_teaser', $node->field_visitor_image['und'][0]['uri']) . '">';
}

1 Comment

Thanks Barmar. That worked wonderfully. I appreciate you help!
0

You cannot nest

<?php > inside another  <?php >.

One option for you could be to concatenate using ".".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.