?
Solved

Concatenating variables in $msg for php email

Posted on 2009-05-18
11
Medium Priority
?
346 Views
Last Modified: 2012-06-27
Greetings,

I know how to basically set up the format for $msg such as:

$msg = "First Order: $tops_1\n" .
             "Name: $name\n" .
             
... etc.  What I need to know is how to concatenate variables on one line if need be.  

I would also like to know is how much freedom there is to format the $msg output in other ways and the formating/syntax to do that.  I know that  \n creates a line break and I think \t creates a tab space but I'm fuzzy on everything else.  I hope this is clear but if not, let me know what more information you need.      
0
Comment
Question by:max7
[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
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24416854
like this?

$msg = "First Order: $tops_1\n" .
             "Name: $first_name $last_name\n" .
0
 
LVL 1

Author Comment

by:max7
ID: 24416873
but doesn't that just break into a separate line?  

I want to create a continuous string of information with the variables i.e. concatenate them into one long sentence.  I know how to do this otherwise in PHP but not sure how to do it within the mail() using $msg.
0
 
LVL 5

Expert Comment

by:bored321
ID: 24416903
I'm not sure what you mean, like this?

$msg = "First Order: $tops_1 Name: $name";   ect..
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:max7
ID: 24417033
here's an example using the $_POST variable.  first_name and last_name are form field names from separate text inputs:

$name = $_POST['first_name']. ' ' .$_POST['last_name'];

By writing the POST variable in this manner, both first and last name are posted together i.e. they have been concatenated.  Does that make sense?
0
 
LVL 5

Expert Comment

by:bored321
ID: 24417116
I understand the term concatenated, just not what you want.

like this?

$msg = $tops_1.$name;
0
 
LVL 1

Author Comment

by:max7
ID: 24417455
>>>like this?

$msg = $tops_1.$name;

I'm not sure what "like this?" means ... are you asking me if this is the correct way to concatenate a $msg string?  I'm asking the question because I don't know.  Please clarify what you are asking.
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24417763
Well the . (dot) is used to concatenate strings in PHP.

That being said there are a number of ways to arrive at the same outcome.

$a = "Hello ";
$b = "World!";

$c = $a.$b;
echo $c;
// Hello World!

$d = $a.$b. " this is Michael."
echo $d;
// Hello World! this is Michael.";

//PHP will also evaluate variables when inside double quoted strings. Note: NOT single quoted strings.

$e = "Michael said $a $b";
echo $e;
// Michael said Hello World!

0
 
LVL 4

Accepted Solution

by:
dkaisla earned 2000 total points
ID: 24418010
remove the \n to display the msg in one line
0
 
LVL 1

Author Comment

by:max7
ID: 24418204
>>>remove the \n to display the msg in one line

Based on that advice, should the syntax for the strings below work?

$msg = "Order One - Top: $tops_1, Bottom: $bottoms_1, Color: $color_1, Size: $size_1, Quantity: $quantity_1\n" .
                "Order two - Top: $tops_2, Bottom: $bottoms_2, Color: $color_2, Size: $size_2, Quantity: $quantity_2\n" .

0
 
LVL 4

Expert Comment

by:dkaisla
ID: 24418232
yup, it should display one order per line :)
0
 
LVL 1

Author Closing Comment

by:max7
ID: 31582792
You're right: it worked.  Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

719 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