The real advantages of using functions correctly and to the maximum capability.

Froger93 Froger93 is offline Offline 31 Days Ago, 4:40 am |
2
Hey guys,

I just wanted to let everyone know that functions can be used very constructively and there are more to them then most think.

Okay this example function is actually pulled from a class of mine so it would probably make more sense in my class but it should make sense anyway.

Right we should all know how to make functions, for those of you that don't I'm going to explain the process.

First off, a function will contain a method or a block of code that we may want to call several times further along in our script. The advantage of adding the block to a function is that we can minimize (so to speak) our code and instead of calling this every time we need to retrieve certain information we use a simple one line call to our function.

Extensive code (padded out a little)
  1. <?php
  2.  
  3. $userid = "RANDOM_ID";
  4. $query = mysql_query("SELECT * FROM `posts` WHERE `auth_id` = '$userid'");
  5. if( @mysql_num_rows( $query ) > 0 ) {
  6. while( $row = mysql_fetch_object( $query ) ) {
  7. echo '<div><a href="http://siteaddress.com/viewpost.php?id=' . $row->id . '">' . $row->title . '</a> Last post on ' . date( "Y-m-D" , $row->time ) . '</div>';
  8. }
  9. } else {
  10. echo "This user hasn't ever made a post.";
  11. }
  12.  
  13. ?>

Okay so that as you can see is a fair amount of code to have to write every 5-10mins, so we need to simplify this:
  1. <?php
  2.  
  3. function get_posts() {
  4. $userid = "RANDOM_ID";
  5. $query = mysql_query("SELECT * FROM `posts` WHERE `auth_id` = '$userid'");
  6. if( @mysql_num_rows( $query ) > 0 ) {
  7. while( $row = mysql_fetch_object( $query ) ) {
  8. echo '<div><a href="http://siteaddress.com/viewpost.php?id=' . $row->id . '">' . $row->title . '</a> Last post on ' . date( "Y-m-D" , $row->time ) . '</div>';
  9. }
  10. } else {
  11. echo "This user hasn't ever made a post.";
  12. }
  13. }
  14.  
  15. ?>

Now we have wrapped this code block in the functions brackets everything that is inside of those brackets is going to be called every time we write get_posts(); on our page.

Although this is great it's not very constructive yet. For instance the code above will only get the posts for one user. So if we needed this function for 150 users we would need to rewrite this function 150 times. Not fun.

So what we can do to get around this is adding a parameter to the function:
  1. <?php
  2.  
  3. function get_posts( [b]$user_id[/b] ) {
  4.  
  5. }
  6.  
  7. ?>

So now when we call the function we can call it with the user who we wish to retrieves ID and then the function should work with all of our users, there is one more change we need to make though.
  1. <?php
  2.  
  3. function get_posts( $user_id ) {
  4. $userid = [b]$user_id[/b];
  5. $query = mysql_query("SELECT * FROM `posts` WHERE `auth_id` = '$userid'");
  6. if( @mysql_num_rows( $query ) > 0 ) {
  7. while( $row = mysql_fetch_object( $query ) ) {
  8. echo '<div><a href="http://siteaddress.com/viewpost.php?id=' . $row->id . '">' . $row->title . '</a> Last post on ' . date( "Y-m-D" , $row->time ) . '</div>';
  9. }
  10. } else {
  11. echo "This user hasn't ever made a post.";
  12. }
  13. }
  14.  
  15. ?>

So in order for us to call this function for a user with the user id that is 123456 we would call get_posts( 123456 ) an the function would echo out all of the posts they have made.

So thats pretty neat but there is more. Right say we have put this function in to production and it is in maybe 20-30 scripts on our live site but we want to change this function to include another parameter to control whether or not we want the title of the posts or the number of the posts, now as some of you may know adding another variable/parameter to the function will cause those without the second parameter to call errors. However there is a simple method to get around this problem.

  1. <?php
  2.  
  3. function get_posts( $user_id , [b]$number=false[/b] ) {
  4.  
  5. }
  6.  
  7. ?>

Because we have added the =false to the end of our newly added parameter we are essentially saying this parameter doesn;t have to be submitted. Okay so here's how we could implement this into our function.

  1. <?php
  2.  
  3. function get_posts( $user_id , $number=false ) {
  4. $userid = $user_id;
  5. $query = mysql_query("SELECT * FROM `posts` WHERE `auth_id` = '$userid'");
  6. if( @mysql_num_rows( $query ) > 0 ) {
  7. [b] if( $number = true ) {
  8. echo mysql_num_rows( $query );
  9. } else {[/b]
  10. while( $row = mysql_fetch_object( $query ) ) {
  11. echo '<div><a href="http://siteaddress.com/viewpost.php?id=' . $row->id . '">' . $row->title . '</a> Last post on ' . date( "Y-m-D" , $row->time ) . '</div>';
  12. }
  13. [b]}[/b]
  14. } else {
  15. echo "This user hasn't ever made a post.";
  16. }
  17. }
  18.  
  19. ?>

So now if we call the function with only the first parameter we will get a list of posts by the user, however if we include the second parameter the we will get the number of posts the user has posted. So this is just a few methods of using functions.

Another point I'd like to make about functions us the global keyword. Some of you may have noticed that if you use a variable from outside of the function then the value is false or it is empty. The way in which we would get around this is by declaring it as global.
  1. <?php
  2. $some_variable = "some_value";
  3. function some_function() {
  4. echo $some_variable;
  5. }
  6. some_function(); // Wouldn't return anything.
  7.  
  8. function some_function() {
  9. global $some_variable;
  10. echo $some_variable;
  11. }
  12. some_function(); // Would return "some_value"
  13. ?>

So I hope you have either learned more about functions or better understand them from reading this little article.

Thanks for reading.
Quick reply to this message  

Tags
functions, php, tutorial

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC