Solved

Concatenating variables in $msg for php email

Posted on 2009-05-18
11
344 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 500 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

Independent Software Vendors: 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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