Jump to content


Photo

Syntax error


  • Please log in to reply
3 replies to this topic

#1 chris86

chris86
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 17 September 2006 - 05:10 AM

I'm getting a syntax error (on line 15) when I insert the following code into a custom.php file.


function random_scheme()
{
$schemes = glob(TEMPLATEPATH . '/skins/light-gold/schemes/*.css');

if ( $schemes )
{
$key = array_rand($schemes);

$scheme = basename($schemes[$key]);
?>
<link rel="stylesheet" type="text/css"
href="<?php echo get_template_directory_uri() . '/skins/light-gold/schemes/' . $scheme; ?>"
/>
<?php

//The bracket below is line 15.
}
} # end random_scheme()

add_action('wp_head', 'random_scheme', 50);


Here's the syntax error I'm getting...

Parse error: syntax error, unexpected '}' in /home/username/public_html/domain_name/blog/wp-content/themes/semiologic/skins/light-gold/custom.php on line 15

I was given this code by someone else who says nothing's wrong with the code.  But, according to the syntax error, it looks like there's a problem with brackets.  He's got 2 closing brackets in a row.  That can't be right..? How do I get rid of the syntax error? 

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 September 2006 - 05:16 AM

He's right. There are two loops, so there needs to be two loop closings.  If you indent the code to show the structure, it's easier to follow:

function random_scheme(){
    $schemes = glob(TEMPLATEPATH . '/skins/light-gold/schemes/*.css');
    if ( $schemes ){
        $key = array_rand($schemes);
        $scheme = basename($schemes[$key]);
?>
<link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri() . '/skins/light-gold/schemes/' . $scheme; ?>"/>
<?php
    }
} # end random_scheme()
add_action('wp_head', 'random_scheme', 50);

Legend has it that reading the manual never killed anyone.
My site

#3 chris86

chris86
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 17 September 2006 - 05:23 AM

Thanks Andy. Good to know his code is correct. Although, it still leaves me hanging.  If the code is technically correct, why am I getting a syntax error on line 15? 

How can this be?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 September 2006 - 05:42 AM

The php interpreter's error message explains what line it was at when it found a condition it could not handle (for the stated reason).  The origin of the error might well have been, and sometime is, many lines before the reported line.  That code in isolation runs without error.  What's happening before it?  Is it perhaps an included file and the source of the error is in the code that included it.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users