Re: Adding strings together....
| From: | Steve Edberg | Date: | Thu, 03 Aug 2000 17:21:13 +0000 |
| Subject: | Re: Adding strings together.... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-9989@lists.php.net to get a copy of this message | ||
At 5:01 PM +0100 8/3/00, Richard Edens wrote:
Hi, With Richard Edens... I've read the tutorial about PHP but i can't find annything for the following simple problem... Adding strings together... In javascript for instance this might look like this: ---- var text_alpha = "hallo "; var text_berta = "world..."; text_alpha = text_alpha + textberta; ---- Witch means that 'text_aplha' became 'hello world...' This even worked with an array: ---- var text_alpha = "hallo "; var text_berta = new Array(); text_berta[0] = "world..." text_alpha = text_alpha + textberta[0]; ----See http://www.php.net/manual/language.operators.string.php
Now with 'emplode' you can split a string into an Array like withI assume you mean 'explode'.
javascript. but how can i add every array element with an enter key like '\n' to a string??? Witch in javascript this would look like: ---- text_alpha = text_alpha + textberta[0] + '\n'; ----This is a little less obvious from the manual. However, in PHP, the double quote not only does variable substitution, it also interprets control characters. For example, a PHP equivalent to the above line would be $text_alpha = $text_alpha . $textberta[0] . "\n"; or $text_alpha .= $textberta[0] . "\n"; You would then get the newline at the end of your string. If you used '\n' (in SINGLE quotes), you would simply have the two characters '\' and 'n' at the end of the string. You could also do something like $tb = $textberta[0]; $text_alpha .= "$tb\n"; Note that using "$textberta[0]\n" here MAY work in PHP3, even though the expression is ambiguous: do you want to use the value of $texberta, followed by the characters '[0]', or the value of $textberta[0]? PHP3 (I think) will assume you want the array element. I believe PHP4 is more restrictive when it comes to embedding subscripted array variables in strings, and it in general isn't a good idea anyway - it can lead to unexpected results. -steve +--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+