Home arrow PHP arrow Page 2 - PHP Functions

Functions that Take Arguments - PHP

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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the last section we created a function that generates random passwords. What if we want a password generator that only displays a password of a certain length? For example, what if we only want three characters, or six or eight? This is where a function that takes arguments becomes necessary. Fortunately for us, in PHP you can create such functions.

The syntax for writing functions that take arguments is as follows:

function function_name($argument){

statements

}

The argument is usually in the form of a variable, but can also be literal, or both:

function function_name($argument,'Avalue'){

statements

}

If you do not add an argument where it is needed, the function will display a error message. The same thing will happen if you only add three arguments when a function expects four. To call a function that takes an argument is the same as calling the ones that don't take arguments. You just need to remember to pass along the needed arguments.

Let's modify our randpass() function to enable us to choose how many characters we want in our password.

Script:createrndpass.php

<?

function randpass($length)

 {

  $chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyzABCDEF
ghijklmnopqrstuvwXYZ1234567890";

 $thepass = '';

 for($i=0;$i<$length;$i++)

 {

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

 }

 return $thepass;

 }   

//to use the function

$password=randpass();

?>

The above shows the result is a six character password.

And this screen shot shows the result for an eight character password.

Shown above is what happens when you don't pass an argument to the function.

There are no limits to how many arguments a function can take, and you can use multiple functions in a script. You can also create a separate file for all your functions and just include it in your scripts to use the functions.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: