PHP series (ii) Declaration and application of PHP functions

Source: Internet
Author: User

(1) definition and application of functions

function name (parameter 1, parameter 2, parameter 3 ...) )

{

function Body

return value

}

<?php

Table (3,8, " Student score Table ", "Red", "green");

function table ($row, $col, $tablename, $bgcolor 1, $bgcolor 2)

{

echo "<table border=1 width=800>";

echo "<caption>

$i = 0;

$k = 0;

while ($i < $row) {

if ($i%2==0)

$BGC = $bgcolor 1;

Else

$BGC = $bgcolor 2;

echo "<tr bgcolor=". $bgc. "' > ";

$j = 0;

while ($j < $col)

{

echo "<td>". $k + +. " </td> ";

$j + +;

}

echo "</tr>";

$i + +;

}

echo "</table>";

}

Table (5,2, " Student information Sheet ", "Yellow", "bule");

Table (8,12, " Student table ", "#f00ff", "#ffffaa");

?>

Three tables displayed (Student score table, student information sheet, student table)

(2 ) function Local variables

function body end, variable release

<?php

function Fun1 ()

{

$a = 100;

echo $a;

}

function fun2 ()

{

$b = 200;

Echo $b;

}

Fun1 ();

Fun2 ();

?>

Show:

100

200

(3 ) Global variables of the function

<?php

$a = 200;

$b = 500;

function Fun1 ()

{

echo $GLOBALS ["a"];

}

function fun2 ()

{

Global $b;

Echo $b;

}

Fun1 ();

Fun2 ();

?>

Show:

200

500

(4 ) Static variables of the function

Static variables are used in the additive

<?php

function test ()

{

static $a = 0; Static Variables

echo $a. " <br> ";

$a + +;

}

Test ();

Test ();

Test ();

?>

Show:

0

1

2

function_exists (); determine if a function exists

<?php

function test ()

{

static $a = 0;

$a + +;

}

Test ();

if (function_exists ("Test1")) {

echo "Test1 is exists";

}else{

echo "Test1 is not exists";

}?>

display:test1 is exists

function parameter passing

<?php

function fun1 ($a =1, $b =2, $c =3, $d =4, $e =5)

{

echo $a. " <br> ";

echo $b. " <br> ";

echo $c. " <br> ";

echo $d. " <br> ";

echo $e. " <br> ";

}

FUN1 (A,B,C,D);

?>

display:a b c d 5

Func_get_args ();// gets an array of parameters in the method

<?php

function Fun ($a)

{

$args =func_get_args (); gets an array of parameters in the method

echo count ($args);

echo "<br>";

Echo $args [4];

}

Fun (1,2,3,5,688);

?>

shown as: 5 688

(5 ) variable function

<?php

function test ()

{

echo "#######<br>";

}

$a =test;

$a ();

? >// Variable function

Display:#######

(6 ) recursive function

<?php

function Demo ($a) {

echo "#####". $a. " <br> ";

if ($a >0)

Demo (--$a);

}

Demo (3);

? >// Recursive function

Shown as:

# # # #3

# # # #2

# # # #1

# # # #0

<?php

function Demo ($a) {

if ($a > 1) {

$r = $a *demo ($a-1);

}else {

$r = $a;

}

return $r;

}

Echo Demo (4);

?> Recursive invocation

4x3x2x1=24

function Fun ($a) {

echo$a. " <br> ";

if ($a >0) {

Fun ($a-1);

}else{

echo "-------<br>";

}

echo$a. " <br> ";

}

Fun (4);

Show:

4
3
2
1
0
-------
0
1
2
3
4

(7) callback function

function as a parameter pass

function Fncallback ($msg 1, $msg 2)

{

Echo ' MSG1: '. $msg 1;

echo "<br/>\n";

Echo ' MSG2: '. $msg 2;

}

$fnName = "Fncallback";

$params = Array (' Hello ', ' world ');

Call_user_func_array ($fnName, $params);

Shown as:

Msg1:hello

Msg2:world

$arr = Array (1,5,7);

Function mycon ($a, $b) {

If ($a > $b) {

RETURN1;

}elseif ($a < $b) {

Return-1;

}else{

Return 0;

}

}

Usort ($arr, "mycon");

Print_r ($arr);

(8) intrinsic Functions

<?php

function Demo ($php, $java, $oracle) {

function php ($php) {

if ($php >=60 && $php <=100) {

Return "PHP grade pass ";

}else{

Return "Failure of the PHP grade ";

}

function Java ($java) {

if ($java >=60 && $java <=100) {

Return "Java grade pass ";

}else{

Return "Failure of the Java grade ";

}

function Oracle ($oracle) {

if ($oracle >=60 && $oracle <=100) {

Return "Oracle pass ";

}Else

{

Return " failed Oracle grade ";

}

echo PHP ($php). " <br> ";

echo Java ($java). " <br> ";

Echooracle ($oracle). " <br> ";

$total = $php + $oracle + $java;

$anv = $total/3;

echo " your total is ". $total. " <br> ";

echo " Your average score is ". $anv. " <br> ";

}

Demo (10,80,70);

?>

Shown as:

PHP Fail to pass the grade
Java Grade pass
Oracle has passed the test
your overall grade is. 160
Your average score is 53.3333333333

(9) Loading the custom function library

Include () fast script execution is included, when processing fails, generates a warning,

Require () can cause a fatal error

Require () a file is imported during preprocessing, like pasting the file into a function where it is used

the two functions of the include_once require_once are included. Will only be included once and will not be repeated

(ten ) anonymous function

<?php
$greet = function ($name) {
printf ("Hello%s\r\n", $name);
};
$greet (' World ');
$greet (' PHP ');

(11)php引用参数的函数

只有在内存变量中才有地址、有引用关系的二个变量、一个变、另一个就变、

如果在函数说明中、有&出现、说明这个 参数是一个引用参数、调用时传参数时、就必须传一个变量

 

$b= 20;

functiondemo(&$a){

    $a=100;

}

echo$b."<br>";

demo($b);

echo$b."<br>";

显示为:

20

100


This article is from the "Linux Warden" blog, so be sure to keep this source http://sswqzx.blog.51cto.com/2494644/1963553

PHP series (ii) Declaration and application of PHP functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.