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

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>'; }

index.php
<?php
$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,386 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.