PHP Class 4: common functions and php Functions

Source: Internet
Author: User

PHP Class 4: common functions and php Functions

Learning Overview:


I. Language Structure
Ii. User-Defined Functions
Iii. variable scope
Iv. Static variables
5. function return value
Vi. Parameters
7. Default Parameters
8. Reference parameters
9. Variable Number Function
10. Callback Function
11. Variable Functions
12. recursive functions
13. File Inclusion



Function category
1. User-Defined Functions
2. Callback Function
3. Variable Functions
4. recursive functions

Language Structure:
If ()
For ()
While ()
Switch ()


Echo ()
Print ()

Array ()
List ()
Foreach ()

Isset ()
Unset ()
Empty ()

Exit ()
Die ()

Include ()
Require ()

Custom functions:
Function fun (){}

Call a function
Fun ()
<? Php

Function jiutb ($ num, $ color ){
For ($ I = 1; $ I <= $ num; $ I ++ ){
For ($ j = 1; $ j <= $ I; $ j ++ ){
Echo "<font color = '{$ color}'>". "$ I * $ j =". ($ I * $ j). "</font> ";
}
Echo "<br> ";
}
Echo "}

Jiutb (9, "# cccccc ");
Jiutb (10, "red ");
?>

Default parameters:
Function jiutb ($ num = 9, $ color = "black "){}

Variable parameters

<? Php

Function fun (){

Echo func_num_args (); // obtain the number of parameters.
Echo array_sum (func_get_args (); // obtain the sum of the Parameters
}

Fun (2, 3, 4, 5 );
?>


The sum of Infinitely multiple parameters:
<? Php

Function fun (){

// Echo func_num_args (); // obtain the number of parameters.
// Echo array_sum (func_get_args (); // obtain the sum of the Parameters

$ Tot = func_num_args ();
$ Arr = func_get_args ();


For ($ I = 0; $ I <$ tot; $ I ++ ){
$ Sum + = $ arr [$ I];
}
Echo $ sum;
}

Fun (2, 3, 4, 5 );
?>


Variable Functions
Function fun (){}
$ A = "fun ";
$ A (2, 3, 4, 5 );

Variable scope:
1. Global Variables
Variable outside the Function
2. Local Variables
Variable in the function
After the glocal keyword is modified, you can change the local variable to a global variable.
<? Php

$ Num = 10;

Function fun (){
Global $ num; // you can modify global variables or use them the same as those of external variables.
$ Num = 20;
}

Fun ();

Echo $ num;
?>


Reference parameters:
1. The function points internally and externally to the same address.
<? Php

$ Num = 10;

Function fun (& $ I) {// & $ I indicates that the above address is uploaded. You need to change it together.
$ I ++;
}

Fun ($ num );

Echo $ num;
?>

Callback function:
Is a parameter.
The parameter of a function is the name of another function. This parameter is called a callback function.
<? Php

// Callback function


Function sum ($ I, $ j ){
Return $ I + $ j
}
Function fun ($ a, $ B, $ k)
Echo $ k ($ a, $ B );

Fun (1, 2, "sum ");
?>

Static variables
When you call the same function multiple times, you can keep track of static variables.
// When calling a program, count the number of calls of this function.
<? Php
$ Num = 0;
// Static variables
Function show (){
Global $ num;
$ Num ++;

Echo "this is the number of {$ num} calls <br> ";
}

Show ();
Show ();
?>
Static variable method:
<? Php


// Static variables
Function show (){
Static $ num;
$ Num ++;

Echo "this is the number of {$ num} calls <br> ";
}

Show ();
Show ();
Show ();
?>

File Inclusion:
1. include () contains errors and the script continues to be executed
2. If require () contains an error, the script will be terminated immediately.

Recursive functions:
Call this function in the function
1. sum:
// Recursive Function
$ Num = 100;
For ($ I = 1; $ I <= $ num; $ I ++ ){
$ Sum + = $ I;
}

Echo $ sum;
Function call Sequence
Show ();

Function show (){}

Show ();


6. php execution sequence
1) load the page
2) syntax detection (1. Syntax Detection 2. Loading functions)
3) execute the script
What do common php functions need to be memorized?

First, there are database functions, such as mysql_connect, mysql_create_db, mysql_fetch_array, mysql_select_db, and mysql_query. There are many database functions. You 'd better write them into a class. For details, see discuz and phpcms, the arrays of PDO php can be used very powerful. It is best to remember loop, division, and deletion of arrays in many places, such as is_array, implode, explode, and array_keys, array_merge and so on are some security functions such as addslashes and htmlspecialchars. Other Character Processing: subtr, strlen, trim, and Other beginners should try their best to use professional editors, such as UE, editplus is conducive to spelling and memory functions. Less IDE tools, such as zendstudio, are used. Although this is convenient, it is not conducive to memory functions. I became blind. Although I had some ideas, I couldn't write any code.
 
Common php Functions

It's not hard to master some frequently used functions. It should be remembered several times.
Check the manual if no problem occurs.
In fact, every PHP developer cannot remember all the functions, but the frequently used functions must be remembered. Otherwise, it would be too disappointing.
The manual is very important. If not, check it.

Okay, answer your questions one by one
Functions are often used.
Functions must be very important.
If you use a function frequently, it will accelerate function learning efficiency. For example, if you use a case to use all the functions that can be used, you will remember a lot of functions.
Common functions are string processing, and array processing.

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.