Jump to content

Archived

This topic is now archived and is closed to further replies.

chris86

Syntax error

Recommended Posts

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

[code]

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);
[/code]


[b]Here's the syntax error I'm getting...[/b]

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? 

Share this post


Link to post
Share on other sites
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:

[code]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);[/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.