?
Solved

PHP combine

Posted on 2011-09-23
3
Medium Priority
?
276 Views
Last Modified: 2012-05-12
How to combine 2 strings ito one?

Like
$var1 = 'Me';
$var2 = ' and';
$var3 = ' you';

so $var4 will have 'Me and you';

Ocrana
0
Comment
Question by:Ocrana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 39

Accepted Solution

by:
Aaron Tomosky earned 2000 total points
ID: 36591126
.
$var4=$var1.$var2.$var3
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36592523
Given the leading spaces you showed in $var2 and $var3 the solution from aarontomosky is correct.  You can learn more about how PHP handles strings here.
http://us2.php.net/manual/en/language.operators.string.php
http://us2.php.net/manual/en/language.types.string.php
0
 
LVL 4

Expert Comment

by:MattJellings
ID: 36592654
Just to elaborate on aarontomosky's answer (please don't accept mine, as their answer is exactly what you requested).

However, say for example you didn't have the leading spaces in the variable contents you could do...

$var4 = $var1 . ' ' . $var2 . ' ' . $var3;

Open in new window


the period . is used to concatenate (join) strings together.

Also, presuming there are no leading spaces in the variable contents you could do the following:

$var4 = "$var1 $var2 $var3";

Open in new window


If you aren't aware the difference between using single quotes and double quotes is as follows:

- Single quotes are treated as text and the contents are not parsed/processed by PHP.
- Double quotes are parsed/processed by PHP and variables within them are treated correctly.

For example...

$var1 = 'test';

echo 'This is a $var1';

// This would echo the string, This is a $var1

echo "This is a $var1";

// This would echo the string, This is a test

Open in new window


I hope that makes sense and helps you out a bit, however as mentioned please don't accept this as the answer as the other guys have already correctly answered it.
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question