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

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,362 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
    }
}