PHP
  Home arrow PHP arrow Page 6 - PHP 101 (part 5) - The Wonderland Fact...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 101 (part 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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 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

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway