Solved

Concatenating variables in $msg for php email

Posted on 2009-05-18
11
340 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
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 19

Expert Comment

by:Michael701
Comment Utility
like this?

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

Author Comment

by:max7
Comment Utility
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
Comment Utility
I'm not sure what you mean, like this?

$msg = "First Order: $tops_1 Name: $name";   ect..
0
 
LVL 1

Author Comment

by:max7
Comment Utility
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
Comment Utility
I understand the term concatenated, just not what you want.

like this?

$msg = $tops_1.$name;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:max7
Comment Utility
>>>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
Comment Utility
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
Comment Utility
remove the \n to display the msg in one line
0
 
LVL 1

Author Comment

by:max7
Comment Utility
>>>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
Comment Utility
yup, it should display one order per line :)
0
 
LVL 1

Author Closing Comment

by:max7
Comment Utility
You're right: it worked.  Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now