PHP Brothers Learn 60-90

Source: Internet
Author: User

Function
A function is a named block of code that completes a specified task. A function can follow a set of values or parameters given to him to accomplish a particular task, and may return a value.
Php?? Two kinds of functions: custom functions + system functions
A game: Walking fights plus points escape upgrade
An independent body, which is encapsulated into functions, process-talk development functions.
Function Benefits:
Design complexity of Control program
Improve the reliability of software
Improve the efficiency of software development
Improved maintainability of software
Increase the repeatability of the program

2. Custom Functions
A function is a struct, declaring one of the simplest functions.
function table () {
Echo ' <table border= ' 1 "width=" align= "center" > ";
Echo ' <caption>for ($i =0; $i <10; $i + +) {
Echo ' <tr> ';
for ($j =0; $j <10; $j + +) {
$BG = $j% 2 = = 0? "#999": ' #ff0000 ';
Echo ' <td bgcolor= '. $bg. ' > '. ($i * + $j + 1). ' </td> ';
}
echo "</tr>";
}
Echo ' </table> ';
}
Table ();

3. Parameters of the function
Allows the user to call the function can change the function inside the execution behavior, to meet the user's needs
When declaring a function, the supplied argument is called a row parameter (formal parameter)
The argument at the time of the call is the actual argument

Row and argument correspondence
Order
Type


4. function return value
No return value is a process, and the execution is done.
Custom Function Format:
Function name (parameter) {
function body
return value
}
The function name can be preceded by a letter underscore followed by 0 or more alphanumeric underscore digits and any string.
Function names are case insensitive. A named function does not use a function that has already been declared, or a function name built into PHP.
Exit (); no return value
return value.

Determine if a function exists
Function_exits ();
if (function_exists ("table")) {
}


Variable range of 5.php
Variables declared inside the function can only be called inside the function, local variables.
The parameter of the function is also a local variable that can be called when the value is passed.

6.php Global Variables
Variables declared outside the function are global variables that can be used in each function (order problems)
PHP declares variables without modifiers, so you can't determine whether it's a new declaration.
Use global $name = "Xiaowei";

In relation to the order
$name = "Xiaoewi";
Global $name;
Echo $name;

Declaration before the call

Constants can be used directly
Hyper-Global array of 9 arrays $_post $_get

7. Static variables
Memory divided into 4 segments
Stack memory-small space release relatively fast
Initializing static segments
static $c = 30;
1. Static variables declared in a function, declared only on the first call
2. The second time after the look is no longer declare direct use
3. Static variables are shared in multiple invocations of the same function
A general static variable is defined inside the function to save its current value ....

Define ("NAME", "Teacher Gao");

8. Referencing variable aliases
$a = 10;
$b = & $a;
Passing the address of A to B, changing B is the equivalent of changing a
Only variables in memory have an address
If there is a & in the function description, indicating that this parameter is a reference parameter, when you call the simultaneous parameter, you must pass a variable
Function demo (& $a) {
$a = 100;
}
$b = 30;
Demo ($b);
Echo $b; -100


The parameters of the function are reference parameters ... The pass must be a variable name and cannot be a value
void Funname (array &args)
A row parameter is used & decorated, and a variable must be passed to this parameter when the function is called.

9. Default parameters
Assigning direct values to a row parameter
Optional parameters
Required Parameters
Demo (string $name, int $age, [string $sex, [string $email]]

10. Variable Parameters
Number demo ([number ...]) ... means to pass a few.
Func_num_args
func_num_arg[$i]
Number of Count arrays

Weak type of language at all


10.php variable function
function add ($a, $b) {
return $a + $b;
}
$var = "Add";
$var ();
Variable function .....
Use variable name + parentheses


11. Callback function
When using a function, if passing a variable does not solve the problem, it is necessary to pass a procedure into the function and change the function execution behavior.
When a function is called, it is not a variable or a value in the argument, but a function, which is the callback function.
Usort (Arr,function ($a, $b) {return $a-$b;});

strlen ($a);

12. Callback Function 1
Variable function making callback function
function Demo ($num, $n) {
for ($i =0; $i < $num; $i + +) {
if ($n ($i)) {
Continue
}
echo $i. " <br/> ";
}
}
function test ($i) {
if (Preg_match ('/3/', $i)) {
return false;
}else{
return true;
}
}
Demo ("Test");

13. Callback function 2, using the system function Call_user_func_array to implement
The number of arguments is the time of change
function AA ($a =3, $b =3, $c =3) {
echo $a. $b. $c;
}
Call_user_func_array ("AA", array);

14. System functions
<?php
$dirname = "./phpmyadmin";
Open Directory Resource
$dir = Opendir ($dirname);
Readdir ($dir). " <br/> ";
Readdir ($dir). " <br/> ";
while ($file = Readdir ($dir)) {
$file = $dirname. "/" . $file;
if (Is_dir ($file)) {
echo $file. " <br/> ";
}
}
Shut down
Closedir ($dir);
?>
15. Recursive functions
1. Call yourself in the function
Be sure to write a condition for the Exit function

PHP Brothers Learn 60-90

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.