Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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