Solved

PHP combine

Posted on 2011-09-23
3
272 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 109

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

786 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