Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP combine

Posted on 2011-09-23
3
Medium Priority
?
284 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 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