back to the basics, programming 101

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

back to the basics, programming 101

Post by rathlon »

Functions are the topic of discussion here:

Ask me yesterday and I understood functions in and out.

Ask me today, and I'd curse you for offending my ear canals with your spicy words.

A variable defined inside a function is said to have "function scope". That is, it is ONLY accessible to the program statements housed inside the functions opening and closing curly braces. A variable declared outside a function, inside me main() part of the script is said to have "global scope". That is, it is accessible to any portion of the script and is freely alterable / changeable. Consider the following:

Code: Select all

<?php


function increase($var1, $var2 = 1)
{
  
   $var = $var1 + $var2

}

//increase $a
$a = 10;
increase($a);
echo $a;


would display:

"10" and not "11"

?>
Easy enough. It would display 10 because the variable $a has global scope. Therefore passing it into the function would not result in the answer we're looking for, "11". Why? Becuase the $var is a new variable inside the function, and not a reference to the variable $a like one may think. Therefore, in order to increase the value of $a, one should pass by reference the value of $a. Passing by reference, rahter than passing by value, allows a "reference" to the original value to be passed. You are free to alter / change the value just as you would to a variable passed by value. The difference, by reference makes a reference to the original variable, any subsequent changes to that value are reflected in the original. Passing by value, results in a "new" copy of the value of the original variable. Therefore, any changes to the "new"value results in changes to a different variable than the one passed to the function as the original parameter. Whew that's a mouthful. So, if we wrote:

Code: Select all

<?php

function increase(&$var1, $var2 = 1)
{
  
   $var = $var1 + $var2

}

//increase $a
$a = 10;
increase($a);
echo $a;


would display:

"11" 
?>
Notice the "&". This allows us to make a reference to the orignal value being passed in as a parameter. Therefore, $a is now referenced, so the statement:

Code: Select all

<?php

$var = $var1 + $var2;
?>
Now equates to our intended value of 11 because the original value of 10 is the value actually being increased, changed, and displayed.

Now, here's where I fall off the damn board. What and how does a return statment work? I always thought something like:

Code: Select all

<?php

function do_A_Query($query)
{
    $result = mysql_query($query);
    $number = mysql_num_rows($result);

    for()
    {
        $rows = mysql_fetch_row($result):
         //process and load an array here named $projects (or whatever)
    }

  return $projects; 
  return $number;
}

   

?>
would allow you to now use the variable named $projects to display its contents etc. However, I seem to be wrong. My initial thought was, perhaps its the rules of scope. A variable defined inside the funciton has "function scope". But wouldn't the return statement negate that scope? Isn't that the purpose of using the return? Even if I defined the variable outside the function (which would give it global scope) and passed it as a parameter, a copy of the orignal would be made. Therefore, I wouldn't actually be returning the original and now altered or loaded value, I'd be returning a different value, or a copy of the original. Thus, using the original value (defined outside wiht global scope) would give me erronous output, especially if I had not initialized the array or whatever type variable it was. Rather, what I had to do in this situation was define the variable inside the funtion as "global projects;". I then could alter and change the info and reference it outside the function after I had changed / altered it just as if it had been defined outside the functin? Why, becuase the "global" keyword gives it global functionality. So, why did I have to define it as gobal, why didn;t my original attempt of returning it work? Forgive the length, I just wanted to make sure I got all my details out, hopefully other newbs can learn as well. Please keep in mind, the syntax above is not meant to be an accurate representation of how it should be defined, I was attempting to make examples. So please, don't go correcting my syntax errors ;). Thanks for the help in advance guys.
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

When you return a value from a function you have to return the value into a variable. Just returning the value doesn't give it global scope. I seem to understand it completely, just don't try and fight the way php handles these things :)

Keith
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

this would return the value and put it in $c

Code: Select all

<?php
function add($a, $b) {
   $var = $a + $b;
   return $var;
}

$c = add(4,7);
?>
once a return statement is reached ... the function ends
so another return statament below that is never reached like in ur example.

and yes ... the returned value must be assigned to something like $c here ... or else printed out .. or else it will be inaccessible.

if u need to modify multiple items, then you use the reference idea :
And no return statment is needed !

Code: Select all

<?php
function increment(&$var1, $$var2) {
    $var1++;
    $var2++;
}

$a = 1;
$b = 2;
increment($a, $b);
?>
Post Reply