Forum Moderators: coopster
$emaildata = 'This email is automatically generated to inform you'. "\r\n"
. 'that a potential customer requested a quote on your website.' . "\r\n\r\n";
$emaildata = $emaildata . 'Date: ' .$date . "\r\n" . 'Client name: ' . $clientName . "\r\n";
$emaildata = $emaildata . 'Client email: ' . $clientEmail . "\r\n" . 'Text submitted:' . "\r\n";
$emaildata = $emaildata . $textArea . "\r\n";
$emaildata = $emaildata . 'URL submitted: ' . $url . "\r\n";
However, for some reason, when I receive the email, 2 carriage returns are missing:
...inform you'. "\r\n" . 'that a potential...
and
$clientEmail . "\r\n" . 'Text submitted:'
I tried to echo $emaildata and it shows just fine, but my email client persists on ignoring those 2 carriage returns. The others work just fine.
Advice anyone?
It's just another one of those beautiful MicroSoft features we have all come to love.
How do you hack mail() to get around Outlook's removal of line breaks [webmasterworld.com]?
fixed link
[edited by: coopster at 4:57 pm (utc) on April 8, 2006]
Try this instead:
$emaildata = 'This email is automatically generated to inform you \r\nthat a potential customer requested a quote on your website.\r\n\r\n';
$emaildata .= 'Date: ' .$date . '\r\nClient name: ' . $clientName . '\r\n';
$emaildata .= 'Client email: ' . $clientEmail . '\r\n'Text submitted: ' . time() . '\r\n';
$emaildata .= $textArea . '\r\n';
$emaildata .= 'URL submitted: ' . $url . '\r\n';
I did, however, read that multiple concenations(using ' . $var . ') is slow. Apparently, using double quotes is faster because vars are1 expanded and no concanations are needed.
Another thing I learned is that line breaks and such are preserved inside double quotes, which means that you wouldn't need to keep appending strings to a variable. You could do this:
$emaildata = "This email is automatically generated to inform you
that a potential customer requested a quote on your website
Date: $date
Client name: $clientName
Client email: $clientEmail
Text submitted: $textArea
URL submitted: $url
";
Thanks for making me learn something today :)
R. v. Concatenation of Strings, ex p. Pushing/Joining of Arrays [webmasterworld.com]
1. I ran his test on my machine several times and got these results:
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve
using concat: 0.0124199390411 seconds
using array: 0.0235970020294 seconds
difference: -0.0111770629883 seconds
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve
using concat: 0.000205039978027 seconds
using array: 0.000146865844727 seconds
difference: 5.81741333008E-005 seconds
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve,
Taylor, Zac, Isaac, Leo, Aaron, Nick, Scott, Dave, Bob, Clint, Chris, Thomas, Ryan, Dennis, Thimo, Steve
using concat: 0.000194072723389 seconds
using array: 0.000144958496094 seconds
difference: 4.91142272949E-005 seconds
As you can see, the results changed on each run, and most markedly between the first and the second run. All runs after the first returned a shorter time for the array, so it would seem he was right.
*BUT*
2. The code starts both processes from an array.
I tried concatening straight ($s= 'Taylor, '.'Zac, '. etc.) and this time the results are in favor of concatenation with a consistant E-005 times.
Conclusion from these experiments:
1. Results change quite a bit so the fastest method may change based on set-up and possibly system cache. (tried with Firefox, Mozilla and IE). I suspect PHP version, server, and a bunch of other variables may interfere with the test results. (In other words, this experiment has too many variables to allow a finite conclusion.
2. If the data are already in an array, pushing the array tends to be faster than extracting the array and concatenating.
3. If the data is not in an array, it's appears to be consistently faster, on my machine, to concat directly.
Here is the code revisited:
<?php
/*
Comparing speed of concatenating strings
and pushing and joining arrays
Original Author: andreasfriedrich
*/
#
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
#
$x = array('Taylor','Zac','Isaac','Leo','Aaron','Nick','Scott',
'Dave','Bob','Clint','Chris','Thomas','Ryan','Dennis',
'Thimo','Steve');
# ##### section added - sylver #####
$s0 = microtime();
$s = 'Taylor, '.'Zac, '.'Isaac, '.'Leo, '.'Aaron, '.'Nick, '.'Scott, '.
'Dave, '.'Bob, '.'Clint, '.'Chris, '.'Thomas, '.'Ryan, '.'Dennis ,'.
'Thimo, '.'Steve';
echo $s, '<br>';
$e0 = microtime();
#
unset($s);
$s = array();
#
$s1 = microtime();
foreach($x as $val) {
$s .= $val . ', ';
}
echo $s, '<br>';
$e1 = microtime();
#
unset($s);
$s = array();
#
$s2 = microtime();
foreach($x as $val) {
array_push($s, $val);
}
$y = implode(', ', $s);
echo $y, '<br>';
$e2 = microtime();
#
$t0 = (getmicrotime($e0) - getmicrotime($s0));
$t1 = (getmicrotime($e1) - getmicrotime($s1));
$t2 = (getmicrotime($e2) - getmicrotime($s2));
$td = $t1 - $t2;
#
echo "using concat: $t0 seconds<br />
using concat from array: $t1 seconds<br>
using array: $t2 seconds<br><br>";
?>