PHP/Functions/Parameters
Содержание
- 1 Accessing Function Parameters
- 2 A function that takes a reference for an argument
- 3 A Function with an Optional Argument
- 4 All optional arguments
- 5 $header
- 6 $header
- 6.1 Creating a capitalize function with a default parameter $each
- 6.2 Creating Functions That Take a Variable Number of Arguments
- 6.3 Declaring a Function That Requires Arguments
- 6.4 Declaring a Function with Arguments
- 6.5 Default Parameters
- 6.6 Define whether a variable is passed by value or reference
- 6.7 Defining and Calling a two-argument function,
- 6.8 Function Arguments
- 6.9 Functions and References
- 6.10 Function with an Optional Argument
- 6.11 Function with default parameters
- 6.12 Function with two parameters
- 6.13 Making arguments be passed by reference
- 6.14 Passing an Argument to a Function by Value
- 6.15 Passing By Reference
- 6.16 Passing Variables as a Reference to the Argument of a Function
- 6.17 Using a Function Call to Pass an Argument to a Function by Reference
- 6.18 Using Optional Parameters
Accessing Function Parameters
<source lang="html4strict">
<?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"; }
?>
</source>
A function that takes a reference for an argument
<source lang="html4strict">
<?php
$my_int = 10; print("Before change_value(), \$my_int =" . $my_int . "
"); function change_value(&$var) { $var = 15; } change_value($my_int); print("After change_value(), \$my_int = " . $my_int . "
");
?>
</source>
A Function with an Optional Argument
<source lang="html4strict">
<html> <body> <?php
function headingWrap( $txt, $size=3 ) { print "<h$size>$txt</h$size>"; } headingWrap("Book title", 1);
?> </body> </html>
</source>
All optional arguments
<source lang="html4strict">
<? function page_header7($color = "336699", $title = "the page", $header = "Welcome") {
print "<html><head><title>Welcome to " . $title . "</title></head>"; print "<body bgcolor="#" . $color . "">";print "
$header
";} // 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 ?>
</source>
Capturing a return value
<source lang="html4strict">
<? function countdown($top) {
while ($top > 0) { print "$top.."; $top--; } print "boom!\n";
} $counter = 5; countdown($counter); print "Now, counter is $counter"; ?>
</source>
Changing argument values
<source lang="html4strict">
<? // 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 "
$header
";} // 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 ?>
</source>
Creating a capitalize function with a default parameter $each
<source lang="html4strict">
<?php function capitalize( $str, $each=TRUE ) {
$str = strtolower($str); if ($each === TRUE) { $str = ucwords ($str); } else { $str = strtoupper($str); } echo ("$str
");
} capitalize("hEllo WoRld!"); capitalize("hEllo WoRld!",FALSE); ?>
</source>
Creating Functions That Take a Variable Number of Arguments
<source lang="html4strict">
<?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) . "
";
?>
</source>
Declaring a Function That Requires Arguments
<source lang="html4strict">
<html> <head> <title>Declaring a Function That Requires Arguments</title> </head> <body> <?php function printBR( $txt ) {
print ("$txt
\n");
} printBR("This is a line"); printBR("This is a new line"); printBR("This is yet another line"); ?> </body> </html>
</source>
Declaring a Function with Arguments
<source lang="html4strict">
<?php function printcheck ($sometext){ print ("$sometext \n"); } printcheck ("A."); printcheck ("AA."); printcheck ("AAA!"); ?> </source>
Default Parameters
<source lang="html4strict">
function doHello($Name = "Paul") {
return "Hello $Name!\n"; } doHello( ); doHello("Paul"); doHello("Andrew"); </source>
Define whether a variable is passed by value or reference
<source lang="html4strict">
<?php function f1($a) {
$a += 4;
} function f2(&$a) {
$a += 10;
} $b = 5; f1(&$b); f2($b); echo "\$b = $b\n"; ?>
</source>
Defining and Calling a two-argument function,
<source lang="html4strict">
<? 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");
</source>
Function Arguments
<source lang="html4strict">
<?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>
<?php addup(8, 16, 24); ?>
<?php addup(8, 16); ?>
</body>
</html>
</source>
Functions and References
<source lang="html4strict">
<?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
$val2
");
?>
</source>
Function with an Optional Argument
<source lang="html4strict">
<html> <head> <title>Function with an Optional Argument</title> </head> <body> <?php function fontWrap( $txt, $size=3 ){
print "$txt";
}
fontWrap("call 1
",5);
fontWrap("call 2
");
fontWrap("call 3
");
fontWrap("call 4
");
?>
</body>
</html>
</source>
Function with default parameters
<source lang="html4strict"> <HTML> <HEAD> <TITLE> Printing text on a Web Page</TITLE> </HEAD> <BODY> <?php function textonweb ($content, $fontsize=3){ echo "$content"; } textonweb ("A
", 7); textonweb ("AA.
"); textonweb ("AAA.
"); textonweb ("AAAA!
"); ?> </BODY> </HTML> </source>
Function with two parameters
<source lang="html4strict">
<? function addNums( $num1, $num2 ) {
$result = $num1 + $num2;$ret = "
number 1: | $num1 |
number 2: | $num2 |
result: | $result |
return $ret;
} print addNums (49, 60); ?>
</source>
Making arguments be passed by reference
<source lang="html4strict">
<? 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; ?>
</source>
Passing an Argument to a Function by Value
<source lang="html4strict">
<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>
</source>
Passing By Reference
<source lang="html4strict">
function square1($number) {
return $number * $number; } $val = square1($val); function square2(&$number) { $number = $number * $number; } square2($val); </source>
Passing Variables as a Reference to the Argument of a Function
<source lang="html4strict">
<?php
function somefunct ($somearg){ $somearg += 3; } $othernum = 12; somefunct ($othernum); echo "$othernum","\n"; somefunct (&$othernum); echo "$othernum","\n";
?>
</source>
Using a Function Call to Pass an Argument to a Function by Reference
<source lang="html4strict">
<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>
</source>
Using Optional Parameters
<source lang="html4strict">
<?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>"; ?>
</source>