When I run the following script, nothing gets printed. Why is it so ?
<?php
$var = "<?php echo 'Hey !'; ?>";
echo $var;
?>
When I run the following script, nothing gets printed. Why is it so ?
<?php
$var = "<?php echo 'Hey !'; ?>";
echo $var;
?>
It echoes "nothing" because your browser doesn't understand <?php
tags, so it won't show the tag contents; it should show something when you select view the page source though.
The reason for this behaviour is that the default content type of your script is set as text/html
(you can confirm this by looking at the response headers) and in the context of HTML, you should use htmlspecialchars()
echo htmlspecialchars($var);
In fact, as a general rule, you should always escape variables appropriately when you output them.
Alternatively you could let the browser know that your output should not be interpreted as HTML; you can do this by setting an appropriate response header:
header('Content-Type: text/plain');
With the above content type your output is shown verbatim by the browser.
change this
$var = "<?php echo 'Hey !'; ?>";
into this
$var = "<?php echo 'Hey !'; ?>";
becousing your syntex is wrong for php engine... make change as:
<?php
$var = "<?php echo 'Hey !'; ?>";
echo $var;
?>
to
<?php
$var = 'Hey !';
echo $var;
?>
Set the content type as text
and you can see your output in your browser.
<?php
header('Content-type: text/plain');
$var = "<?php echo 'Hey !'; ?>";
echo $var;
?>
It is because your browser is expecting html and it doesnt understand the php tags you have in the output.
Other wise using htmlspecialchars
function would be a better option.
try something like this
$var = '<?php echo \'Hey !\';?>';
echo htmlspecialchars($var);