PHP/Functions/Return Value

Материал из Web эксперт
Версия от 07:04, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A Function That Returns a Value

 
<html>
<head>
<title>A Function That Returns a Value</title>
</head>
<body>
<div>
<?php
    function addNums( $firstnum, $secondnum ) {
      $result = $firstnum + $secondnum;
      return $result;
    }
    print addNums(3,5);
?>
</div>
</body>
</html>



Function Requiring Two Arguments

<html>
<head>
<title>Function Requiring Two Arguments</title>
</head>
<body>
<?php
function fontWrap( $txt, $size ) {
    print "<font size=\"$size\">$txt</font>";  
}
fontWrap("call 1<br>",5);
fontWrap("call 2<br>",3);
fontWrap("call 3<BR>",3);
fontWrap("call 4<BR>",3);
?>
</body>
</html>



Function return more than one value

<?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";
?>



Functions that return true or false

 
<?
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.";
}
?>



Math Function Library

<?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) . "<br />");
     print(multiply(5, 10) . "<br />");
     print(divide(5, 10) . "<br />");
     print(subtract(10, 50) . "<br />");
     print(add(10, 5) . "<br />");
?>
</body>
</html>



Multiple return statements in a function

 
<?
function payment_method($cash_on_hand, $amount) {
    if ($amount > $cash_on_hand) {
        return "credit card";
    } else {
        return "cash";
    }
}
?>



Passing Arguments and Returning Values by Reference

 
<?php
function add2(&$number) {
    $number += 2;
    return $number;
}
$mynum = 5;
$output = add2($mynum);
$output++;
echo "<p>output = {$output}, mynum = {$mynum}</p>\n";
function &initialize() {
    $new = array_fill(0, 10, array_fill(0, 10, 0));
    return $new;
}
$newarray =& initialize();
echo print_r($newarray, true);
?>



Returning a list an array from function

 
<?
function small_numbers() {
    return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
?>



Returning an array from a function

 
<?
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);
}
?>



Returning a Value by Reference

 
<?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<BR>\n";
?>



Returning a value from a function

 
<?
$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;
}
?>



Returning by Reference

 
function &return_fish( ) {
            $fish = "Wanda";
            return $fish;
    }
    $fish_ref =& return_fish( );



Returning More Than One Value

 
<?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] . "<br />";  
echo $myarray[1];  
?>



Returning Values by Reference

 
<?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(); 
?>



return multiple values from a function

 
<?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";
?>



User-Defined Function to Determine a Leap Year

 
<?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<BR>";
    } else {
        echo "2000 is not a leap year.<BR>";
    }
    $answer = is_leapyear(); 
    if($answer) {
        echo "2003 is a leap year.<BR>";
    } else {
        echo "2003 is not a leap year.<BR>";
    }
?>



Using an array returned from a function

 
<?
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.";
}
?>