PHP
  Home arrow PHP arrow Page 6 - PHP 101 (part 5) - The Wonderland Factor
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 101 (part 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-09-06


    Table of Contents:
  • PHP 101 (part 5) - The Wonderland Factor
  • So Who Are You, Anyway?
  • Graffiti For The Masses
  • Calling Godzilla
  • Q
  • Arguments And Responses
  • The Wonderland Factor

  • 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 101 (part 5) - The Wonderland Factor - Arguments And Responses
    ( Page 6 of 7 )

    Now, all this is well and good, but it's not exactly setting the house on fire, is it? Well, there are a couple of things you need to add into the mix for your user-defined functions to actually start earning their keep: return values and function arguments.

    Usually, when a function is invoked, it generates a "return value" - this may be value of the last expression evaluated, or a value explicitly returned to the main program via the "return" statement. Our next example demonstrates how a return value works:


    <html> <head> <basefont face=Arial> </head> <body> <?php function answer_yes() { $answer = "Yes!"; return $answer; } function answer_no() { $answer = "No!"; return $answer; } ?> Would you watch an Al Pacino movie without thinking twice? <br> <? $response = answer_yes(); echo $response; ?> <p> Does 2 + 2 equal 6? <br> <? $response = answer_no(); echo $response; ?> <p> Is this the coolest PHP tutorial on the planet? <br> <? $response = answer_yes(); echo $response; ?> </body> </html>
    The output of the script above has not changed; however, there are significant differences in the manner of its execution. In the example above, the functions do not provide any output - they simply assign a string to the variable $answer. When the function is invoked, it passes the value of this variable to the main program, where it is assigned to the variable $response and displayed on the page.

    And just as a PHP function can return values *to* the main program, it can also accept "function arguments" *from* the main program. Take a look:

    <html> <head> <basefont face=Arial> </head> <body> <?php function multiply($alpha, $beta) { $product = $alpha * $beta; return $product; } $num1 = 9; $num2 = 16; ?> <? echo $num1; ?> times <? echo $num2; ?> is <? $answer = multiply($num1, $num2); echo $answer; ?> </body> </html>
    The first thing you should notice here is that the function definition has changed - the parentheses, which seemed to be there only for decorative purposes, now enclose two PHP variables called $alpha and $beta. When arguments are passed to the function, they will be stored in these PHP variables for the duration of the function and can be further acted upon by the statements within the function.

    In the example above, $num1 (9) and $num2 (16) are the two arguments passed to the function multiply() The function multiply() accepts the two arguments, performs a mathematical operation on them, stores the result in $product and returns $product to the main program as $answer.

    Here's the output:

    9 times 16 is 144
    The multiply() function can be used with any pair of numbers - as the arguments passed to it change, so will the result. This is how functions allow you to re-use the same piece of code over and over again, without any need to re-write code each time.{mospagebreak title=Flavour Of The Month} An important point to be noted here is that it isn't possible to use variables outside the function within the function definition...unless you use the special "global" keyword. This keyword, when used within a function, allows you to use a variable from outside the function within the function as well. To illustrate this, consider the following two examples:


    <?php // set variable $flavour = "strawberry"; // function to change variable value function change_flavour($name) { $flavour = $name; return $flavour; } echo "Before calling the function, the flavour is $flavour.<p>"; // change variable value change_flavour("blueberry"); echo "After calling the function, the flavour is $flavour."; ?>
    Here's the output:

    Before calling the function, the flavour is strawberry. After calling the function, the flavour is strawberry.
    And here's what you need to do to make the variable a global variable:

    <?php // set variable $flavour = "strawberry"; // function to change variable value function change_flavour($name) { //make the variable global global $flavour; $flavour = $name; return $flavour; } echo "Before calling the function, the flavour is $flavour.<p>"; // change variable value change_flavour("blueberry"); echo "After calling the function, the flavour is $flavour."; ?>
    Here's the output:

    Before calling the function, the flavour is strawberry. After calling the function, the flavour is blueberry.


     
     
    >>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...





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