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

Couple of questions

  1. What is the different between $func = "add_up"; and $func = 'add_up';

    1. Why are we using return instead of echo?

14 Answers

Kevin Korte
Kevin Korte
28,149 Points
  1. In this example, basically nothing. In more complicated applications, it can make a difference in PHP. Here is a good stack exchange answer to your question. http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php

  2. Return is less obtrusive than echo. If you echo the results of a function, the results will go to the screen every time. Returning the results just means that function evaluates to that return value, and from there, when you call the function, you can decided to echo it, or use the function results to do something else before echoing some value to the screen. As you get deeper into PHP, you'll see most functions you write you'll want the value returned, so you can do something else with it. At lease this way you can later decide if you want to echo the value or not. It's a touch more modular this way.

Thank you for the reference for Stack, it is a bit over my head right now. I will keep looking at it as I go through the course.

For #2. I am not sure I understand. Is it ok to give another example?

I don't I understand this "If you echo the results of a function, the results will go to the screen every time. "

Kevin Korte
Kevin Korte
28,149 Points

For answer number one, basically at this stage there is practically no difference between single and double quotes, just be consistent. If you open with a double quote, you have to close with a double quote.

For number two. Basically, if you echo the reults of a function in the function, and call the function, the results are going to be echoed to the screen. If you just return it, than you would have to echo the function itself to get that results.

Check out these shapshot to help you. Look at the index file.

The first one here is simple. Shows two identical functions, one returns the value, on echos the value. You can see how the functions react when called based on whether it was returned or echoed. https://w.trhou.se/lddpwcik1t

And the result: http://port-80-8qfiko3dln.treehouse-app.com/

This one is a bit more complicated, but it shows using some functions to manipulate values in other functions. Feel free to play around with the return and echo calls. You can fork it to your own Workspace so you can do whatever you want to it. Just play around and see what happens. https://w.trhou.se/zmqsn18drm

And the result: http://port-80-yu9g30kbx8.treehouse-app.com/

Thank you.

Is it ok to post the simple example? It looks like the urls are the same (https://w.trhou.se/zmqsn18drm)

Also, again. I REALLY appreciate the help. This is what makes Treehouse different. I have gotten a lot more help here than anywhere else.

Thanks!!

Kevin Korte
Kevin Korte
28,149 Points

You're welcome, and sure you can post that example. I'm okay with that.

Oh, my apologies.

I was asking if it was ok for you to post the simple example.

The complicated and the simple are the same URL.

Again, my apologies.

Kevin Korte
Kevin Korte
28,149 Points

I'm sorry, I didn't realize that. I updated that post; I'll also post the link here: https://w.trhou.se/lddpwcik1t

Thanks! Ok quick question:

Why would we want to just return a function if the value is not echo'd to the screen? In a program would the argument in the returned function be an argument like this. (testing to see if I can place code in chat :)

<?php my_name_returned($somevariable); ?>

or

<?php my_name_returned($oldname = $newname); ?>

Where in the last example, we are adding things to the name. For example, a person enters their name and the program would make their name sound funny like, "Tom the Bear" etc...

On a side note, we are making progress! I posted code!!! :)

Kevin Korte
Kevin Korte
28,149 Points

You're doing good so far. To answer your questions

Why would we want to just return a function if the value is not echo'd to the screen? We have to return something, this is usually the result of the function. Functions are just bits of reusable code is all. I showed why you might just want to return the results of a function in my other example, where I had the function un-reverse my name, knowing I wanted to concatenate my first and last name in a later function, I didn't what just my first and just my last name going to the screen, so I returned them instead of echoing them.

  • In a program would the argument in the returned function be an argument like this.* Yes, that is how you pass in an argument to a function. In the example, this would assume outside of the function you have a variable named $somevariable that has some data stored. Inside you're function, you can use that information to make choices inside the function.

Finally, yes you can set variable like that, but in your bottom example you are just reassigning one variable to be exactly what another variable is. It's not really doing much.

Here is a super simple way to do funny names, using functions and arguments

Code: https://w.trhou.se/a52azz5bsa Results: http://port-80-kz0eoyah4s.treehouse-app.com/

Ah thanks so much!

I took a look at the code. So, this portion:

return $array[$random_int];

Does this combine and store the array and the random name before the concatenation?

echo "Hello, " . $name . " " . get_funny_name($random_int) . "!";

great news though :) almost understand the echo'd statement 100%.

In the echo'd statement, you are passing the random int into the the function because we are attaching that to the $name varaiable?

Oops, that last statement should not be question, it is the way I understand it though :)

Kevin Korte
Kevin Korte
28,149 Points

Does this combine and store the array and the random name before the concatenation?

It is selecting one of the values randomly. If you were to var_dump($array) in the function, you'd get this

array(5) { [0]=> string(8) "the bear" [1]=> string(8) "the duck" [2]=> string(9) "the great" [3]=> string(9) "the clown" [4]=> string(8) "the fish" }

See the 0,1,2,3, and 4 before each term, that's what the random integer is doing. They get stored in a key => index sort of way. I can select any index, by calling the array variable and attaching to it in square brackets the key. In this case, the key is an integer, and at that a random integer generated by PHP.

If you changed it to $array[1] than the result would always be the duck. All $array[$random_int] is becoming is one of those 5 phrases. It does store the result of the function in the computers memory, which we call on later.

In the echo'd statement, you are passing the random int into the the function because we are attaching that to the $name varaiable?

Not quite. $name and $random_int have nothing to do with each other. $name is just the value of whatever was put into the input form on the index page. In the concat statement, when I call the function name, I pass it the value of the variable $random_int. Where I define the function, I also passed in $random_int as an argument, but that is the variable name that receives the actual argument.

Because of scope, variable inside of functions are only available in the function they are defined within. Look back at the updated code, I changed the functions $random_int variable to just be $r_i to prove this point.

UPDATED CODE: https://w.trhou.se/z2gdmkzj35

So at the bottom, in the concat where I call get_funny_name($random_int) I am calling the function, and passing it an argument, in this case an integer stored in a variable that was randomly generated above.

When the function runs, it assigns our variable $random_int to be $r_i inside the function. Now I can use $r_i since I know inside the function, it stores the variable of the random integer. The function still returns the same single phrase, and echos it to the screen.

Ah thank you! It is getting a little more clear