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

Tome Perica
Tome Perica
14,604 Points

File Handling with PHP

Why this is not correct (Challenge Task 2 of 3) ?

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

foreach($files as $file){ echo '<h3>'; if(substr($file, 0,3) == 'fun'){ echo $file; } echo '</h3>'; }

$files = scandir('example');

foreach($files as $file){
  echo '<h3>';
  if(substr($file, 0,3) == 'fun'){
    echo $file;
  echo '</h3>';

2 Answers

Umesh Ravji
Umesh Ravji
42,362 Points

Hi Tome, you're not looking for files that begin with the string fun, but files that contain fun anywhere in the name. Using the strpos() function makes the most sense in this case, not the substr() function.

if (strpos($file, 'fun') !== false) {
    // do something with $file

You also have to make sure that you're only echoing out the H3 tags inside your conditional check, otherwise the H3 tags will printed out, even though the condition isn't true.