PHP Lesson 16 - more function examples

Started by Parham, October 25, 2003, 03:58:29 PM

Previous topic - Next topic

Parham

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:


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):


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:


<?

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.

[Unknown]

Just remember that it is a bad idea to make "wrappers" for functions you don't like - it only slows things down.

-[Unknown]

Palli Moon

I realy admire your work it has helped me more than any other tutorial i have ever read. And, Well, they are not few ;)
Palli "Bootlegger" Moon

pulpitfire

Quote from: [Unknown] on October 25, 2003, 04:17:26 PM
Just remember that it is a bad idea to make "wrappers" for functions you don't like - it only slows things down.

-[Unknown]

What's the fastest way to keep the square root function from taking in negative numbers and yielding errors?

pulpitfire

#4
Quote from: Palli Moon on November 15, 2003, 04:04:32 PM
I realy admire your work it has helped me more than any other tutorial i have ever read. And, Well, they are not few ;)

Agreed.  Does anyone know of some kind of program that will walk you step by step through a series of brief lessons, and that lets you input and test code for each? And could you please get a larger avatar Palli?  I'm having a hard time seing your name there :).

[Unknown]

Quote from: pege on November 18, 2003, 10:36:34 AM
What's the fastest way to keep the square root function from taking in negative numbers and yielding errors?

Awareness.  It's better to say this:

if ($test < 0)
   die('Ack!  What\'re you trying to do, Mr. Crazy?');
else
   $test = sqrt($test);

Or if you don't want an error message...

$test = sqrt(abs($test));

The reason is that functions incur a penalty in speed.  PHP's functions are all written in C++ - and are very, very fast.  However, whenever you do something yourself, it takes a lot longer.  PHP is not anywhere as fast as C++... so, calling a PHP function from your own function only doubles the penalty of calling a function.

While functional programming is wonderous and great, the general rule of thumb is to not create functions that are shorter than five lines.  If it's longer than that, it's worth the penalty because you won't go insane, and the size of the code will be smaller... (which actually does help.)

Just remember that whenever you call a function, it has to *find* the function.  Windows, etc. programming is worse here - the processor actually has to chew along and find the position of the new code.  This is called a "jump", because the processor is "jumping" to the new portion of code.  In assembly this gets very complicated, because "short jumps" aren't bad at all, but "long jumps" are horrible.

Luckily, PHP is simpler.

-[Unknown]

Palli Moon

Palli "Bootlegger" Moon

tim antley

Quote from: Parham on October 25, 2003, 03:58:29 PMThere 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.

That one paragraph explained more than php.net [nofollow], hotscripts, dynamicdrive, or any book I've read.

Thanks so much for this entire endeavor.
BayouMX.com [nofollow]

Advertisement: