### MSN Random Online

 Lesson 16 - More function examples   Thursday, 15 February 2007 17:53

# Lesson 16 - More function examples

In this lesson, we're going to tackle another function problem.  One of the functions in PHP that I personally don't like the functionality of is the sqrt() function.  Why don't I like how this function works?  Well it's because it takes in negative numbers, and it continues execution of your program. If you use the sqrt() function and use whatever number you get across your program, you'll get a ton of errors if you used a negative argument.  That's not to say it's bad, but the way I'd rather have it work is for it to exit the program in case the argument is negative.

So what I'll do is create my own sqrt() function, and call it squareroot() (since we can't override built in functions)

Again, first we'll have to come up with a name.  squareroot() as mentioned above seems appropriate.  We could always use a more condensed name like sqroot() or sqrt2(), but again, that's a style choice and you can choose whatever name you think is appropriate.

The next thing we'll have to do is decide what variables this function will require (through parameters/arguments).  This function will only require one variable - the number we wish to find the square root of.

Finally, we have to create the body of our function based on the variables we chose as parameters.  For the body, the code is fairly simple.  If the argument is less than 0, exit the program with an error (we can use the built-in die() function for this).  If the argument is any number greater or equal to 0, return the square root of that number.

Putting it all together, we get this:

Code:
`function squareroot(\$num) {  if (\$num < 0) {    die('error, cannot use negative number in squareroot()');  } else {    return sqrt(\$num);  }}`

If the number is negative, the program will die and stop all execution.  Simple enough?

The second thing I want to give a little tutorial on is static variables.  When you normally run a function, any variables used inside that function quickly disappear after the function finishes executing.  Sometimes, under some circumstances, you may want those variables to stick around.  Here is a quick demonstration of that (recoded from the PHP documentation to use better variable names):

Code:
`function continuous_count(\$num = 0) {  static \$current = 0;  echo \$current;  \$current += 1;}`

What this function will do is declare a variable as static inside a function, and continue to increment that variable every time the function is called.

So:

Code:
`<?function continuous_count(\$num = 0) {  static \$current = 0;  echo \$current;  \$current += 1;}continuous_count();continuous_count();continuous_count();continuous_count();continuous_count();?>`

would produce "01234"

There are a few things you should know about static variables.  First of all, they can't take on any expressions.  You cannot assign it something like "sqrt(16)" or "1+2" or anything that requires PHP to execute to get the value of.  Second of all, a static variable is only declared the first time a function is called.  On the first run of the above code, the variable \$current is set to "0".  On the second run of the above code, since \$current is already static and has a value, the line is skipped and the function continue to output and increment the variable.

### Top Members

No top members yet.