PHP
  Home arrow PHP arrow 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? 
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
    ( Page 1 of 5 )

    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.

    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.

    function_name();

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

    Script:createrndpass.php

    <?

    function randpass()

     $chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyzABCDEF
    ghijklmnopqrstuvwXYZ1234567890";

     $thepass = '';

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

     {

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

     }

     return $thepass;

    }

    //to use the function

    $password=randpass();

    ?>

    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT