Jump to content

Importance Of Returns?


Sy Mercade
 Share

Recommended Posts

Hey there, I'm kinda stuck on the reason "return" is necessary. I'll use W3school's example.

<?phpfunction add($x,$y){$total=$x+$y;return $total;}echo "1 + 16 = " . add(1,16);?>

Alright, I understand that a function is something you use to group code together that you'll probably use over and over again, and you can change the values on the spot. Like:function askQuestion($theforum){echo "I have another question! Please answer this" . $theforum; wait awhile (not a code)}if (stuck == true){askQuestion("W3Schools")}but what I don't understand is, why, in the example from W3schools, is return necessary? What exactly does return do?? Does it change the function into whatever is returned?? I don't understand why you can't just substitute that code for something like this.

<?phpfunction add($x,$y){$total=$x+$y;}echo "1 + 16 = " . add(1,16);?>

This is probably a really stupid question but it (along with loops and arrays) don't quite click with me yet, any advice is appreciated!!Oh yeah, and I skipped ahead to the file part for a sec, but when I tried the code on my domain, it said I didn't have permission. I tried the whole CHMOD thing, but the only permissions it allows me to set is Read Write and Execute. fopen is completely unusable, however I have been able to read from txt files already on the server. What do I do??Thanks!

Link to comment
Share on other sites

The return construct tells the function what value to report back to its caller. In the example above, you attempt to echo add(1,16). The script knows what actual value to echo when you call add() through the return statement - in this case, $total.What script did you try using fopen() with? Remember that PHP doesn't run as the current session's user.

Link to comment
Share on other sites

I tried the whole CHMOD thing, but the only permissions it allows me to set is Read Write and Execute.
Those are the only three permissions available. You can give the file owner, the owner's group, and everyone else permission to read, write, or execute the file. Those are all you need.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...