The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20150630

php add conditional html tag parameter: example how to avoid having to escape quotes w complex (curly) variable parsing syntax

i have a php/html application where i want to add a conditional html tag "title" attribute if a value for "email" exists.

the trouble people usually run into is the escaping of single and/or double quotes, but if u use php's "complex (curly) variable parsing syntax", then u don't have to escape any quotes at all (generally):

<a href="#" <? if($this->data['email'] != '') echo " title='{$this->data['email']}'" ?>>Email</a>

so if "email" is set, then the rendered output for the tag will look like this:
<a href="#" title='example@example.com'>Email</a>

the "title" attribute will most likely be rendered with double quotes (""), not single (''), but for purposes of illustration, i'm showing u the relationship between the php code and the rendered output.

fyi, the "simple syntax" way is like this:
<a href="#" <? if($this->data['email'] != '') echo ' title="'.$this->data['email'].'"' ?>>Email</a>

(gives identical output as "complex" syntax)

No comments:

Post a Comment