Why can I access PHP objects inside a string? -
this php code rendering html anchor element. why this:
$archives .= " <a href='" . get_year_link($archpost->year) . "' title='click go full listing " . $archpost->year . "' target='_blank'>[list^]</a>"; work this:
$archives .= " <a href='" . get_year_link($archpost->year) . "' title='click go full listing $archpost->year' target='_blank'>[list^]</a>"; coming c/c# , such, have preference first variant, because think 1 cannot dump variables smack-dab in middle of string. apparently php has no problem this. why not?
this called string interpolation, , available in c# well.
personally, find using string interpolation bad practice. makes code hard read in ide, , can lead misinterpretation of strings. (maybe wanted dollar sign mean dollar sign, because used double quotes " instead of single quotes ', have bug.) concatenate instead:
$var = 'something' . $somethingelse; if you're using echo, more performant echo list, rather concatenate:
echo 'something', $somethingelse; the other issue, @ least used in example, dumping data directly context of html no escaping done. if output contained reserved entities, generating invalid ambiguous html. can lead security issues depending on context used, such xss problems. want use htmlspecialchars() around arbitrary data used in html. (or, use template engine you.)
Comments
Post a Comment