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

samsor ithnin
samsor ithnin
3,175 Points

please i am stuck at challenge task 2 of 3

if file name content the string " fun".surrounded it with h3 tag. i still couldn't find the answer about this question. it just saying " do not see all required content.

index.php
<?php
$files = scandir( 'example' );

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

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Samsor

The scandir() function returns an array containing all of the files within the example directory. In order to check for the string fun within each file, you must do so within some sort of loop.

foreach ($files as $file) {
    // do something with $file
}

There's a minor issue with your code. You are supposed to echo out the name of the file, $file, between the H3 tags, not the string fun.

foreach ($files as $file) {
    if (strpos($file, 'fun') !== false) {
      echo '<h3>fun</h3>'; // fix this to echo out the value of $file instead of fun
    }
}