php is one of the most popular languages currently. As a master figure in the PHP training industry, Li Yanhui's course explanations are clear and comprehensive. His PHP tutorial video is a classic and widely circulated on the Internet. This course is his first season video, about the basic knowledge of php.
Course playback address: http://www.php.cn/course/392.html
The teacher’s teaching style:
The teacher’s lectures are vivid, witty, witty, and touching. A vivid metaphor is like the finishing touch, opening the door to wisdom for students; an appropriate humor brings a knowing smile to students, like drinking a glass of mellow wine, giving people aftertaste and nostalgia; a philosopher's aphorisms, cultural references Proverbs are interspersed from time to time in the narration, giving people thinking and warning.
The more difficult part in this video is the PHP custom function:
Generally speaking, redundant code is bad. Rewriting the code again and again is not only a waste of time, but also looks shoddy from a layout and structure perspective. Like all good programming languages, PHP uses many methods to alleviate the problem of redundant code. The most common and easiest to implement method is to use functions.
one. Standard functions
There are more than 1,000 standard functions in the standard PHP distribution package. These standard functions are built into the system and can be used directly without the need for users to create them themselves.
<? echo md5('123456'); //MD5函数对字符串进行加密处理 ?>
2. Custom functions
PHP built-in functions allow interacting with files, using databases, creating graphics, and connecting to other servers. However, in actual work, there are many times when things are needed that the creators of the language could not foresee.
Declaring a function allows us to use our own code like built-in functions. Simply call this function and provide it with the necessary parameters. This means that the same function can be called and reused multiple times throughout the script.
Create function
<? function functionName() { echo '这是一个无参无返回自定义函数'; } ?>
Call function
<? functionName(); ?>
Function naming
1. The function name cannot be the same as the existing function name name.
2. The function name can only contain letters, numbers and underscores.
3. The function name cannot start with a number.
Function call containing parameters and no return
<? function functionArea($radius) { area=radius * $radius * pi(); echo $area; } functionArea(10); ?>
Function call containing parameters and returning: Use the return() statement to report to the function caller Returns an arbitrary deterministic value, returning program control to the caller's scope.
<? function functionArea($radius) { return radius∗radius * pi(); } echo functionArea(10); ?>
Function call containing default parameters: You can specify a default value for the input parameter. If no other value is provided, the default value will be automatically assigned to the parameter.
<? function functionArea($radius=10) { return radius∗radius * pi(); } echo functionArea(); ?>
Function call that returns multiple values: It can be constructed by returning an array and then using the list() function.
<? function functionInfo(name,age,$job) { userInfo=array(name,age,job); //可以用追加的方式比较常用 return $userInfo; } list(name,age,$job) = functionInfo('吴祁',19,'学生'); echo 今年name.′今年′.age.'岁了,目前还是个'.$job; ?>
Function calls containing parameters passed by reference: Passed by reference, modifications to parameters within the function can also be reflected outside the scope of the function.
<? $prices = 50; $tax = 0.5; function functionPrices(&prices,tax) { prices=prices + (prices∗tax); tax=tax * 2; } functionPrices(prices,tax); echo $prices; echo '<br />'; echo $tax; ?>
Please note that function calls will not be case-sensitive, so calling functionname(), FunctionName(), or FUNCTIOINNAME() are all valid and will all return the same results. For convenience, all are in lowercase here.
It is important to note that function names and variable names are different. Variable names are case-sensitive, so $Name and $name are two different variables, but Name() and name() are the same function.
The above is the detailed content of Li Yanhui PHP video tutorial season 1 resource sharing. For more information, please follow other related articles on the PHP Chinese website!