English 中文(简体)
Do I need to escape this?
原标题:
  • 时间:2009-11-11 18:31:44
  •  标签:
  • php
  • escaping

It might be a bit unusual, but I need to echo <?php. However, I think that PHP treats it as an actual <?php and starts executing code instead of treating it as a string. How can I escape <?php and ?> so they can be treated as strings? There was nothing in the manual about this.

Thanks, I know this is a bit unusual.

最佳回答

You can use the &lt; and &gt; html entities (to replace < and > ). These are only handled in the browser, so PHP would not attempt to run that code.

问题回答

just use htmlentities function

<?php echo "<?php echo "hello" ?>" ?>

prints out <?php echo "hello" ?>

Check out PHP s sourcecode of functions on how they print out data.

http://in2.php.net/source.php?url=/manual/en/function.htmlentities.php

In HTML,

&lt;?php

Or in PHP:

echo htmlentities( <?php );

If this is your code:

<?php
  echo  <?php ;
?>

And you run that as a web page, you will see nothing. But not because PHP is not echoing your string <?php, but because the browser sees < and thinks that s the start of a tag, and tags are not displayed. It s obviously an error, but that s what the browser is doing.

To get around this, escape the < part, use htmlentities():

<?php
  echo htmlentities( <?php );
?>

Which when it gets echoed, will result in HTML source of:

&lt;php

Which when displayed in the browser shows:

<?php

If they are echoed in a string they will not be executed.

echo  <?php ?> ; // prints <?php ?>
echo "<?php ?>"; // prints <?php ?>

No, you do not have to do anything special.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签