homepage Welcome to WebmasterWorld Guest from 23.22.173.58
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Appending multiple strings with same text
GoNC



 
Msg#: 4651895 posted 2:46 am on Mar 7, 2014 (gmt 0)

This should be a day 1 question, and I'm almost embarrassed to ask! LOL

Let's say I have:

$a = "apple";
$b = "banana";

Now, I want to say:

$a .= " is a fruit"; // apple is a fruit
$b .= " is a fruit"; // banana is a fruit

Is there a way to combine this in to one statement, so that I don't have to put " is a fruit" twice? Without creating a third variable, I mean.

I know that this doesn't work, but to give you an idea of what I mean, something like:

$a .= $b .= " is a fruit";

(Note for other readers, this doesn't work because it appends $b to $a first, making $a = "applebanana is a fruit")

 

jinxed

5+ Year Member



 
Msg#: 4651895 posted 8:27 am on Mar 7, 2014 (gmt 0)

Would it really make much of a difference to add a 3rd variable?

If so, why not just put all your 'fruits' into an array, and loop through them appending the extra text?

GoNC



 
Msg#: 4651895 posted 9:02 am on Mar 7, 2014 (gmt 0)

It wouldn't make much of a difference at all to add a 3rd variable, really. In fact, that's what I usually do. This is just something I find myself doing on occasion, though, and I thought that there might be a "cleaner" way of doing it that I didn't know.

So I guess it's just for my own education, really.

jinxed

5+ Year Member



 
Msg#: 4651895 posted 1:22 pm on Mar 7, 2014 (gmt 0)

I think a solution will normally be dictated by the context in which you are coding. If you had hundreds of fruits, for example, you would really be better off with a loop!

4serendipity

10+ Year Member



 
Msg#: 4651895 posted 8:42 pm on Mar 8, 2014 (gmt 0)

If you had hundreds of fruits, for example, you would really be better off with a loop!


Precisely what I was thinking

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved