Solved

PHP combine

Posted on 2011-09-23
3
270 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
3 Comments
 
LVL 38

Accepted Solution

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now