Jump to content

Archived

This topic is now archived and is closed to further replies.

taylhis

Making variable not echo...

Recommended Posts

This should be simple -- I am BRAND NEW to PHP...

I have this code...

foreach ($result as $value) {

echo trim($value)."+";


this currently works as it should and let's say the input is hello -- the web output is "hello+helloa+hellob+helloc" as it should be (using the other code in the program before this...  But..  I need to make it the variable so that a variable such as $car = "hello+helloa+hellob+helloc"...

When I simply replace echo trim($value)."+"; with $car = trim($value)."+"; it keeps REPLACING the variable each time and not adding it onto the end.  ANy help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Use $car .= trim($value) . "+"

.= adds whats on the right to the end of whats on the left. It'll produce waht you want.

For example:
[code=php:0]$foo = 'hello';

// We for got world so we add it in:

$foo .= " world";

//$foo now holds "hello world";[/code]

Share this post


Link to post
Share on other sites
[code]
<?php

$car .= trim($value) . "+"

?>
[/code]

is going to add an extra "[b]+[/b]" to the end of the string. Try this:

[code]
<?php

foreach( $result as $value ){
   // trim each array item
   $value = trim($value);
}

$car = implode('+', $result);

?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.