PHP reading notes (7)-Functions

Source: Internet
Author: User
Tags file handling

Custom functions

  PHP has more than 1000 functions built into it, so the function makes PHP a very powerful language. Most of the time we use the built-in functions of the system to meet the requirements, but the custom function is more clear about the structure and logic of the code by encapsulating a set of code to make it reusable.

How PHP functions are defined:
1. Start with the keyword " function "
2. The function name can begin with a letter or an underscore:function name()
3. Writing the function body in curly braces

1 <? PHP 2     function Say () 3     {4         echo ' Hello World '; 5     }6// call function here 7 say ();
Parameters of the function

  PHP functions can have no parameters, there can be several parameters, multiple parameters are called parameter lists, separated by commas, the parameters are similar to a variable, called to pass data into the function body. By passing parameters, we can make the function implement the operation of the parameters and get the result we want.

The variable name of the parameter can be freely specified, but it is best to be able to express the relevant meaning, commonly used to set the parameters of the method:

1 <? PHP 2     function sum ($a$b) {3     echo$a$b ; 4     }5// call the function here to calculate the value of 1+2 6     sum; 7 ?>
return value

 Use the return keyword to return a value to a function that returns any type that includes an array and an object, or NULL if the return is omitted.

1 function Add ($a) {2     return$a+1; 3 }4$b = Add (1);

  The return statement immediately aborts the function and returns control back to the line of code that called the function, so the return value of the following function is the same as the function above.

  function  Add ( $a  ) { 2  return   $a  +1; 3   $a  = 10 4  return   $a  +20 5   6   $b  = Add (1 7   

  A function cannot return multiple values, but you can get a similar effect by returning an array.

1 function numbers () {2     return Array (1, 2, 3); 3 }4list ($one$two$three) = numbers (); 5
Variable functions

  The so-called mutable function, that is, the function is called by the value of the variable, because the value of the variable is variable, so you can call different functions by changing the value of a variable. Often used in callback functions, function lists, or depending on dynamic parameters to invoke different functions. The variable function is called by the variable name in parentheses.

 1function  name () {2     echo ' Jobs '; 3     }4     $func = ' name '; 5     $func // calling a mutable function

  Mutable functions can also be used on the object's method calls.

1<?PHP2 classBook {3     functionGetName () {4         return' BookName ';5     }6 }7     $func= ' GetName ';8     $book=NewBook ();9     $book-$func();Ten?>
Built-in functions

Built-in functions refer to PHP's default supported functions, and PHP contains a number of standard common processing functions, including string processing, array functions, file handling, session and cookie processing.

We first take the string processing function as an example, through the built-in function str_replace can implement the substitution of strings. The following example replaces "Jobs" with "Steven Jobs":

 1$str = ' I am Jobs '. ; 2 $str Str_replace $str ); 3 Echo $str // The result is "I am Steven Jobs"

 Other functions are supported by other extensions, such as MySQL database processing functions, GD image processing functions, mail processing functions, PHP default loading some of the common extension libraries, we can install or load other extension libraries to increase the PHP processing function.

Determine if a function exists

  When we create custom functions and understand the use of mutable functions, in order to ensure that the functions called by the program are present, we often use function_exists to determine whether a function exists. The same method_exists can be used to detect whether a class's methods exist.

1 function func () {2} 3 if (function_exists(' func ')) {4     Echo ' Exists '; 5 }

class defines whether the class_exists can be used.

1 class myclass{2} 3 // check if class exists before use 4 if (class_exists(' MyClass ')) {5     $myclass New MyClass (); 6 }

There are many such checks in PHP, such as whether a file exists file_exists, and so on.

1 $filename = ' test.txt '; 2 if (! file_exists ($filename)) {3     echo$filename . ' Not exists. ' ; 4 }
previous section: PHP reading notes (6)-Arrays

PHP reading notes (7)-Functions

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.