Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Basic File Handling Reading Files

Gurveer Singh
Gurveer Singh
9,004 Points

can i get help with reading data from php file?

If the filename contains the string "fun", display the name of the file surrounded by H3 tags.

HINT: strpos will return 0 if the result is found at the beginning of the file. Returns FALSE if the needle was not found.

index.php
<?php
//add code here
$files = scandir('example');
if (srtpos($files, 'fun') != false) {
  echo '<h3>  <h3>';
}

2 Answers

Jeffrey Howard
Jeffrey Howard
18,173 Points

Hi! You are almost there, just a few important items that needs to be added and fixed in order to complete this task.

  1. You need a foreach loop to properly list out the files in the $files array.

  2. Add your if statement inside the foreach loop but first the comparison operator should be typed with a double equal sign. For example !== false.

  3. Variable $file should be placed in between the H3 tags.

The final product should look like the code below:

//add code here
$files = scandir('example');

foreach ($files as $file) {
    if (strpos($file, 'fun') !== false ) {
      echo '<h3>' . $file . '</h3>';
    }
}

Hope this helps! Good Luck and happy coding!

Gurveer Singh
Gurveer Singh
9,004 Points

Thankyou very much, Jeff