PHP
  Home arrow PHP arrow Page 3 - 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 - Setting Default Values


    (Page 3 of 5 )

    When creating a function that takes arguments, you can set default values for it:

    function function_name($argument2, $argument = 6){

    statements

    }

    Why would you want to set default values in a function? Well, if you are for example writing a program that involves calculating tax, which does not change often but does change sometimes, a function like this would be appropriate. It allows the assumption of a value, but still permits you to change the value when needed.

    The default value is overwritten when you specify a value, otherwise the value will stay the same. For example, let's create a new function that will take your age, first name and last name:

    function mydetails($firstname, $lastname, $age=23){

    echo "My name is " .$firstname. " " .$lastname. " and I am
    ".$age." years old.";

    }

    To call this function, type: mydetails(34,'John','Doe')

    And the result will be...

    My name is John Doe and I am 34 years old

    As you can see, the default value of 23 has been overwritten to 34.

    If, for example, you call this function without the $age argument, the function will assume that $firstname is the first argument. In other words, it will replace $age with $firstname and $firstname with $lastname. In that case, when you call this function, the results will be:

    My name is Doe and I am John years old.

    Just make sure that you write the default values after the other standard values (the arguments without defaults):

    function mydetails($firstname, $lastname, $age=23){

    echo "My name is " .$firstname. " " .$lastname. " and I am
    ".$age." years old.";

    }

    This is because PHP assigns the values directly to the arguments as they are received from the call line.

    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