Very critical Error in PHP. Please help

I am doing this -

I have an array by the name - $products, and have some values in it.


$contents = "Hello This is test - " . foreach($products as $temp_p) { echo $temp_p; } . " - OK.";
echo $contents;

I don’t know what’s wrong going in this, (I am getting parse error in the above like, where I have defined the foreach loop). So what else method is left for trying, and doing this hence?

I want to get the values within one variable, in above code, as we are getting in below example,


$contents = "Hello This is test - " . strtoupper("hello") . " - With simple function inside.";
echo $contents;

Please help someone!


$contents = "Hello This is test - ";
foreach($products as $temp_p) {
   $contents .= $temp_p;
}
$contents .= " - With the for each loop inside.";

:slight_smile:

Another solution:


$contents = "Hello This is test - " . implode("",$products) . " - Using implode to concatenate the array contents.";
echo $contents;  

Thanks a lot transio and ScallioXTX.
I am really grateful to you. It worked now.

Oops! Can I ask one more thing in the same question, if you won’t feel bad?

When I am doing


$contents = “Hello This is test - " . implode(‘’, get_name($products))
. " - Using implode to concatenate the array contents.”;
echo $contents;

get_name is my own function, i have created.

PHP is saying Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\ esting.php on line 13

Is there any way to apply a function to work with the individual values in the array and then use the implode function, or some vice-versa?

Thanks.

return an array from the function and it will implode fine… or implode the values inside the function and return a string to concatenate…

By the way, you’re missing a quote on your first param of the implode() function… it should be implode(“”, $array) <– the first param is your delimiter, in this case an empty string.

Thanks again. I got it working.