PHP
  Home arrow PHP arrow Page 2 - PHP Functions
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

PHP Functions
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 32
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek