First GO!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Blyant
Forum Newbie
Posts: 12
Joined: Fri Sep 30, 2005 8:14 am

First GO!

Post by Blyant »

Below is my first attempt at PHP with a lot of help from this forum and php.net, I want the PHP to check a directory called images and return a list. I also want the PHP to print out an Xml file to use in a flash document. (If I made sense:D ) My web hosts have Php 4.4.

Is this an efficent way to write / use Php?

Code: Select all

<?php
echo "<?xml version=\"1.0\"?>\n";
echo "<portfolio>\n";

$dh = opendir('images');
while (false !== ($file = readdir($dh))) {
   if(strpos($file,'.')>0){
       echo "<work" . " Url=\"images/".$file . "\" />\n";
   }
}
echo "</portfolio>\n";
?>
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

You can mix and match php with output (html/xml/whatever). Also better to use print.
Stick a space after 'if', inbetween your greater than sign, and before the {.
There's no reason to concatenate after 'work.'
If you use double quotes, include the variable inside them. Double quotes will parse variables and newlines (\n & \r). Or use single quotes and you don't have to escape your double quotes in the text. Or use single quotes in the text. ^_^ Choices, choices. You can also use {} around a variable in a string to make it easy to find etc. ("sometext{$var}moretext" rather than "sometext$varmoretext")

Code: Select all

<?xml version="1.0"?>
<portfolio>
<?php
$dh = opendir('images');
while (false !== ($file = readdir($dh))) {
   if (strpos($file,'.') > 0) {
       print("<work url='images/{$file}' />\n";
   }
}
?>
</portfolio>
Post Reply