PHP Course Note 4

Source: Internet
Author: User

the function of PHP reference parameter in class

1. Only the variable in memory has an address.

2. There are two variables of the reference relationship, one change, and the other changes

3. If there is a & in the function, indicating that this parameter is a reference parameter, you must pass a variable when calling the simultaneous parameter.

function of PHP default parameters in class

1. The default parameters must be followed by default parameters and cannot have variables without default parameters.

The function of the variable number of PHP parameters in class

1. $arr = Func_get_args ();

This function can receive all variables. form an array.

2.func_num_args (); Number of output parameters '

3.

Function Demo () {

$sum = 0;

if ($i =0; $i <func_num_args (); $i + +) {

$sum + = Func_get_arg ($i);

}

return $sum;

}

Class of php variable function

1. Function add ($a, $b) {

return $a + $b;

}

$var = "Add";

echo $var (40,23);

2. If a function name is given to a variable (string), then the function is called after the variable is appended with parentheses.

Lesson PHP callback function

1. When using a function, it is necessary to pass a procedure into the function if a variable does not solve the problem. Change the execution behavior of the function.

2. In a function call, a function is passed in a parameter that is not a variable or a value, which is the callback function.

Class 1 PHP production callback function

<?php

function Demo ($n, $num) {
$num = "Aida";
for ($i =0; $i < $n; $i + +) {
if ($num ($i)) {
Continue
}
echo $i. " <br> ";
}
}
function Aida ($i) {
if ($i >10 && $i <20) {
return true;
}

}

Echo Demo ("Aida");

?>

Using the callback function, the variable function is used.

Lesson 2 PHP production callback function

/*
function Demo ($num, $n) {
if ($n ($i))
for ($i =0; $i < $num; $i + +) {
if (Call_user_func_array ($n, Array ($i))) {
Continue
}
echo $i. " <br> ";
}
}
function test ($i) {
if ($i = = Strrev ($i)) {
return true;
}
else{
return false;
}
}
Demo ("Test");

*/

class 3 PHP production callback function

function Demo ($num, $n) {
if ($n ($i))
for ($i =0; $i < $num; $i + +) {
if (Call_user_func_array ($n, Array ($i))) {
Continue
}
echo $i. " <br> ";
}
}

Class filter{
function One ($i) {
if ($i%3==0) {
return true;
}
}
static function ($i) {
if (Preg_match ('/3/', $i)) {
return true;
}
}
}

function test ($i) {
if ($i = = Strrev ($i)) {
return true;
}
else{
return false;
}

}
Demo (500,array (New Filter (), "one"));
Demo (500,array ("Filter", "both");

Lesson PHP system functions

$dirname = ' D:\Music ';

function Dirfor ($dirname) {
$dir = Opendir ($dirname);

Readdir ($dir);
Readdir ($dir);
while ($file = Readdir ($dir)) {
$file = $dirname. " \ \ ". $file;
if (Is_dir ($file)) {
echo "directory:". $file. " <br> ";
Dirfor ($file);
}else{
echo "File:". $file. " <br> ";
}

}
Closedir ($dir);
}
Dirfor ($dirname);

php recursive function in class

A function call itself is recursive.

Class , PHP load Custom Library of functions

1.

Include ();

Require ();

Both require and include are functions that invoke the calling page, but require is the file that the program executes, and a fatal error is reported if the discovery does not exist. The include is only the file that the program executes, and if it does not exist, it will only warn that the file does not exist.

Add: Require has no return value, and include does not, so require executes faster than include.

Avoid including the same file at once, using require_once () or include_once (). But do not use the require_once () when you can use the require, because the efficiency is high.

2.include contains files while executing non-PHP files.

If it is a non-PHP file, it is output directly.

Lesson 94 new features of PHP5.3 anonymous function

1. Anonymous functions

$var = function () {

}; Be sure to add a semicolon to the end

Also called a closure function.

Class The concept of PHP closure function (closures)

1. Child functions can use local variables of the parent function, which is called closures.

Function Demo () {

$a = 10;

$var = function ($n) use ($a) {

echo $n. " <br> ";

echo $a;

};

$var (5);

}

Demo ();

Class The characteristics of the PHP closure function (closures)

When the closure function returns, the function internal variable is active, and the function sits in the stack area remains.

Lesson 98 An overview of PHP arrays

Class a basic concept and classification of PHP arrays

1. The contents of the array are called "elements".

2. Key-value pairs

3. Indexed arrays, associative arrays

class -php Array declaration features

1.[] to manipulate the subscript, you can also use {}. However, it is recommended to use [].

2. [] In the array is not considered to be a special symbol in the string.

3. Subscript 02 will not be a valid decimal number, so it will not be cast.

4. The subscript true will convert to 1,false will be converted to 0

5. The subscript is null or empty.

Lesson 101 PHP indexes and associative arrays and the details to note

1. The same content of subscript will be overwritten.

2. The automatic subscript of an array starts from 0 and then grows, only the maximum value +1 that has occurred.

3. String subscripts for associative arrays do not affect the arrangement rules for index subscripts.

4. Negative numbers can also be used as subscripts.

Lesson 102 php using array () to declare arrays

1. If the function returns an array, you can add [] and subscript directly after the function. For example demo () [2];

2. Array declarations can also be used $arr = [' 2 ', ' 3 ', ' 5 '];

Lesson 103 php Delete the values in the array application example (Monkey selected king)

1. Change the value to NULL in the array, and the subscript will be in, but the subscript will disappear with unset (). Subscripts are not re-indexed and need to be re-indexed using the Array_values () function.

2.

<?php

function Xdw ($m, $n) {
$arr = Array ();

$a = "a";

for ($i =0; $i < $m; $i + +) {
$arr [] = $a + +;
}

$i = 0;
while (count ($arr) >1) {
if (($i + 1)% $n = = 0) {
Unset ($arr [$i]);
}else{
$arr [] = $arr [$i];

Unset ($arr [$i]);
Print_r ($arr);
}
$i + +;

}
return $arr;

}

Print_r (XDW (4,3));

?>

Lesson 104 Declaration and application of PHP two-dimensional array

Lessons PHP multidimensional Arrays

1. If the two-dimensional array is empty, it will become an outer layer, with the inner layers being 0;

$array [][]= "23";

$array [][]=44;

will only be in the outer layer from 0 to 1, the inner layers are 0.

Lesson 106 using a for loop to iterate through an array

1. High efficiency is the way the array is accessed.

2.count ($arr) gets the array length. Is the number of actual elements of the array.

The 3.for cycle has a condition that the subscript cannot be broken. Must be continuous.

Lesson 107 using a foreach statement to iterate through an array

1.foreach (array as custom variable) {

......

}

The number of times the 2.foreach loop depends on the number of arrays

3.foreach will assign the elements in the array to a custom variable at a time in each loop. This variable is used in each loop, that is, the element in the current array.

Lesson 108 using the foreach statement to iterate through an array of applications

PHP Course Note 4

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.