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 trialShane May
5,114 PointsCan anyone see why this is working? (php function)
<?php
function hey(){
return 'hey';
}
$space = hey();
echo $space;
?>
Ted Sumner
Courses Plus Student 17,967 PointsEdited to format code.
3 Answers
Daniel Gauthier
15,000 PointsIt's working on this end, which could mean a few things:
*1. You're clicking on the php file, which will just produce a blank page in Chrome with the single line written out in full:
<?php function hey(){ return 'hey'; } $space = hey(); echo $space; ?>
If this is the case, then you may be simply opening the php file like you would an html file. PHP files don't work when you just open them, they need to be run on a server (local or hosted) running PHP, otherwise you'll see any html/css as normal, but the php code will show up as text.
*2. You're pathing to the file through a server, but the server doesn't have PHP enabled, or the PHP version isn't adequate to run the code.
If this is the case, look into getting PHP installed on the server and the code will run properly.
It's more than likely the first option though, so just upload your php file to a server and access it using a browser by navigating to the site the way a user would and you'll get the results you're looking for.
Shane May
5,114 PointsYes because it doesn't work
Ted Sumner
Courses Plus Student 17,967 PointsTry clearing your cache. Also, do other PHP sites work in your environment?
You can see the code works in this snapshot.
Ted Sumner
Courses Plus Student 17,967 PointsI agree with Daniel's answer. I would also like to add you can simplify your code like this:
<?php
function hey(){
return 'hey';
}
echo hey();
?>
Daniel Gauthier
15,000 PointsDaniel Gauthier
15,000 PointsHey Sheldon,
Looking at the code, I can't see why it wouldn't work.
You created a function that returns the string hey, then set the value of $space to whatever hey() is returning, then you're echoing out the value of $space, which at this point is the string 'hey'.
Is there something you felt shouldn't be working here?