Home arrow PHP arrow Page 6 - PHP 101 (part 5) - The Wonderland Factor

Arguments And Responses - PHP

This concluding article in the series illustrates PHP's filefunctions, with examples of how to read and write files on the system. Italso includes an explanation of user-defined functions, return values andfunction arguments, together with some not-so-real-life examples.

TABLE OF CONTENTS:
  1. PHP 101 (part 5) - The Wonderland Factor
  2. So Who Are You, Anyway?
  3. Graffiti For The Masses
  4. Calling Godzilla
  5. Q
  6. Arguments And Responses
  7. The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 7
September 06, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: