Webmaster Forum
Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs

UK Web Hosting
UK Web Hosting
Website Hosting
Website Hosting
UK One Way
UK One Way
Free Website Thumbnail Creator
 
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-19-2008, 04:08 PM
Junior Member
 
Join Date: Feb 2008
Posts: 7
iTrader: 0 / 0%
jackfranklin is on a distinguished road
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 (1, 5);
  }
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(5, 10);
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(5, 10);
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(3, 5) {
  
$area = 3 * 5;
  
$perim = (3 + 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 (5, 10);
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
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks

Webmaster Resources
UK Web Hosting
UK WW SEO Tools
Free site submission
Web Directory
 
Advertisement
Get top 10 exposure
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
2 column 2 columns 301 addtype advice bbpress bid bidding directory blog post british telecom broadband cheap chip maker community coupon code designs directory domain for sale domain name ecommerce ecommerce information ecommerce poll edegra fibre optic forum fraud free free portal script free script generate revenue generic viagra google graphics heaven intel internet spending james in london kamagra keywords layout link exchange links wanted linux and windows server local search modeling monopoly online retail online spending optimising owg in london parking photography php picture of the day purple robots.txt scour scripts search engine search quality site promotion special discount speed cameras submission theme usability web hosting website win a network wordpress

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 10:36 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150