PHP
  Home arrow PHP arrow Page 5 - 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 - 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.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek