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

Can't complete code challenge - code is correct though

Hi Guys,

This is related to the file handing with php challenge. I've completed the code and it displays correctly, though I am being told that "I do not see the <h3> tags"

They are definitely there, and viewing the source of the displayed results also shows them.

Is this me, or a bug?

Thanks,

Si

index.php
<?php
//add code here
$files = scandir('example');

foreach ($files as $file) {
  if (strpos($file, 'fun')) {
    echo str_replace('fun', '<h3>'.$file.'</h3>', $file);
    } else {
    echo $file;
  }
 }

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Si Pendlebury ! Yup, you definitely have <h3> tags and this Bummer! message is somewhat misleading. I believe what it means is that it doesn't see them in the places it expects. Much of this is to do with your interpretation of the instructions.

The file names are only supposed to show if they contain the word "fun". But you're showing all file names and have the ones (most of them) that contain "fun" within <h3> tags. But you are also missing a couple of file names. You are missing the ones that start with "fun".

You wrote:

if (strpos($file, 'fun'))

This checks for "truthiness", but remember that the strpos will return a 0 if the string begins with "fun". And 0 evaluates as "falsey", thus, all of those files will end up in your else clause. The solution: change that if statement to not be explicitly equal to FALSE. The !== FALSE is key here. Secondly, remove the else clause entirely.

I feel like you can get it with these hints, but let me know if you're still stuck! :sparkles:

Got it - thanks for that :)

Si