Robbert Robbert -3 years ago 132
PHP Question

PHP script stops before final echo

I am trainee, learning PHP. As a small project I wrote some code.

I am trying to make a (test) website about cars. I want to show that each car has several options. I have a database in MySQL.

I wrote (this is just a part of the code):

foreach ($key['options'] as $options) {
$contents = str_replace("[OPTION]",$options['text'], $contents);

$result= '';

foreach ($key['motor'] as $motor) {
$nm = $motor['name'];
$cnt = $motor['count'];
$result .= $cnt ." " .$nm. " ";

return $result;

$paginas .= $contents;

echo $paginas;

So far the code. The thing is, after the result code, the script stops (which is normal).

But I don't want that the script stops, the only thing I want is that the final echo also echo's all my
return $result options
PLUS the echo of '$paginas'.

It is a bit hard to explain maybe, but I hope you guys understand.

Answer Source

Have you read the documentation of the return statement?

return returns program control to the calling module. Execution resumes at the expression following the called module's invocation.

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file.

No matter where it is used, it transfers the control to a different part of the code. The statement(s) following the return statement in the current context are not executed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download