Advertisement:

Author Topic: PHP Lesson 16 - more function examples  (Read 38826 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 16 - more function examples
« on: October 25, 2003, 03:58:29 PM »
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: [Select]
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: [Select]
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: [Select]
<?

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.

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 16 - more function examples
« Reply #1 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]

Offline Palli Moon

  • Newbie
  • *
  • Posts: 2
  • Gender: Male
  • I'm a llama!
Re: PHP Lesson 16 - more function examples
« Reply #2 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 ;)
Palli "Bootlegger" Moon

Offline pulpitfire

  • Sophist Member
  • *****
  • Posts: 1,132
  • Gender: Male
  • This is what the php parser looks like.
    • Pulpitfire Devotionals
Re:PHP Lesson 16 - more function examples
« Reply #3 on: November 18, 2003, 10:36:34 AM »
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?

Offline pulpitfire

  • Sophist Member
  • *****
  • Posts: 1,132
  • Gender: Male
  • This is what the php parser looks like.
    • Pulpitfire Devotionals
Re: PHP Lesson 16 - more function examples
« Reply #4 on: November 18, 2003, 10:42:05 AM »
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 :).
« Last Edit: November 18, 2003, 10:56:55 AM by pege »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 16 - more function examples
« Reply #5 on: November 18, 2003, 06:25:51 PM »
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]

Offline Palli Moon

  • Newbie
  • *
  • Posts: 2
  • Gender: Male
  • I'm a llama!
Re: PHP Lesson 16 - more function examples
« Reply #6 on: November 20, 2003, 04:03:36 PM »
Yup!  :D
Palli "Bootlegger" Moon

Offline tim antley

  • Semi-Newbie
  • *
  • Posts: 19
  • Unamused
    • BayouMX.com
Re: PHP Lesson 16 - more function examples
« Reply #7 on: April 05, 2007, 01:22:36 AM »
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.

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

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