Jump to content


Photo

Making variable not echo...


  • Please log in to reply
3 replies to this topic

#1 taylhis

taylhis
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 August 2006 - 05:54 PM

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.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 05:57 PM

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:
$foo = 'hello';

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

$foo .= " world";

//$foo now holds "hello world";


#3 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 31 August 2006 - 06:30 PM

<?php

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

?>

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

<?php

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

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

?>


#4 taylhis

taylhis
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 August 2006 - 08:09 PM

You guys rock!  WORKS!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users