简体   繁体   中英

Syntax error for printing an array

I'm attempting to print an array of php elements embedded in html

If I input

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.$parent'.</small></p></div></li>';

I get a result that says something like "Chris James Parent of Array

but if I attempt to print the array with a foreach as so

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.
            foreach($parent as $p){
                echo $p.' ';
            }
            .'</small></p></div></li>';

The program crashes completely. I would assume that I'm doing something syntactically incorrect, but I can't spot the issue. Is there a simply way to print the elements in thearray that would avoid the crash?

Thanks in advance!

You concatenate output with . not additional PHP statements:

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of ';

            foreach($parent as $p){
                echo $p.' ';
            }

echo '</small></p></div></li>';

However you can just implode $parent :

echo '<strong>'.$s[firstname].' '.$s[lastname].'</strong><div class="moreinfo"><p><small>'.$s[role].' of '.implode(' ', $parent).'.</small></p></div></li>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM