我的PHP脚本中的PHP解析错误

我的PHP脚本中的PHP解析错误,php,Php,我已经花了几个小时研究了这段代码,但找不到缺少的逗号、括号或引号等。我是一名PHP初学者,但我正在尝试——找到它的任何帮助都将是惊人的。我得到的错误是: PHP分析错误:语法错误,文件中意外出现$end。。。在线235 这是我的密码: <?php $page = get_page_by_title( 'CSA' ); $page1 = get_page_by_title( 'Co-op Members' ); $page2 = get_page_by_title(

我已经花了几个小时研究了这段代码,但找不到缺少的逗号、括号或引号等。我是一名PHP初学者,但我正在尝试——找到它的任何帮助都将是惊人的。我得到的错误是:

PHP分析错误:语法错误,文件中意外出现$end。。。在线235

这是我的密码:

<?php
    $page = get_page_by_title( 'CSA' );
    $page1 = get_page_by_title( 'Co-op Members' );
    $page2 = get_page_by_title( 'Recipes' );
    $page3 = get_page_by_title( 'FAQs' );
    $page4 = get_page_by_title( 'Contact Us' );
    $page5 = get_page_by_title( 'Home' );

    if ( is_page($page->ID) )
    {?>
        <div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top5.jpg') no-repeat scroll center bottom #244714 !important;">
    <?php
    }elseif ( is_page($page1->ID) )
    {?>
        <div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top2.jpg') no-repeat scroll center bottom #244714 !important;">
    <?php
    }elseif ( is_page($page2->ID) )
    {?>
        <div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top3.jpg') no-repeat scroll center bottom #244714 !important;">
    <?php   
    }elseif ( is_page($page3->ID) )
    {?>
        <div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top4.jpg') no-repeat scroll center bottom #244714 !important;">
    <?php
    }elseif ( is_page($page4->ID) )
    {?>
        <div id="page-bg" style="background: url('<?php bloginfo('template_directory'); ?>/images/green/page-top5.jpg') no-repeat scroll center bottom #244714 !important;">
    <?php
    }elseif ( is_page($page5->ID) )
    {?>
        <div id="page-bg" style="background: url('http://ycgrown.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
    }else{
?>
        <div id="page-bg" style="background: url('http://ycgrown.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">


您缺少PHP标记:

{?>
            <div id="page-bg" style="background: url('http://xyz.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
<?php
        }else{
?>
            <div id="page-bg" style="background: url('http://xyz.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
{?>

您还缺少else条件的右大括号

 }else{
?>
        <div id="page-bg" style="background: url('http://ycgrown.com/wp-content/uploads/2013/03/Cherry-Tomatoes.jpg') no-repeat scroll center bottom #244714 !important;">
 <?php
 }
 ?>
}其他{
?>

您可以做一些事情来整理这些代码。理想情况下,开始时的代码将放在控制器中,然后您的呈现将主要是带有一些PHP代码片段的HTML

<?php
// This would be best in a controller class, but for now we'll enclose
// it in its own little section
$page = get_page_by_title( 'CSA' );
$page1 = get_page_by_title( 'Co-op Members' );
$page2 = get_page_by_title( 'Recipes' );
$page3 = get_page_by_title( 'FAQs' );
$page4 = get_page_by_title( 'Contact Us' );
$page5 = get_page_by_title( 'Home' );
?>

<!-- This is your 'view layer', so keep your PHP small and concise -->
<?php if ( is_page($page->ID) ): ?>
    <div
        id="page-bg"
        class="bgnd"
        style="background-image: url('<?php bloginfo('template_directory') ?>/images/green/page-top5.jpg');"
    >
<?php elseif ( is_page($page1->ID) ): ?>
    <div
        id="page-bg"
        class="bgnd"
        style="background-image: url('<?php bloginfo('template_directory') ?>/images/green/page-top2.jpg');"
    >
<?php elseif ... ?>
这将使事情变得更加干燥(不要重复你自己),从而节省你的时间和精力


最后,我缩进了
div
s,使它们的属性每行一个。这是一个品味问题,但在我看来,它更可读,并且仍然完全像HTML一样有效。它还有一个好处,那就是您需要在IDE中少做/不做水平滚动,这可以使调试时的生活更轻松。

另外,在o对于巨人,如果。在最后添加
@Brett,我在下面给了你很多建议。有用吗?
.bgnd { background: no-repeat scroll center bottom #244714 !important; }