1

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 ?

14
  • 5
    Are you sure that short tags are enabled on your server? Commented Feb 5, 2013 at 20:41
  • 2
    do a 'view source' in you browser. if the php code shows up there, you server is misconfigured and isn't running .php files through the php interpreter. by default, .html files are usually NOT run through php, so no surprise that's not working. Commented Feb 5, 2013 at 20:42
  • 1
    @palatok are you running this on a server or your local machine? if it's on your local machine, are you running apache and navigating to the page, or double clicking the file to open it? your code works properly for me Commented Feb 5, 2013 at 20:45
  • 1
    double click to open won't run php, that's the issue then. you need an apache server (like xampp) and you need to navigate to it like localhost/somefolder/new.php double clicking it won't run the script. if you install xampp and start up the apache server, then stick your file inside the htdocs folder and use localhost/new.php, that should work Commented Feb 5, 2013 at 20:48
  • 1
    it's because things like php and jsp have to be "run" not just opened Commented Feb 5, 2013 at 20:50

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

<p>Roses are <?php echo $color; ?></p> . i tried this also ... not working
2

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

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

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

Comments

2

<?=$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 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.