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 PHP Basics Unit Converter Integers

sapna rathore
sapna rathore
468 Points

Hi, I'm not able to get the output in the console.

Hi, I'm writing very basic PHP code such as <? php echo "hello world"; ?>, but I'm not able to get the out come in the console. My code is in the hello.php file (I typed php hello.php and pressed enter but no out come or error)

Harry James
Harry James
14,780 Points

Hey Sapna,

The code isn't supposed to appear in the console - it should appear online at /hello.php.

Let me know whether it is or not! :)

4 Answers

Adam Goddard
Adam Goddard
1,756 Points

Hey Sapna,

I had the same problem. I was able to solve it by saving the file before i ran it in the console. If there is a little red dot next to the file name (hello.php) then you need to save it first.

Hope this works for you too

Adam

Oziel Perez
Oziel Perez
61,321 Points

remember that hello.php is actually used as a web page. The <?php echo "hello world";?> snippet will write the phrase to the php file. Then you must view the file in a browser, or in your case, preview the file, and "hello world" will be displayed as html. If anything, almost all echoed output is in html

php can be output to a console if used correctly.

first you need to add the path to "php.exe" in your system environment variables.

On Windows 7, XP, Vista, 2008, 2012 and up:
Go to Control Panel and open the System icon (Start → Control Panel)
Go to the Advanced tab
Click on the 'Environment Variables' button
Look into the 'System Variables' pane
Find the Path entry (you may need to scroll to find it)
Double click on the Path entry
Enter your PHP directory at the end, including ';' before (e.g. ;C:\php)
Press OK

this will allow you to run "php <FILE>.php" in cmd.

C:\TEMP>dir hi*
 Volume in drive C is Evo250GB
 Volume Serial Number is 27CA-4DD0

 Directory of C:\TEMP

03-Sep-16  23:11                28 hi.php
               1 File(s)             28 bytes
               0 Dir(s)  28,795,969,536 bytes free

C:\TEMP>type hi.php
<?php echo "hello world";

C:\TEMP>php hi.php
hello world

C:\TEMP>

in addition make sure that the opening tag is correct:

use "<?php" and not "<? php", there should be no space.

also you don't have to use the closing "?>" tag in php files.

Thank you, that was very thorough. I took off the closing tag, and then I was able to see it in the console. Why does Treehouse says it needs the closing tag, if it's optional? Also, I still can't see 'Hello world' in preview. Maybe that's later in the class?

Harry James
Harry James
14,780 Points

Hey Marie Urbina,

It's good practice to include the closing tag anyway. It explicitly states where the PHP code ends. This is useful in specific contexts, say a website consisting of HTML and PHP written in the same .php file.

Hope this helps :)

Kyle Vassella
Kyle Vassella
9,033 Points

I thought I was having this problem too, but it was a combination of two problems.

  1. Make sure your workspace is saved.
  2. Your eyes may have fooled you. Look again at the console results, to the very left most of the statement line - if you're code is written correctly you should see a '1' (the result of your echo $num_one; statement). In my case it was hard to see because it comes directly before the next 'treehouse:~/workspace$' with no space between the two.