Solved

Concatenating variables in $msg for php email

Posted on 2009-05-18
11
343 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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 …

830 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