Luke Miles
Luke Miles

Reputation: 15

Unexpected bracket - PHP

Here is some PHP mixed with HTML, I apologize that it's quite messy.

  <div class="media"><?php echo ($inf['post_url']) ?><img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" /></div>
  {block:Caption}<?php if (array_key_exists('caption', $inf))?><div class="copy"><?php Echo ($inf['caption']);?></div><?php }; ?>
  <?php }; ?>

The second line produces this error

Parse error: syntax error, unexpected '}' on line 1708

I do not see anything wrong with the brackets. What is producing this error?

Upvotes: 0

Views: 2859

Answers (4)

Kevin Nielsen
Kevin Nielsen

Reputation: 4433

If you boil off all the HTML, lines 2 and 3 come out to this:

if (array_key_exists('caption', $inf)) Echo ($inf['caption']); };  }; 

You don't need either of those closing right curly braces.

Upvotes: 0

Andrew
Andrew

Reputation: 2154

There was an extra bracket at the end as well as some superfluous semicolons, and "echo" was capitalized. Give this a shot:

<div class="media">
   <?php echo ($inf['post_url']); ?>
   <img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
</div>
   {block:Caption}
   <?php if (array_key_exists('caption', $inf)) {?>
     <div class="copy">
     <?php echo ($inf['caption']);?>
     </div>
   <?php } ?>

Upvotes: 0

Ezequiel Muns
Ezequiel Muns

Reputation: 7742

It greatly helps if you indent your code correctly.

    <div class="media">
        <?php echo ($inf['post_url']) ?>
        <img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
    </div>
    {block:Caption}
    <?php if (array_key_exists('caption', $inf)) ?>
        <div class="copy">
        <?php Echo ($inf['caption']);?>
        </div>
    <?php }; ?>
<?php }; ?>

Very basic, the closing brackets do not have corresponding opening brackets.

Some more stylistic advice: echo is a PHP construct, so you don't need to call it like a function, that is echo something is equivalent to echo(something) but the former is preferred. Also, it pays to be consistent with the capitalisation of reserved words, i.e. if you're using lowercase, always use lowercase for reserved words.

Upvotes: 1

deceze
deceze

Reputation: 522076

If you'd format your code properly so it'd actually be readable, the superfluous bracket (and missing opening bracket) would be easy to spot:

    <div class="media">
        <?php echo ($inf['post_url']) ?>
        <img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
    </div>
    {block:Caption}
    <?php if (array_key_exists('caption', $inf))?>
        <div class="copy">
            <?php Echo ($inf['caption']);?>
        </div>
    <?php }; ?>
<?php }; ?>

Upvotes: 1

Related Questions