Php Getting Started Guide for some function usage instructions

Source: Internet
Author: User
Tags php tutorial

A: Like naming variables, you cannot use built-in function names or numbers to name functions.
B: repeated calls.
C: supports static elements.
D: non-fixed parameters are supported.
The technician is recommended to standardize functions as follows:
A: create A category prefix for the function name, for example, str_xxx for The struct type, bool_xxxx for the Boolean type, APP_xxxx for the public function, and temp_xxx for the temporary function.
B: The first step of the function is to judge first. Although sometimes you know that a certain type of parameter will be passed in, as for standardization, first judgment and then processing are for program robustness and safety.
C: do not pollute original variables. If you have project experience and debug application experience, you will understand.
D: reference functions should be used as little as possible, occupying a large amount of memory and causing serious losses.
E: Do not write code in uppercase; do not think it is cool.
F: excessive generation of functions is a method of regression. You can think about whether the process is repetitive, whether it needs to be encapsulated, and whether it is wise to block the process as a function at will.
G: write your function comments.
The code is as follows:

<? Php Tutorial
* @ Author Yuans
* @ Copyright
* @ Package: common usage and features of the function.
# Notes for writing basic functions.
// To facilitate ide management and code prompting, we start with fun _ when naming all functions.
Function fun_cutstr ($ str, $ str_width = 0, $ str_pad = '...'){
// Every function has to consider some exceptions, such as incorrect function introduction, 0, or false.
// The characters after the truncated characters are expected to be returned from the outside, so even if this function does not work, the incoming values should be returned.
If (empty ($ str) === true | empty ($ str_width) === true)
Return $ str;
// Filter parameters
$ Str_width + = 0;
// Maintain a principle and try not to pollute the original parameters,
$ Return_str = mb_strcut ($ str, 0, $ str_width, 'utf-8 ');
// Strengthen the judgment. If return_str cannot have a value, many servers cannot execute it because it is a mb function.
If (empty ($ return_str) === false ){
Return $ return_str. $ str_pad;
} Else {
Return $ str;
Echo fun_cutstr ('aaaaaaaaaaaaaaaaaaaaaaaaaaa', 5); // out disply: "aaaaa ...";
# Because it is UTF-8 encoded, each Chinese character is 4 bytes. Here, "I am..." is returned ...";
Echo fun_cutstr ('I am a technician', 8 );
# Or we need to consider serious damage to the function, such as the following function
Echo fun_cutstr (false); // out: false
Echo fun_cutstr ('tbbbbbbbbbbbbs ', 'aaaaaaa'); // out: tbbbbbbbbbbbbs
Echo fun_cutstr ('', 'aaaaaaaaa'); // out: empty

Basic knowledge of PHP functions
Copy the code as follows:

<? Php
$ B = & fun_cutstr ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 5); // out disply: "aaaaa ...";
Fun_cutstr ('cccccccccccccccccccccccc', 5 );
Echo $ B;

The referenced function will not run properly in php 5.3, and 6.0 will eventually discard it. In theory, echo $ B will return ccccc...
$ B introduces the function address, so any changes to the function will be assigned to $ B.
Of course, these can be seldom used, so you don't have to worry too much, especially for new learners.
Static functions are as follows:
Copy the code as follows:

<? Php
* @ Author Yuans
* @ Copyright
* @ Package: common usage and features of the function.
# Notes for writing static functions.
Function fun_static (){
Static $ a = 1;
Echo $ a ++;
Fun_static ();
Fun_static ();
Fun_static ();

Static $ a = 1; it is executed only when the function is called for the first time, indicating that it is static. During the second execution, the $ a variable is used to retrieve the static value, instead of assigning $ a = 1. and so on, the values are constantly added. <BR>


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.