Solved

PHP combine

Posted on 2011-09-23
3
274 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 500 total points
ID: 36591126
.
$var4=$var1.$var2.$var3
0
 
LVL 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

717 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