0

I made small search for this error. there are a lot of topics but looks like none of them apply to me. As I see it this code is perfectly fine. Why do I have an error?

<ul class="uk-grid uk-text-center" data-uk-grid-margin>
    <?php foreach ($cats as $cat): ?>
        <li class="uk-width-medium-1-5">
            <a href="<?php JRoute::_($cat->link)?>">
                <?php if ($cat->level == 1) :?>
                    <img src="<?php $cat->image;?>" alt="<?php $cat->description; ?>" width="175" height="80">
                <?php else: ?>
                    <img src="<?php $cat->image;?>" alt="<?php $cat->description; ?>" width="135" height="135">
                <? endif;?> 
                <h2 class="uk-article-title">
                    <?php $cat->title; ?>
                </h2>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

Here is my php version

/var/www/localhost # php -v
PHP 7.4.15 (cli) (built: Feb  8 2021 20:31:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

But I do not think it is a problem because i started from 7.2 and then 7.3 and all gave same result.

Ссылка с ошибкой http://flareapp.io/share/4m4oEOJ5

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38

1 Answers1

1

this works well, you forget php after <? in this line: <?php endif;?>

So <? endif;?> is wrong and <?php endif;?> will work well.

<ul class="uk-grid uk-text-center" data-uk-grid-margin>
    <?php foreach ($cats as $cat): ?>
        <li class="uk-width-medium-1-5">
            <a href="<?php JRoute::_($cat->link)?>">
                <?php if ($cat->level == 1) :?>
                    <img src="<?php $cat->image;?>" alt="<?php $cat->description; ?>" width="175" height="80">
                <?php else: ?>
                    <img src="<?php $cat->image;?>" alt="<?php $cat->description; ?>" width="135" height="135">
                <?php endif;?> 
                <h2 class="uk-article-title">
                    <?php $cat->title; ?>
                </h2>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

or if you want to write without php word in <?php you should do it for all the lines related to if statement so your code will be like this: but I recommand to use above code instead of bellow code:

<ul class="uk-grid uk-text-center" data-uk-grid-margin>
    <?php foreach ($cats as $cat): ?>
        <li class="uk-width-medium-1-5">
            <a href="<?php JRoute::_($cat->link)?>">
                <? if ($cat->level == 1) :?>
                    <img src="<?php $cat->image;?>" alt="<?php $cat->description; ?>" width="175" height="80">
                <? else: ?>
                    <img src="<?php $cat->image;?>" alt="<?php $cat->description; ?>" width="135" height="135">
                <? endif;?> 
                <h2 class="uk-article-title">
                    <?php $cat->title; ?>
                </h2>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

so if we use <? for if() statement, we can't use <?php else: for else statement. we should use <? else: ?> like the if() statment

Raskul
  • 1,674
  • 10
  • 26
  • 1
    Thank you. This is not my code; I always use ` – Sergey Romanov Feb 28 '21 at 09:02
  • You're always welcome, I'm happy to solve the problem. if you use `php intelephense` extension in `vscode`, you can see the problem, before you debug it. – Raskul Feb 28 '21 at 09:06
  • I do use this extension, but it only highlighted `` with that syntax error. That is because inteliphence uses php executable to Lint files and that php gave endforeach error. – Sergey Romanov Feb 28 '21 at 10:36