PHP/Functions/Return Value — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:04, 26 мая 2010
Содержание
- 1 A Function That Returns a Value
- 2 Function Requiring Two Arguments
- 3 Function return more than one value
- 4 Functions that return true or false
- 5 Math Function Library
- 6 Multiple return statements in a function
- 7 Passing Arguments and Returning Values by Reference
- 8 Returning a list an array from function
- 9 Returning an array from a function
- 10 Returning a Value by Reference
- 11 Returning a value from a function
- 12 Returning by Reference
- 13 Returning More Than One Value
- 14 Returning Values by Reference
- 15 return multiple values from a function
- 16 User-Defined Function to Determine a Leap Year
- 17 Using an array returned from a function
A Function That Returns a Value
<source lang="html4strict">
<html> <head> <title>A Function That Returns a Value</title> </head> <body>
<?php
function addNums( $firstnum, $secondnum ) { $result = $firstnum + $secondnum; return $result; } print addNums(3,5);
?>
</body> </html>
</source>
Function Requiring Two Arguments
<source lang="html4strict">
<html> <head> <title>Function Requiring Two Arguments</title> </head> <body> <?php function fontWrap( $txt, $size ) {
print "$txt";
}
fontWrap("call 1
",5);
fontWrap("call 2
",3);
fontWrap("call 3
",3);
fontWrap("call 4
",3);
?>
</body>
</html>
</source>
Function return more than one value
<source lang="html4strict">
<?php
function retrieve_user_profile() { $user[] = "A"; $user[] = "A@example.ru"; $user[] = "English"; return $user; } list($name,$email,$language) = retrieve_user_profile(); echo "Name: $name, email: $email, preferred language: $language";
?>
</source>
Functions that return true or false
<source lang="html4strict">
<? function can_pay_cash($cash_on_hand, $amount) {
if ($amount > $cash_on_hand) { return false; } else { return true; }
} function myFunction($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100); $tip_amount = $meal * ($tip / 100); $total_notip = $meal + $tax_amount; $total_tip = $meal + $tax_amount + $tip_amount; return array($total_notip, $total_tip);
} $total = myFunction(15.22,8.25,15); if (can_pay_cash(20, $total)) {
print "I can pay in cash.";
} else {
print "Time for the credit card.";
} ?>
</source>
Math Function Library
<source lang="html4strict">
<?php
function subtract($n1, $n2) { return $n1 - $n2; } function add($n1, $n2) { return $n1 + $n2; } function divide($n1, $n2) { if($n2 == 0) return -1; else return $n1 / $n2; } function multiply($n1, $n2) { return $n1 * $n2; } function to_pow($n1, $pow) { if($pow) return $n1 * to_pow($n1, $pow - 1); return 1; } print(to_pow(6, 1) . "
"); print(multiply(5, 10) . "
"); print(divide(5, 10) . "
"); print(subtract(10, 50) . "
"); print(add(10, 5) . "
");
?> </body> </html>
</source>
Multiple return statements in a function
<source lang="html4strict">
<? function payment_method($cash_on_hand, $amount) {
if ($amount > $cash_on_hand) { return "credit card"; } else { return "cash"; }
} ?>
</source>
Passing Arguments and Returning Values by Reference
<source lang="html4strict">
<?php function add2(&$number) {
$number += 2; return $number;
} $mynum = 5; $output = add2($mynum); $output++;
echo "output = {$output}, mynum = {$mynum}
\n";function &initialize() {
$new = array_fill(0, 10, array_fill(0, 10, 0)); return $new;
} $newarray =& initialize(); echo print_r($newarray, true); ?>
</source>
Returning a list an array from function
<source lang="html4strict">
<? function small_numbers() {
return array (0, 1, 2);
} list ($zero, $one, $two) = small_numbers(); ?>
</source>
Returning an array from a function
<source lang="html4strict">
<? function myFunction2($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100); $tip_amount = $meal * ($tip / 100); $total_notip = $meal + $tax_amount; $total_tip = $meal + $tax_amount + $tip_amount; return array($total_notip, $total_tip);
} ?>
</source>
Returning a Value by Reference
<source lang="html4strict">
<?php
function &find_var($one, $two, $three) { if(($one > 0) && ($one <= 10)) return $one; if(($two > 0) && ($two <= 10)) return $two; if(($three > 0) && ($three <= 10)) return $three; } $c_one = "foo"; $c_two = 42; $c_three = 4; $right_var = &find_var($c_one, $c_two, $c_three); $right_var++; echo "The value of \$c_three and \$right_var are: "; echo "$c_three and $right_var
\n";
?>
</source>
Returning a value from a function
<source lang="html4strict">
<? $number_to_display = number_format(285266237); print "The population of the US is about: $number_to_display"; function myFunction($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100); $tip_amount = $meal * ($tip / 100); $total_amount = $meal + $tax_amount + $tip_amount; return $total_amount;
} ?>
</source>
Returning by Reference
<source lang="html4strict">
function &return_fish( ) {
$fish = "Wanda"; return $fish; } $fish_ref =& return_fish( ); </source>
Returning More Than One Value
<source lang="html4strict">
<?php function addandsubtract ($firstvalue, $secondvalue){
$firstreturnvalue = ($firstvalue + $secondvalue); $secondreturnvalue = ($firstvalue - $secondvalue); $myarray = array (); $myarray[0] = $firstreturnvalue; $myarray[1] = $secondreturnvalue; return $myarray;
}
$myarray = array ();
$myarray = addandsubtract (10, 3);
echo $myarray[0] . "
";
echo $myarray[1];
?>
</source>
Returning Values by Reference
<source lang="html4strict">
<?php class myclass {
private $thevalue; private $theword; public function __construct (){ $num_args = func_num_args(); if($num_args > 0){ $args = func_get_args(); $this->theword = $args[0]; } } public function setvalue ($newvalue){ $this->thevalue = $newvalue; } public function getvalue () { return $this->thevalue; } public function getword () { return $this->theword; }
} $myclass1 = new myclass ("A"); $myclass1->setvalue (1);
$myclass2 = new myclass ("B"); $myclass2->setvalue (2);
$myclass3 = new myclass ("C"); $myclass3->setvalue (3);
$myclass4 = new myclass ("D"); $myclass4->setvalue (4);
$classarr = array ($myclass1,$myclass2,$myclass3,$myclass4); function &findclass ($whichclass,$classarr){
for ($i = 0; $i < count ($classarr); $i++){ if ($classarr[$i]->getvalue() == $whichclass){ return $classarr[$i]; } }
} $myobject = new myclass (""); $myobject =& findclass (3,$classarr); echo $myobject->getword(); ?>
</source>
return multiple values from a function
<source lang="html4strict">
<?php
function retrieve_user_profile() { $user[] = "Jason"; $user[] = "jason@example.ru"; $user[] = "English"; return $user; } list($name,$email,$language) = retrieve_user_profile(); echo "Name: $name, email: $email, preferred language: $language";
?>
</source>
User-Defined Function to Determine a Leap Year
<source lang="html4strict">
<?php
function is_leapyear($year = 2004) { $is_leap = (!($year % 4) && (($year % 100) || !($year % 400))); return $is_leap; } $answer = is_leapyear(2000); if($answer) { echo "2000 is a leap year
"; } else { echo "2000 is not a leap year.
"; } $answer = is_leapyear(); if($answer) { echo "2003 is a leap year.
"; } else { echo "2003 is not a leap year.
"; }
?>
</source>
Using an array returned from a function
<source lang="html4strict">
<? function myFunction2($meal, $tax, $tip) {
$tax_amount = $meal * ($tax / 100); $tip_amount = $meal * ($tip / 100); $total_notip = $meal + $tax_amount; $total_tip = $meal + $tax_amount + $tip_amount; return array($total_notip, $total_tip);
} $totals = myFunction2(15.22, 8.25, 15); if ($totals[0] < 20) {
print "The total without tip is less than $20.";
} if ($totals[1] < 20) {
print "The total with tip is less than $20.";
} ?>
</source>