PHP question

Marketcake

God of Leisure
Dec 6, 2009
450
8
0
Paradise
I'm using mail() to send a message for a custom script. I finally got it working fine in text-only form. I used this to make sure it was less than 70 chars as per the php documentation

PHP:
$message = wordwrap($message, 70);

Then I realized though I would really like to add bold and italics. I did more research and added the appropriate headers:

PHP:
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

When I viewed the email however, there were a lot of linebreaks way before 70 characters, even though in my code there was no <br/> OR \n in that particular spot

1. If I use mime, do I need to include \n in my message, or only breaks. maybe they were conflicting?

2. When I use mime, do I need to use wordwrap function or can html emails have lines over 70 chars
 


Okay I figured it out. if anyone cares, mime doesnt appear to need \n nor wordwrap.

I am still curious though what the difference is between

"this message is $goodorbad";

and

'this message is ' . $goodorbad;

i tried googling it but google doesnt really like me searching " . " ' . ' lol
 
The only difference (that I'm aware of) between the quoted strings and apostrophe'd ones is that when using quotes you can include stuff like \n for a newline and \t for a tab and they will show a newline and a tab - and variables show their value.

When using apostrophes \n will stay as \n and 'this is a $var'; will not show the value of $var.
 
I do now

............................................________
....................................,.-'"...................``~.,
.............................,.-"..................................."-.,
.........................,/...............................................":,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:"........./
..............?.....__.........................................:`.........../
............./__.(....."~-,_..............................,:`........../
.........../(_...."~,_........"~,_....................,:`........_/
..........{.._$;_......"=,_......."-,_.......,.-~-,},.~";/....}
...........((.....*~_......."=-._......";,,./`..../"............../
...,,,___.\`~,......"~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-"
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`
 
You know there is a programming section of the forum right?

Moved.

There's a great article which talked about the use of single and double quotes somewhere, if I remember correctly I think it was by Tobsn. Poss on his blog, but I can't find it right now.

Anyway the conclusion was that use of single quotes is more efficient.

If you had an apostrophe in the sentence, then you would need to either use double quotes or escape the apostrophe.

'this message's content is ' . $goodorbad; wouldn't work.

"this message's content is $goodorbad"; works

'this message\'s content is ' . $goodorbad; more efficient.

Just found this:

Parsing variables within strings uses more memory than string concatenation. When writing a PHP script in which memory usage is a concern, consider using the concatenation operator (.) rather than variable parsing.
 
  • Like
Reactions: Marketcake
Moved.

There's a great article which talked about the use of single and double quotes somewhere, if I remember correctly I think it was by Tobsn. Poss on his blog, but I can't find it right now.

Anyway the conclusion was that use of single quotes is more efficient.

If you had an apostrophe in the sentence, then you would need to either use double quotes or escape the apostrophe.

'this message's content is ' . $goodorbad; wouldn't work.

"this message's content is $goodorbad"; works

'this message\'s content is ' . $goodorbad; more efficient.

Just found this:

Not quite because escaped characters are only parsed inside of double-quotes, so either:

echo "this message's content is ", $goodorbad;
echo "this message's content is $goodorbad";

For echoing I believe either of those are more efficient than using the concatenation operator because you are calling one less function by leaving it out as echo can take comma-seperated arguments (and you have to use the double quotes because of the apostrophe). If you weren't dealing with apostrophe in your text than you could do:

echo 'this message content is ', $goodorbad;

Or if you're setting a variable use concat:

$blah = 'this message content is ' . $goodorbad;

I think that makes sense?
 
  • Like
Reactions: kineticbug
Not quite because escaped characters are only parsed inside of double-quotes, so either:

echo "this message's content is ", $goodorbad;
echo "this message's content is $goodorbad";

For echoing I believe either of those are more efficient than using the concatenation operator because you are calling one less function by leaving it out as echo can take comma-seperated arguments (and you have to use the double quotes because of the apostrophe). If you weren't dealing with apostrophe in your text than you could do:

echo 'this message content is ', $goodorbad;

Or if you're setting a variable use concat:

$blah = 'this message content is ' . $goodorbad;

I think that makes sense?

+Rep.

Concat . if you're combining (ala variable).
Chain , if you're echoing.

And of course only use double quotes if you're actually trying to include a variable in a string.

But honestly, you wouldn't notice any kind of performance different, it's just good practice.
 
For echoing I believe either of those are more efficient than using the concatenation operator because you are calling one less function by leaving it out as echo can take comma-seperated arguments (and you have to use the double quotes because of the apostrophe).

Except concatenation then echoing is about 3x faster than using commas.
You can check it yourself #523020 - Pastie
 
+Rep.

Concat . if you're combining (ala variable).
Chain , if you're echoing.

And of course only use double quotes if you're actually trying to include a variable in a string.

But honestly, you wouldn't notice any kind of performance different, it's just good practice.

Wrong, concatenation will always be faster whether it's a variable or an echo. The same rules apply to single quotes, they will always be faster than double quotes.
 
The best thing about PHP is its community support and documentation

Read up on the variations of strings and how you can use them here

In regards to using ' vs " the jury is still out. Most times you will find the actual server has more effect on the speed rather then the code itself.

in saying that a lot of people have done actual testing
Microbenchmarks of single and double qouting
The PHP Benchmark

single quotes: 27.59 ms
double quotes: 27.74 ms
Then there is the issue of concatenation with single quotes vs embedded variables with double quotes, which is easier to read, which works the fastest etc.

anyways, with email if you are going to send in any user input then make sure you parse the data for new line characters and email headers before sending out the email. spammers love it when you don't check that type of stuff
 
Zend Framework - Use it

Zend Framework: Documentation

Code:
$mail = new Zend_Mail();
$mail->setBodyText('My Nice Test Text');
$mail->setBodyHtml('My Nice <b>Test</b> Text');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();
 
Thanks, Looks like I'll be switching to single quotes now. Dammit. All these years using double quotes... I should have guessed.

Once you get into strongly typed stuff single quotes are for char and double are for Strings, oh my! <-- my attempt at recovering from looking like a n00b