Is there a way to configure php heredoc variable substitution?

Viewed 112

I regularly use the PHP HereDocs and NowDocs to add large strings of HTML, JavaScript, and CSS content to my code, but I sometimes run into situations where I want to have a PHP variable's value substituted into a HereDoc string that also contains jQuery code, which causes errors/problems when the $ prefix for jQuery conflicts with the $ prefix for the PHP variable's name. This situation only gets worse when I prefix JavaScript object variables that are assigned jQuery Objects because I indicate this by prefixing the JavaScript variable name with a $, i.e., $variable-name.

Is there a way to configure PHP to only substitute variable values when it 'sees' a variable specified as ${variable-name} instead of $variable-name? This way the PHP compiler won't produce an error when it can't find a PHP variable for the jQuery statement that follows the $ or cause problems when it substitutes my jQuery statement or JavaScript variables for a PHP variable that happens to exits?

When I use a HereDoc, I explicitly use double quotes around the opening identifier to make it clear that I'm going to specify a PHP variable in the string that will have its value substituted into the string, i.e.:

$statement = <<<"STATEMENT"...${phpVar}...STATEMENT;

I realize that the default action of a HereDoc is to allow substitutions and explicitly enclosing the identifier with double quotes just makes it easier to differentiate the HereDoc from a NowDoc statement, and that enclosing a php variable in curl-braces only makes the variable easier for me to see the PHP variable amongst the rest of the text in the string.

I use NowDocs to prevent the unwanted substitutions or to make the compilation faster.

However, I'm looking for a way to make variable substitution only happen when I explicitly use double quotes in my HereDocs and curl-braces around the PHP variable.

To get around situations where I need to use jQuery and PHP variable substitution in the same text string, I break my HTML/JavaScript/CSS into chunks and enclose them in HereDocs and NowDocs, but this makes reading my code much harder.

If it isn't possible to make ${php_variable} the only way to cause variable value substitution rather than $php_variable in HereDocs that explicitly use double quotes, then how can I submit this as an enhancement request to the PHP Development Organization?

Thanks

0 Answers
Related