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

Shane May
Shane May
5,114 Points

Can anyone see why this is working? (php function)

<?php
function hey(){
  return 'hey';
}
$space = hey();
echo $space;
?>
Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey 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?

Edited to format code.

3 Answers

Daniel Gauthier
Daniel Gauthier
15,000 Points

It'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
Shane May
5,114 Points

Yes because it doesn't work

Try clearing your cache. Also, do other PHP sites work in your environment?

You can see the code works in this snapshot.

I 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();
?>