[Wolves] php mailto formatting query

Mark Rogers mark at quarella.co.uk
Wed Sep 19 08:10:14 UTC 2012

On 13/09/12 00:12, Wayne Morris wrote:
> On 12/09/2012 19:34, James Turner wrote:
>>> //print("<TD><a
>>> href=\"mailto:info at xxxx.com?subject=".$row["postcode"]."\"
>>> target=\"_blank\">Email Enquiry</a>\n";
>> Looks OK to me other than a missing ) immediately before the final semi-colon.
> Spot on ;-) Thanks both ;-)

Coming at this a bit late so I wasn't going to reply but:

I'd suggest either of the following are easier to read:

Suggestion 1: Use single quotes for the PHP string so that the embedded double 
quotes don't need escaping, since you're only using PHP doule-quotes for the 
carriage return at the end anyway:
     print('<TD><a href="mailto:info at xxxx.com?subject='.$row['postcode'].'" 
target="_blank">Email Enquiry</a>'."\n");

Suggestion 2: If you're going to use double quotes, let PHP expand your 
variables and rely on the fact that HTML also allows single quotes:
     print("<TD><a href='mailto:info at xxxx.com?subject=$row[postcode]' 
target='_blank'>Email Enquiry</a>\n");

This assumes that you've already URL encoded the value of $row['postcode']; if 
you haven't then I would go with a modified version of the first one:
href="mailto:info at xxxx.com?subject='.urlencode($row['postcode']).'" 
target="_blank">Email Enquiry</a>'."\n");

Of-course there's always the option to drop out of PHP:
href="mailto:info at xxxx.com?subject=<?=urlencode($row['postcode'])?>" 
target="_blank">Email Enquiry</a>

.. but personally I find code that drops in and out of PHP very hard to read.

Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450
Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER

More information about the Wolves mailing list