PHP/Functions/Parameters

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing Function Parameters

 
<?php
  function validatelogin ($username, $password){
    $actualuser = "myusername";
    $actualpass = "mypassword";
    
    if (strcmp ($username, $actualuser) == 0 && strcmp ($password, $actualpass) == 0){
      return true;
    } else {
      return false;
    }
  }
  
  if (validatelogin ("myusername","mypassword")){
    echo "You are logged in correctly";
  } else {
    echo "You have an incorrect username and/or password";
  }
?>



A function that takes a reference for an argument

<?php
     $my_int = 10;
     print("Before change_value(), \$my_int =" . $my_int . "<br />");
   
     function change_value(&$var) { 
        $var = 15; 
     }
   
     change_value($my_int);
     print("After change_value(), \$my_int = " . $my_int . "<br />");
?>



A Function with an Optional Argument

 
<html>
<body>
<?php
    function headingWrap( $txt, $size=3 ) {
      print "<h$size>$txt</h$size>";
    }
    headingWrap("Book title", 1);
?>
</body>
</html>



All optional arguments

 
<?
function page_header7($color = "336699", $title = "the page", $header = "Welcome") {
    print "<html><head><title>Welcome to " . $title . "</title></head>";
    print "<body bgcolor="#" . $color . "">";
    print "<h1>$header</h1>";
}
// Acceptable ways to call this function:
page_header7(); // uses all defaults
page_header7("66cc99"); // uses default $title and $header
page_header7("66cc99","my wonderful page"); // uses default $header
page_header7("66cc99","my wonderful page","This page is great!"); // no defaults
?>



Capturing a return value

 
<?
function countdown($top) {
    while ($top > 0) {
        print "$top..";
        $top--;
    }
    print "boom!\n";
}
$counter = 5;
countdown($counter);
print "Now, counter is $counter";
?>



Changing argument values

 
<?
// One optional argument: it must be last
function page_header5($color, $title, $header = "Welcome") {
    print "<html><head><title>Welcome to " . $title . "</title></head>";
    print "<body bgcolor="#" . $color . "">";
    print "<h1>$header</h1>";
}
// Acceptable ways to call this function:
page_header5("66cc99","my wonderful page"); // uses default $header
page_header5("66cc99","my wonderful page","This page is great!"); // no defaults
?>



Creating a capitalize function with a default parameter $each

 
<?php
function capitalize( $str, $each=TRUE ) {
  $str = strtolower($str);
  if ($each === TRUE) {
     $str = ucwords ($str);
  } else {
     $str = strtoupper($str);
  }
  echo ("$str <br />");
}
capitalize("hEllo WoRld!");
capitalize("hEllo WoRld!",FALSE);
?>



Creating Functions That Take a Variable Number of Arguments

 
<?php
  function addanything (){
    $total = 0;
    $args = func_get_args ();
    for ($i = 0; $i < count ($args); $i++){
      if (is_int ($args[$i])){
        $total += $args[$i];
      }
    }
    return $total;
  }
  echo addanything (1,5,7,8,11) . "<br />"; 
?>



Declaring a Function That Requires Arguments

<html>
<head>
<title>Declaring a Function That Requires Arguments</title>
</head>
<body>
<?php
function printBR( $txt ) {
     print ("$txt<br>\n");
}
printBR("This is a line");
printBR("This is a new line");
printBR("This is yet another line");
?>
</body>
</html>



Declaring a Function with Arguments

  <?php
  function printcheck ($sometext){
      print ("$sometext \n");
  }
  printcheck ("A.");
  printcheck ("AA.");
  printcheck ("AAA!");
  ?>



Default Parameters

 
function doHello($Name = "Paul") {
            return "Hello $Name!\n";
    }
    doHello( );
    doHello("Paul");
    doHello("Andrew");



Define whether a variable is passed by value or reference

 
<?php
function f1($a) {
  $a += 4;
}
function f2(&$a) {
  $a += 10;
}
$b = 5;
f1(&$b);
f2($b);
echo "\$b = $b\n";
?>



Defining and Calling a two-argument function,

 
<?
function page_header4($color, $title) {
    print "<html><head><title>Welcome to " . $title . "</title></head>";
    print "<body bgcolor="#" . $color . "">";
}
?>
//Multiple optional arguments
page_header4("66cc66","my homepage");



Function Arguments

 
<?php 
  function addup( $a = 32, $b = 32, $c = 32)
  {
    $total = $a + $b + $c;
    echo("$a + $b + $c = $total");
  }
?>
 
<html>
 <head>
  <title>Function Arguments</title>
 </head>
 <body>
  <h3> <?php addup(8, 16, 24); ?> </h3>
  <h3> <?php addup(8, 16); ?> </h3>
 </body>
</html>



Functions and References

<?php
     $val1 = "";
     $val2 = "";
   
     function return_multi_value(&$value1, &$value2)
     {
          $value1 = "This is the first value";
          $value2 = "This is the second value";
     }
   
     return_multi_value($val1, $val2);
     print("$val1<br />$val2<br />");
?>



Function with an Optional Argument

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



Function with default parameters

  <HTML>
  <HEAD>
  <TITLE> Printing text on a Web Page</TITLE>
  </HEAD>
  <BODY>
  <?php
  function textonweb ($content, $fontsize=3){
     echo "<FONT SIZE=$fontsize>$content</FONT>";
  }
  textonweb ("A <BR>", 7);
  textonweb ("AA.<BR>");
  textonweb ("AAA. <BR>");
  textonweb ("AAAA! <BR>");
  ?>
  </BODY>
  </HTML>



Function with two parameters

 
<?
function addNums( $num1, $num2 ) {
    $result = $num1 + $num2;
    $ret = "<table border=\"1\">";
    $ret .= "<tr><td>number 1: </td><td>$num1 </td></tr>";
    $ret .= "<tr><td>number 2: </td><td>$num2 </td></tr>";
    $ret .= "<tr><td>result: </td><td>$result</td></tr>";
    $ret .= "</table>";
    return $ret;
}
print addNums (49, 60);
?>



Making arguments be passed by reference

 
<?
function add_some_extra(&$string) {
    $string .= "and something extra.";
}
$str = "This is a string, ";
add_some_extra($str);
echo $str; 
function foo ($bar) {
$bar .= " and something extra.";
}
$str = "This is a string, ";
foo ($str);
echo $str; 
foo (&$str);
echo $str; 
?>



Passing an Argument to a Function by Value

<html>
<head>
<title>Passing an Argument to a Function by Value</title>
</head>
<body>
<?php
function addFive( $num ) {
     $num += 5;
}
$orignum = 20;
addFive( $orignum );
print( $orignum );
?>
</body>
</html>



Passing By Reference

 
function square1($number) {
            return $number * $number;
    }
    $val = square1($val);
    function square2(&$number) {
            $number = $number * $number;
    }
    square2($val);



Passing Variables as a Reference to the Argument of a Function

<?php
  function somefunct ($somearg){
    $somearg += 3;
  }
  $othernum = 12;
  somefunct ($othernum);
  echo "$othernum","\n";
  
  somefunct (&$othernum);
  echo "$othernum","\n";  
?>



Using a Function Call to Pass an Argument to a Function by Reference

<html>
<head>
<title>Using a Function Call to Pass an Argument to a Function by Reference</title>
</head>
<body>
<?php
function addFive( $num ){
     $num += 5;
}
$orignum = 20;
addFive( &$orignum );
print( $orignum );
?>
</body>
</html>



Using Optional Parameters

 
<?php
$access = array("a", "c", "h", "r", "r");
$admin = array("e", "q");
function check_access($username, $adminonly = false) {
    global $access, $admin;
    if (in_array($username, $admin)) {
        return true;
    }
    if (!($adminonly) && in_array($username, $access)) {
        return true;
    }
    return false;
}
echo check_access("r") ? "is" : "is NOT" ," allowed.</p>";
echo check_access("q") ? "is" : "is NOT" ," allowed.</p>";
echo check_access("r", true) ? "is" : "is NOT" ," an admin.</p>";
echo check_access("q", true) ? "is" : "is NOT" ," an admin.</p>";
?>