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 Building Websites with PHP Dependency Management Namespacing

"Hello World!" doesn't appear

Yesterday I began this course and everything was great. When I wrote this code:

<?php
require 'vendor/autoload.php';
date_default_timezone_set('America/Cancun');

$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.txt', Monolog\Logger::WARNING));

$log->addWarning('Foo.');
echo "Hello World.";

The app.txt appears with this line:

[2015-05-22 18:20:44] name.WARNING: Foo [] []

I ran my code again and the result was this:

[2015-05-22 18:20:44] name.WARNING: Foo [] []
[2015-05-22 13:25:03] name.WARNING: Foo [] []

But today when I launched the workspace and I changed my code to this:

<?php
require "vendor/autoload.php";

  date_default_timezone_set("America/Cancun");

  use Monolog\Logger;
  use Monolog\Handler\StreamHandler;

  $log = new Logger('name');
  $log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));

  $log->addWarning('Oh Noes.');

  echo "Hello World!";

It doesn't work. Please I need help.

Jason Wist
Jason Wist
2,483 Points

It doesn't show Hello World in the browser or it doesn't write to the log file app.txt?

It doesn't show Hello World in the browser AND it doesn't write to the log file app.txt.

15 Answers

Jason Wist
Jason Wist
2,483 Points

A few things you can try.

Having: error_reporting(-1); ini_set('display_errors', 'On');

At the start of your program will display errors for you. This could help with trouble shooting.

I was having troubles as well. I was getting Server 500 errors.

I tried renaming the file (old_index.php). Creating a new one and copy/pasting your code. That worked for some reason.

Also make sure you are trying to clear cache (ctrl + F5).

I think sometimes workspaces just bugs out and we just have to deal with it with restarting workspaces or trying to clear our browsers cache.

Hi Jason. I wrote: error_reporting(-1); ini_set('display_errors', 'On'); and this is I got:

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "app.txt" could not be opened: failed to open stream: Permission denied' in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84 Stack trace: #0 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\StreamHandler->write(Array) #1 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(265): Monolog\Handler\AbstractProcessingHandler->handle(Array) #2 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(317): Monolog\Logger->addRecord(300, 'Oh Noes.', Array) #3 /home/treehouse/workspace/index.php(26): Monolog\Logger->addWarning('Oh Noes.') #4 {main} thrown in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 84

Jason Wist
Jason Wist
2,483 Points

So the error "Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "app.txt" could not be opened: failed to open stream: " Shows that php couldn't open app.txt. Try deleting app.txt or like I suggested creating a new index.php.

Also feel free to file a bug report to Treehouse with that information. If we are lacking the permission to write to app.txt that's out of our hands and a problem with workspaces.

I created a new index.php and it didn't work, but I deleted the app.txt and it worked again. I can't mark your answer like the best answer. But it is the best answer. Thanks Jason.

Adam Oliver
Adam Oliver
8,214 Points

I would also like an answer to this. Worked fine earlier. Closed workspace and reopend and it no longer displays 'Hello World!'. Nothing has changed.

I found deleting the app.txt file also worked for me, that must be the fix!

Misty Shaw
Misty Shaw
10,825 Points

Deleting app.txt has worked for me as well. Also, when I restarted my browser, app.txt reappeared in the Workspace. The modified warnings now appear with "Oh Noes."

It work! But why have to delete this file?

Jason Wist
Jason Wist
2,483 Points

Echo Yang That's why I recommended adding

error_reporting(-1); 
ini_set('display_errors', 'On');

so we could see what error was occurring.

For Eduardo he was unable to write to the txt file. If the syntax is correct that seems like it is an issue with Workspaces itself and nothing wrong with the PHP code written.

Try added those two lines and read the output of them. It can hint or outright tell you what is going wrong.

Michael Critelli
Michael Critelli
5,072 Points

I was having the same issue as everyone else. I am not to positive why its happening but I found this snippet of code and this works for me.

require_once 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\BrowserConsoleHandler;

$log = new Logger('name');
$log->pushHandler(new BrowserConsoleHandler);

$data = array(1,2,3,4);

// add records to the log
$log->addWarning('Foo');

That's weird! I wonder if this is a composer thing.. It could be that Workspaces isn't storing your installed dependencies - I'm just talking out loud. If that is the problem, you could try running composer install / update.

If that's not the problem, you're code looks fine to me and has been tested by Jason Wist, so it might be worth raising with Treehouse support :)

Matt Pill
Matt Pill
15,689 Points

I'm having the same issue! Everything was fine until I added my namespacing. I did the same as Jason Wist by simply creating a new index.php file and it seemed to do the job. Very odd!

Adam Oliver
Adam Oliver
8,214 Points

It also means I can't carry on with the next part, as it relies on that functioning.

jason chan
jason chan
31,009 Points

did you drag your index.php file outside of the vendor folder? It's more of IT problem. LOLs. Location of files do matter in programming and typing where the file is... It's like making nothing appear. So programming is magic. LOLs.

Adam Oliver
Adam Oliver
8,214 Points

If that was the case Jason, it wouldn't have worked before.

Did you try deleting app.txt??? It worked for me.

I've tried: Renaming the php file, clearing the cache, restarting the browser, refreshing the file tree inside of workspace and deleting app.txt... so far nothing has worked and i still receive a 500 error. Any help would be appreciated.

Here is my code:

<?php
require "vendor/autoload.php";

  date_default_timezone_set("America/NewYork");

  use Monolog\Logger;
  use Monolog\Handler\StreamHandler;

  $log = new Logger('name');
  $log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));

  $log->addWarning('Warning!');

  echo "Hello World!";

Also, after deleting 'app.txt' and remaking the file my code isn't logging the warnings anymore.

just tried deleting app.txt again and refreshed and now my code is working again. It remade the app.txt file and is now logging warnings as well :)

James Kemp
James Kemp
433 Points

If you have this issue again, type:

"chmod 777 app.txt" (without quotes) into console and try again.

If you haven't even got an app.txt file, create it, then run the above.

Dan Varnau
Dan Varnau
7,681 Points

I was having this issue and recreating app.txt and index.php didn't work. This console command did fix it though. What does it do?

Brent Larson
Brent Larson
2,554 Points

For whatever reason, my code will run when I am not using any namespacing on line 12, (assuming you followed the video directly)

Tony Brackins
Tony Brackins
28,766 Points

Yep, Deleting app.txt worked. Was having same issue.

Zeljko Porobija
Zeljko Porobija
11,491 Points

At first, I got this error message (feel free to skip it): Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "app.txt" could not be opened: failed to open stream: Permission denied' in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:87 Stack trace: #0 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\StreamHandler->write(Array) #1 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(289): Monolog\Handler\AbstractProcessingHandler->handle(Array) #2 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(341): Monolog\Logger->addRecord(300, 'Foo', Array) #3 /home/treehouse/workspace/index.php(11): Monolog\Logger->addWarning('Foo') #4 {main} thrown in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 87 Then I deleted app.txt and everything went fine. The new app.txt has been created, with a correct log.