最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - adding or removing endforeach; throws error!

matteradmin8PV0评论

This is my code, where i keep getting either error: unexpected endforeach; if endforeach is used or unexpected end of file if i don't use endforeach; It's driving me crazy!!

<ul id="portfolio-filter">
    <?php
    $k=0;
    $services = array('all', 'marketing', 'SEO', 'web-design', 'web-development',                           'wordpress'); 
    foreach($services as $key) : ?>                     
    <li class="<?php if($k==0) { ?> active <?php { ?>"><a class="<?php echo $key; ?>" href="#"><?php echo $key; ?></a>/</li>
    <?php endforeach; ?> 
</ul>

Any help will be appreciated!

This is my code, where i keep getting either error: unexpected endforeach; if endforeach is used or unexpected end of file if i don't use endforeach; It's driving me crazy!!

<ul id="portfolio-filter">
    <?php
    $k=0;
    $services = array('all', 'marketing', 'SEO', 'web-design', 'web-development',                           'wordpress'); 
    foreach($services as $key) : ?>                     
    <li class="<?php if($k==0) { ?> active <?php { ?>"><a class="<?php echo $key; ?>" href="#"><?php echo $key; ?></a>/</li>
    <?php endforeach; ?> 
</ul>

Any help will be appreciated!

Share Improve this question edited Mar 18, 2019 at 21:44 WebElaine 9,8491 gold badge17 silver badges28 bronze badges asked Mar 18, 2019 at 21:41 J. A.J. A. 12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

If you pasted your code directly,

The second curly bracket here <?php if($k==0) { ?> active <?php { ?> should be a closing one, like this:

<?php if($k==0) { ?> active <?php } ?>

There is also a stray slash </a>/</li> - should be - </a></li> - but that shouldn't cause any PHP errors.

Here is your full code

<ul id="portfolio-filter">
<?php
$k=0;
$services = array('all', 'marketing', 'SEO', 'web-design', 'web-development', 'wordpress'); 
foreach($services as $key) : ?>                     
<li class="<?php if($k==0) { echo "active"; } ?>"><a class="<?php echo $key; ?>" href="#"><?php echo $key; ?></a>/</li>
<?php endforeach; ?> 

Post a comment

comment list (0)

  1. No comments so far