PHP
  Home arrow PHP arrow Page 2 - PHP Functions
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP Functions
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2006-08-01

    Table of Contents:
  • PHP Functions
  • Functions that Take Arguments
  • Setting Default Values
  • Creating Functions that Return a Value
  • Using Variables in Functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP Functions - Functions that Take Arguments


    (Page 2 of 5 )

    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


       · Bit of a n00b myself but im sure the code is incorrect.. this is how it should...
       · no his code is correct except for the part where he explains the use of default...
       · The description of the default arguments is wrong - and confusing.What he is...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT