Parse error: syntax error, unexpected end of file

2019-02-21 04:36发布

问题:

This is my php code of index.php of a wordpress theme:

<section id="content">
    <?php
        $i=1;
        while(have_posts() && i < 7):the_post();
        $tumbUrl = '';
        if(has_post_thumbnail())
        {
            $tumbUrl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        }
        if(i < 4):
    ?>
    <div id="row1">
        <div id="tile<?echo $i?>" class="tile">
            <div id="img<?echo $i?>" class="tileimage"<?if($tumbUrl != ''):?> style="background-image:<?echo $tumbUrl; ?>"<?endif;?>></div>
            <div id="text<?echo $i?>" class="tiletext"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
        </div>
    </div>
    <?
        else:
    ?>
    <div id="row2">
        <div id="tile<?echo $i?>" class="tile">
            <div id="img<?echo $i?>" class="tileimage"<?if($tumbUrl != ''):?> style="background-image:<?echo $tumbUrl; ?>"<?endif;?>></div>
            <div id="text<?echo $i?>" class="tiletext"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
        </div>
    </div>
    <?
        endif;
        endwhile;
    ?>
</section>

and when I want to run it I get the error saying that Parse error: syntax error, unexpected end of file in C:\wamp\www\wordpress\wp-content\themes\Odatis\index.php on line 31 but I cann't find any error.

Can anybody help me?

(my PHP version is 5.4.3)

回答1:

Its very simple. You use short open tags <?.

Enable the short open tags in your php.ini or use the complete php tags like <?php in newer PHP-Versions its disabled by default. But you shouldn't use the short syntax in your projects that can cause Problems if you share your code.

http://www.php.net/manual/en/ini.core.php#ini.short-open-tag



回答2:

Here is the modified working code...

<section id="content">
    <?php 
        $i=1;
        while(have_posts() && i < 7):the_post();
        $tumbUrl = '';
        if(has_post_thumbnail())
        {
            $tumbUrl = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
        }
        if(i < 4):
    ?>
    <div id="row1">
        <div id="tile<?php echo $i; ?>" class="tile">
            <div id="img<?php echo $i; ?>" class="tileimage"<?php if($tumbUrl != ''): ?> style="background-image:<?php echo $tumbUrl; ?>"<?php endif; ?>></div>
            <div id="text<?php echo $i; ?>" class="tiletext"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></div>
        </div>
    </div>
    <?php 
        else:
    ?>
    <div id="row2">
        <div id="tile<?php echo $i; ?>" class="tile">
            <div id="img<?php echo $i; ?>" class="tileimage"<?php if($tumbUrl != ''): ?> style="background-image:<?php echo $tumbUrl; ?>"<?php endif; ?>></div>
            <div id="text<?php echo $i; ?>" class="tiletext"><a href="<?php the_permalink(); ?>" rel="bookmark" title="a<?php the_title(); ?>"><?php the_title(); ?></a></div>
        </div>
    </div>
    <?php 
        endif;
        endwhile;
    ?>
</section>
Note: Make sure the ending mark(;) are there and also the space as required.


回答3:

Try to replace all <?echo ###?> with <?= ### ?>. Or you need to enable the short open tags in your php.ini if your PHP is < 5.4.

aww no: There is a semicolon missing in <?php the_permalink() ?>

One of these should fix it, otherwise I'm sorry :/