开发者

Printing php code with highlight_string

I am trying to show my code with highlight_string. All of my variables are being stripped out of the printed code. What am I doing wrong? An example of what is happening ...

<?php highlight_string("
    <?
        $a=3;
        $b=4;
        if ($a < $b){
        echo 'a is less than b';
        }
    ?>"); 
?>

The output looks like this

    <?
        =3;
        =4;
  开发者_运维问答      if ( < ){
        echo 'a is less than b';
        }
    ?>


Replace the double quotes (") with single quotes (') PHP tries to fill up variables printed within double quotes.

<?php highlight_string('
    <?
        $a=3;
        $b=4;
        if ($a < $b){
        echo \'a is less than b\';
        }
    ?>'); 
?>


Tried using single quotes?

Using double quotes prints the values of variable rather than their defined names.


When you use double quotes ", you allow PHP to replace all instances of a variable with its value. For example, if I do this:

$a=5;
echo "$a";

My output will be:

5

If instead I did...

$a=5
echo '$a';

My output will be

$a


In a php double-quoted string the dollar sign indicates that the variable after the dollar sign should be placed into the string, ie

 $a = 1;
 echo("A is $a");
 #prints A is 1

Even if $a is not defined, php will assume that you mean to create $a here:

 echo("A is $a");
 #prints A is 

To get around this, use single quoted strings which take the string literally:

 echo('A is $a');
 #prints A is $a


Single quotes not double

<?php highlight_string('
    <?
        $a=3;
        $b=4;
        if ($a < $b){
        echo \'a is less than b\';
        }
    ?>'); 
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜