palatok
palatok

Reputation: 1034

why php and html mixed code is not working?

I tried to write this simple code but it's not working. This is my code...

    <html>
    <body>

    <?php
    $color = "red";
    ?>

    <p>Roses are <?=$color?></p>

    </body>
    </html> 

i saved this code as new.php it shows in the browser window only "Roses are" text. the value of $color is not printed. i also saved it as new.html but the result is same. what is the problem? what have i done wrong ?

Upvotes: 1

Views: 859

Answers (3)

Oskar Persson
Oskar Persson

Reputation: 6765

Try

<html>
<body>

<?php
$color = "red";
?>

<?php echo('<p>Roses are '.$color.'</p>'); ?>

</body>
</html>

or

<html>
<body>

<?php
$color = "red";
?>

<p>Roses are <?php echo $color; ?></p>

</body>
</html>

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324820

<?=$color?> only works if you have short tags enabled in the configuration, or if you are using PHP 5.4 or higher.

Since it's not showing anything, this is clearly not the case. Either change the configuration, upgrade to PHP 5.4, or use the full <?php echo $color ?>

Upvotes: 2

Joseph Silber
Joseph Silber

Reputation: 220136

It seems like short tags are disabled on your server. Use this instead:

<?php $color = "red"; ?>

<p>Roses are <?php echo $color; ?></p>

Upvotes: 2

Related Questions