Home arrow PHP arrow PHP Functions

PHP Functions

If you're looking for a way to save time when you program, look no further. Creating functions lets you reuse code that you've used before without having to rewrite the whole thing. Keep reading to learn how.

  1. PHP Functions
  2. Functions that Take Arguments
  3. Setting Default Values
  4. Creating Functions that Return a Value
  5. Using Variables in Functions
By: Jacques Noah
Rating: starstarstarstarstar / 46
August 01, 2006

print this article



While programming, you'll discover that you use certain pieces of code over and over, either within a script or in several scripts. Instead of writing that code over and over you can create a function and place those routines in it. This will save you time and make programming easier, especially as websites become more complex. Another advantage of a function is that it executes wherever and whenever you call it, in the same way that print() displays text.

User Defined Functions

The syntax to create a user defined function is:

function function_name(){

//statements here


You use the same naming conventions as you do for variables, without the dollar sign. Another rule to remember is to not use spaces when giving a name to a function. Otherwise it will be interpreted as two different words which will result in an error message. Use an underscore instead. Also, as a matter of good coding practice, it is good to give a representative name to a function. For example, set_name() would be a better function name than name1().

There are no limits as to how many statements can be included in the function.  A function must include all the required elements, which are:

  • Function name
  • Opening and closing parentheses - ()
  • Opening and closing braces - {}
  • Statements

The actual formatting of the function itself is not important, as long as the above elements are included. You call the function by its name to execute it.


The above will cause the statements in the function to be executed.

Let's create a function that generates a random password.

We will call the function randpass():



function randpass()

 $chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyzABCDEF

 $thepass = '';



  $thepass .= $chars{rand() % 39};


 return $thepass;


//to use the function



The above function creates a password with random numbers and letters. The $chars variable contains letters and numbers that are mixed up. The content of that variable is then randomized with the rnd() function and a result is returned in the $thepass variable.

The "return $thepass;" part of the function returns the function result to whatever variable you want it in. Therefore to run this function, all we need to do is:

$password =randpass();

>>> More PHP Articles          >>> More By Jacques Noah

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: