Professional Web Applications Themes

string concatenation from array - PHP Development

I'm having a bit of difficulty getting a string to attach to itself from an array. Here is the bit of code I'm working on. $wresult = ""; foreach ($search_string as $word_result){ $wresult = $wresult & " " & $word_result; } echo ("$wresult"); Anyone see why when I run through each part of the array, it won't attach the next string and so on? thanks...

  1. #1

    Default string concatenation from array

    I'm having a bit of difficulty getting a string to attach to itself from an
    array. Here is the bit of code I'm working on.

    $wresult = "";

    foreach ($search_string as $word_result){
    $wresult = $wresult & " " & $word_result;
    }

    echo ("$wresult");

    Anyone see why when I run through each part of the array, it won't attach
    the next string and so on?

    thanks


    Micah Montoy Guest

  2. #2

    Default RE: [PHP] string concatenation from array

    Use the "." concatenation operator. : )


    $wresult = "";

    foreach ($search_string as $word_result) {
    $wresult = $wresult . " " . $word_result;
    }
    echo $wresult;


    Matt
    > -----Original Message-----
    > From: Micah Montoy [mailto:montmicaisu.edu]
    > Sent: Tuesday, August 12, 2003 3:58 PM
    > To: [email]php-generallists.php.net[/email]
    > Subject: [PHP] string concatenation from array
    >
    > I'm having a bit of difficulty getting a string to attach to itself
    from
    > an
    > array. Here is the bit of code I'm working on.
    >
    > $wresult = "";
    >
    > foreach ($search_string as $word_result){
    > $wresult = $wresult & " " & $word_result;
    > }
    >
    > echo ("$wresult");
    >
    > Anyone see why when I run through each part of the array, it won't
    attach
    > the next string and so on?
    >
    > thanks
    >
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >
    > ---
    > Incoming mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.507 / Virus Database: 304 - Release Date: 8/4/2003
    >
    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.507 / Virus Database: 304 - Release Date: 8/4/2003


    Matt Giddings Guest

  3. #3

    Default Re: [PHP] string concatenation from array

    On Tue, Aug 12, 2003 at 01:58:21PM -0600, Micah Montoy wrote:
    > I'm having a bit of difficulty getting a string to attach to itself from an
    > array. Here is the bit of code I'm working on.
    >

    A bunch of simplifications, efficiency tweaks on this approach...
    > $wresult = "";
    $wresult = '';

    >
    > foreach ($search_string as $word_result){
    > $wresult = $wresult & " " & $word_result;
    $wresult = $wresult . ' ' . $word_result;
    OR you can do...
    $wresult .= $word_result;
    > }
    >
    > echo ("$wresult");
    echo $wresult;


    BUT, the far far far better way to do this just takes one line:

    echo implode(' ', $search_string);

    --Dan

    --
    FREE scripts that make web and database programming easier
    [url]http://www.ysisandsolutions.com/software/[/url]
    T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
    4015 7th Ave #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409
    Analysis & Solutions Guest

  4. #4

    Default Re: [PHP] string concatenation from array

    Micah,

    The & sign in PHP (to the best of my knowledge) does not concatenate.

    Use a . instead. See below.

    $wresult = "";

    foreach ($search_string as $word_result){
    $wresult .= $word_result;
    }

    That should work for you.



    Jonathan Pitcher


    On Tuesday, August 12, 2003, at 02:58 PM, Micah Montoy wrote:
    > I'm having a bit of difficulty getting a string to attach to itself
    > from an
    > array. Here is the bit of code I'm working on.
    >
    > $wresult = "";
    >
    > foreach ($search_string as $word_result){
    > $wresult = $wresult & " " & $word_result;
    > }
    >
    > echo ("$wresult");
    >
    > Anyone see why when I run through each part of the array, it won't
    > attach
    > the next string and so on?
    >
    > thanks
    >
    >
    >
    > --
    > PHP General Mailing List ([url]http://www.php.net/[/url])
    > To unsubscribe, visit: [url]http://www.php.net/unsub.php[/url]
    >
    Jonathan Pitcher Guest

  5. #5

    Default Re: [PHP] string concatenation from array

    My personal favourite is to use an array. It's much cleaner syntax IMO...it
    is very easy to miss a '.' on a '.=', and introduce a nasty bug.

    $wresult = array();

    foreach ($search_string as $word_result) {
    $wresult[] = $word_result;
    }
    echo join('', $wresult);

    or in php 4.3

    echo join($wresult);

    --
    Peter James
    Editor-in-Chief, php|architect Magazine
    [email]petejphparch.com[/email]

    php|architect
    The Magazine for PHP Professionals
    [url]http://www.phparch.com[/url]


    ----- Original Message -----
    From: "Matt Giddings" <cen10761centurytel.net>
    To: "'Micah Montoy'" <montmicaisu.edu>; <php-generallists.php.net>
    Sent: Tuesday, August 12, 2003 2:00 PM
    Subject: RE: [PHP] string concatenation from array

    > Use the "." concatenation operator. : )
    >
    >
    > $wresult = "";
    >
    > foreach ($search_string as $word_result) {
    > $wresult = $wresult . " " . $word_result;
    > }
    > echo $wresult;
    >
    >
    > Matt
    >
    > > -----Original Message-----
    > > From: Micah Montoy [mailto:montmicaisu.edu]
    > > Sent: Tuesday, August 12, 2003 3:58 PM
    > > To: php-generallists.php.net
    > > Subject: [PHP] string concatenation from array
    > >
    > > I'm having a bit of difficulty getting a string to attach to itself
    > from
    > > an
    > > array. Here is the bit of code I'm working on.
    > >
    > > $wresult = "";
    > >
    > > foreach ($search_string as $word_result){
    > > $wresult = $wresult & " " & $word_result;
    > > }
    > >
    > > echo ("$wresult");
    > >
    > > Anyone see why when I run through each part of the array, it won't
    > attach
    > > the next string and so on?
    > >
    > > thanks
    > >
    > >
    > >
    > > --
    > > PHP General Mailing List (http://www.php.net/)
    > > To unsubscribe, visit: http://www.php.net/unsub.php
    > >
    > >
    > > ---
    > > Incoming mail is certified Virus Free.
    > > Checked by AVG anti-virus system (http://www.grisoft.com).
    > > Version: 6.0.507 / Virus Database: 304 - Release Date: 8/4/2003
    > >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.507 / Virus Database: 304 - Release Date: 8/4/2003
    >
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    Peter James Guest

  6. #6

    Default Re: [PHP] string concatenation from array

    From: "Jonathan Pitcher" <jpitchercfedc.com>
    > The & sign in PHP (to the best of my knowledge) does not concatenate.
    Correct, it's a bitwise AND operator.

    [url]http://us2.php.net/manual/en/language.operators.bitwise.php[/url]

    ---John Holmes...
    Cpt John W. Holmes Guest

Similar Threads

  1. string concatenation
    By Rinoa in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 22nd, 01:05 PM
  2. String concatenation qn
    By Wolf Blaum in forum PERL Beginners
    Replies: 4
    Last Post: January 26th, 12:21 PM
  3. String Concatenation Bug
    By ecow in forum PHP Development
    Replies: 1
    Last Post: August 18th, 02:14 AM
  4. [PHP] string concatenation from array
    By Chris W. Parker in forum PHP Development
    Replies: 0
    Last Post: August 12th, 08:04 PM
  5. String concatenation with .= <FH>
    By Jay Tilton in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 13th, 01:53 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139