Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping a literal <?php and <? in a PHP script

Tags:

php

escaping

I am templatizing my php.ini using PHP. I have a script to set up a development environment by generating httpd.conf, apachectl, and php.ini from templates using a CLI PHP script. Unfortunately there are literal <? and <?php strings in php.ini (in a comment). Is it possible to escape those somehow so php doesn't interpret them as normal PHP escape sequences?

Currently my workaround is to wrap them in a real PHP escape sequence that outputs them as a string, like this:

; This directive determines whether or not PHP will recognize code between
; <?php echo "<? and ?>" ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php echo "<?php and ?>" ?> tag combination. With the wide spread use
;
like image 489
nohat Avatar asked Sep 23 '09 23:09

nohat


People also ask

How do I escape characters in PHP?

Escape Sequences In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.

What is the meaning of escaping to PHP?

Escaping is a technique that preserves data as it enters another context. PHP is frequently used as a bridge between disparate data sources, and when you send data to a remote source, it's your responsibility to prepare it properly so that it's not misinterpreted.

What is the correct way to end a PHP statement?

Note: PHP statements end with a semicolon ( ; ).

What is the meaning of \r escape sequence in PHP?

Widely used Escape Sequences in PHP \\ – To escape the backslash. \$ – To escape $. \n – To add line breaks between strings. \t – To add tab space. \r – For carriage return.


1 Answers

This:

<?php echo "<?php"; ?>

Will output:

<?php
like image 81
gahooa Avatar answered Oct 13 '22 11:10

gahooa