Welcome our webmaster and SEO forum
Please enjoy the forum, contribute what you can, and wind up the Moderators!
Results 1 to 2 of 2

Thread: Tutorial: A Beginner's Guide to Using Functions

  1. #1
    jackfranklin is offline Junior Member
    Join Date
    Feb 2008
    Posts
    6

    Default Tutorial: A Beginner's Guide to Using Functions

    A Quick Guide to PHP Functions.

    What is a function?
    A function is a way of repeating PHP code over and over again quickly and easily. Instead of having 20 lines of code repeated 5 times, you use the 20 lines of code once, and repeat 1 line of code 5 times.

    Ok. Now you understand exactly why functions are so useful, I’ll go into a bit more detail about how they can be used.

    To create a function, type:
    PHP Code:
    function name {
      } 
    And all the code for the function goes in between those curly braces.

    You can have a simple function like this:
    PHP Code:
    function random() {
      return 
    rand (15);
      } 
    And then you can call it:
    PHP Code:
    random(); 
    And you will get a random number between 1-5. Now, in-between those brackets you can have parameters. This may confuse you at first, but once you get it it is very easy.

    PHP Code:
    function random($min$max) {
      return 
    rand ($min$max);
      } 
    Just take a minute to read it over. Remember in the last function, the random function was between 1 and 5? Now, it will be between $min and $max. How do we assign $min and $max? When we call for the function:
    PHP Code:
    random(510); 
    It will give you a random number between 5 and 10. All I have done when calling the function is replaced $min with 5, and $max with 10.
    If you want to assign the function to a variable, you can easily, just like you would for most variables.
    PHP Code:
    $variable random(510); 
    Let’s make things a bit more complex. Readers of my ‘Beginning PHP’ will be familiar with this example. I want to work out the area of my rectangle, and the perimeter. Now, knowing what we do about parameters within functions, you should understand this code:
    PHP Code:
    function rectangle($width$height) {
      
    $area $width $height;
      
    $perim = ($width $height) * 2;
      echo &
    #8216;<h4>Area is: ‘.$area.’</h4>’;
      
    echo &#8216;<h4>Perimeter is: ‘.$perim.’</h4>’;
      

    Slightly confused? I’m going to show the same function again, but replace $width and $height with a number, which should help you out.
    PHP Code:
    function rectangle(35) {
      
    $area 5;
      
    $perim = (5) * 2;
      echo &
    #8216;<h4>Area is: 15 </h4>’;
      
    echo &#8216;<h4>Perimeter is: 16</h4>’;
      

    Hopefully that is clear to you know. To call on the function, and assign the width and height values, we do:
    PHP Code:
    rectangle (510); 
    That concludes a brief tour of functions. Hopefully you now understand how to create a function, how to call a function, and how functions spare PHP developers a lot of time.

    Remember – It’s not about reading this, to learn anything you must type the code out for yourself.

    Jack

  2. #2
    damienb is offline Junior Member
    Join Date
    Oct 2010
    Posts
    10

    Default

    Thanks for the tips but can you recommend more advanced php lessons?
    Last edited by damienb; 11-18-2010 at 10:20 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124