php inline with html working oddly

i support a website for a bunch of us jazz musicians. i have some php that works great to select a random image to display, but when i use it inline it give some odd output. the code below works from the command line, but when i try to use it inline it prints ‘=0 echo…’ as though the “$rand >= 0” wasn’t working as a comparison operator. is there something i’m missing about using php inline? any thoughts appreciated. --andy

<?php $dir=opendir("../httpdocs/img/spinners/"); $i=0; while($imgfile=readdir($dir)) { if ($imgfile != "." && $imgfile!="..") { $imgarray[$i]=$imgfile; $i++; } } closedir($dir); $rand=rand(0,count($imgarray)-1); if ($rand >= 0) { echo ''; } ?>

Do you have this code in the .php file or in .html? It looks like this page (and php code) is interpreted as html. So, everything between opening <?php … and $rand > is interpreted as html tag (thus not displaying), while content start with = 0

If this code is within .php file, you need to make sure php is installed and correct mime type is configured for .php files. If this code is within .html file, you can either rename it to .php or add handler for .html pages within this directory using .htaccess


AddHandler application/x-httpd-php .html

ahh… i see… yes it is an html file… i expected anything within <? and ?> to be interpreted as PHP. i did try changing extension to php, but some other code got confused… i’m going to do the job with external php for the time being, and learn some more about how PHP gets handled. thanks so much for your prompt reply. it was driving me nuts. :slight_smile: it does make sense. appreciate your help.

forgot to mention how much i like PHP and happy to be part of the community. hope i can help someone one day. thanks again.

You’re welcome! We hope to see you here often :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service