Search code examples
phphtmlpre

Strange behaviours of <?php when send to the browser in XMLHTTP response


Trying to print on my webpage this:

<?php  

// Usage: $master=new WebSocket("localhost",12345);

class WebSocket{
  var $master;
  var $sockets = array();
  var $users   = array();
  var $debug   = false;

  function __construct($address,$port){
    error_reporting(E_ALL);
    set_time_limit(0);
    ob_implicit_flush();

    $this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed");

Getting only:

master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed");

!!

In same time I can see in Chrome Inspector Network tab, that it was delivered to the browser correctly.

When I inspected PRE element I see this:

<pre><!--?php  

// Usage: $master=new WebSocket("localhost",12345);

class WebSocket{
  var $master;
  var $sockets = array();
  var $users   = array();
  var $debug   = false;

  function __construct($address,$port){
    error_reporting(E_ALL);
    set_time_limit(0);
    ob_implicit_flush();

    $this--->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed");

</pre>

Who comment out the code and why, and the main question how to stop it!?


Solution

  • Replace <?php with &lt;?php and replace $this-> with $this-&gt; and you'll get the intended result. FIDDLE

    In general, literal angle brackets and ampersands in HTML should be replaced with entities to avoid misinterpretation. In this case, Chrome assumes that receiving literal PHP code was a mistake, not intentional, and turns it into a comment. I can't find any documentation saying that it does this, but it's obvious what's going on.