Either an echo is missing or PHP wants replacing with =
This was the case +
{$array["$item"]['price']}
{$array["$item"]['title']}
I also taxed {}
Like this →
$array["$item"]['price']
$array["$item"]['title']
Still, I am facing issues. Features are not getting published
.even if I echo then they are publishing just $features.
You still have a syntax error there, the one that @John_Betong mentioned earlier.
Show us your code. If you mean that your code is
echo $features;
and all it does is echo the contents of that variable, then please explain why that’s incorrect, and what it should be doing instead.
PS - much easier to copy/paste the code and put a comment on the end to show where the error is than to create all these images with arrows on, surely?
whole code →
<?php
foreach($products as $key => $product) :
echo card($products, $key);
endforeach;
//=======================
function card( array $array = [], string $item = ''):string {
$features = '';
foreach($array["$item"]['features'] as $key => $feature) :
$features .= '<li class=""> $feature </li>';
endforeach;
ob_start();
?>
<div class="cardbody">
<h1 class="text-centre"><span>$</span><?= $array["$item"]['price'] ?></h1>
<h3 class="text-centre"><?= $array["$item"]['title'] ?></h3>
<ul class="list-group text-centre">
<?= $features ?>
</ul>
<div class="footer text-centre">
<!-- Stripe Button wil go here -->
</div>
</div><!-- cardbody -->
<?php
$result = ob_get_clean();
return $result;
}
?>
Output →
The echo or = is missing and where is declare strict, error reporting, etc?
Take a look at post #75 and notice how the for next loop is not the same.
Think about the differences between single and double quotes.
Got it:
$features .= '<li class="">' . $feature . '</li>';
That’s one way. The only reason for the complexity is because you want those quotes around the blank class-name, so couldn’t you leave that out altogether? I’m very rusty on HTML / CSS, but I can’t see what it’s doing. Then you have
$features .= "<li>$feature</li>";
Yes, true. Thanks for pointing.
[off-topic]
I have adopted the following coding style:
- PHP variables are never inside double quoted strings
- PHP variables are always concatenated using periods although I should use commas
- strings are always enclosed in single quotes
- href, class, title, alt, etc always use double quotes
- use PHP HereDoc for complicated strings
Just my style but I recommend adopting your own style and sticking to it.
[/off-topic]
I agree, in particular about not just sticking variable names inside double-quoted strings. I’m not actually sure, on that basis, why I posted my last post, really it was just to point out a further simplification.
For sure your intention was to help. Thanks.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.