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 Functions Function Returns and More PHP Closures

Couple more questions

  1. what is the difference between:

A. $name = "Mike"; B. $name = 'Mike';

  1. In this video I noticed that we had a semi-colon at the end of the function, but I did not see one at the end of the function on the previous video. Why is this?

Example: function answer() { return 42; }

Now, in this example, we have the below function and it will not work unless there is a ";". I am not sure why.

<?php

$name = 'Mike';

$greet = function() use ($name){ echo 'Hello there'; };

$greet();

?>

5 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, William. The difference (in PHP, at least) between double and single quotes is that double quotes will do interpolation, while single quotes won't. What that means is this:

<?php

$name = 'Mike';

echo 'Hello, $name!';    //<-- this will print out as: Hello, $name!
echo "Hello, $name!";  //<-- this will print out as: Hello, Mike!

?>

As for your second question, if you have a single line of code inside a PHP block or a function, the comma is optional. Once you put more than one line of code, though, it will be necessary or you will get errors.

Thank you!

In the video the code spans for 4 lines and yet there is no semi-colon for both of the functions presented.

(When I copied and pasted it came out on one line)

He had it like this:

function answer() { return 42; }

With no semi-colon.

hmmm, it seems it keeps putting it on one line. :(

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

No, worries! I assume that it looks something like this:

<?php
function answer() {
  return 42
}
?>

In which case, no semicolon is necessary because the line inside the function is a single line. If the function was more complicated (as most are), then you'd definitely need it. I always use semicolons because it makes my code much more readable, and honestly, leaving off an occasional semicolon here and there doesn't save that much time.

function answer() { return 42; }

how do you post code?

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Like this:

(Always have one single line blank between your comment body and the three backticks.)

```php <-- three backticks (usually under the ESC key) and your code type

... your code here

``` <-- three backticks to close

(Another blank line if you have more text to type out below)

Et voila!