PHP
  Home arrow PHP arrow Page 5 - 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 - Using Variables in Functions


    (Page 5 of 5 )

    You've used functions that take arguments. These arguments are variables that have been passed to the function as arguments when a function is executed. You can also use variables within a function using the global statement.

    The global statement extends the scope of a variable. The term scope refers to the realm in which the variable can exist. For example, the scope of a variable that you create in a script exists throughout the life of that script. In other words, the variable can be used anywhere in the script, in the same way that environment variables such as $_SERVER['PHP_SELF'] exist throughout the server.

    Functions create a new realm for variables to exist in. Function variables such as the arguments passed by a script, and variables defined within a function, exist only within that function and cannot be used outside of the function's realm. These kind of variables are called "local" variables with a "local" scope. In the same vein, the variables outside a function can only be passed to a function as an argument or by the use of the term "global."

    The "global" statement makes a variable be the same outside and within the function. In effect a local variable with local scope is turned into a global variable with global scope. So, any changes made to this variable within a function are also passed on to the variable when it is used outside of the function.

    The syntax of a global variable is:

    function function_name($argument){

    global $variable;

    statements

    }

    As long as $variable exists outside of the function, it will also have the same value within the function. Here's a simple demonstration:

    <?php
    $n1 = 2;
    $n2 = 2;

    function Add()
    {
       global $n1, $n2;

       $n2 = $n1+ $n2;
    }

    Add();

    echo $n2;
    ?>

    The above outcome will be "4" because we declared $n2 and $n1 as global within the function, and that caused those "global" variables to reference the variables declared in the script.

    Conclusion

    Functions make it easy to code when you are working on large projects and also make your code more portable. Another thing to keep in mind about functions in general, is that their execution time is slower than if you'd just written it out. But you would not really notice it, especially if you are not a programmer. This is particularly true if you use classes in larger projects. We will be discussing classes in an upcoming article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 3 hosted by Hostway
    Stay green...Green IT