PHP learning notes-user-defined functions

Source: Internet
Author: User
PHP learning notes-custom function definition function format: functionfunctionName (){???? Function content} & lt ;? Php ?? Functionmyfunction (){?????? Echo 'My first php function ';?? PHP learning notes-custom functions

Define the function format: function functionName (){

???? Function content

}

?? Function myfunction (){

?????? Echo 'My first php function ';

?? }

?>

Like other language functions, php functions can have parameters and return values, and parameters can have default values.

Function that returns multiple values: you can use the list () function to construct an array!

Function call that contains reference and passing parameters: you can modify the parameters in the function beyond the function range to respond to reference and passing parameters.

?? $ Name = 'guxia ';

?? Function functionName (& $ name ){

????? $ Name = 'wustrive _ 200 ';

? }

? FunctionName ($ name );

? Echo? $ Name;


?>

Note: In php, numbers are case-insensitive, but variable names are case-sensitive.

The scope of a variable can control where the variable is visible and available. Different programming languages have different variables
Use Domain rules. PHP has simple rules:
Variables declared in a function start from the statement that declares them to the end of the function. This is called a function
Domain. These variables become local variables.
The variable scope declared outside the function starts from the statement that declares them to the end of the file, rather than within the function.
. This is called global scope. These variables become global variables.
Special Super global variables are visible inside and outside the function.
Using require () and include () does not affect the scope. If these two statements are used inside the function, the function scope
Applicable. If it is not within the function, the global scope applies.
The keyword "global" can be used to manually specify that a variable defined or used in a function has a global scope.
You can manually delete a variable by calling unset ($ variable_name. If a variable is deleted, it is not included in the parameter
The specified scope is in progress.

??? $ A = 5;
??? Function fna (){
??? ??? Global $;
??? ??? $ A ++;
??? }
??? Fna ();
??? Echo $ ;???
?>



??? $ GLOBALS ['A'] = 10;
??? Function fna (){
??? ??? $ GLOBALS ['A'] ++;
??? }
??? Fna ();
??? Echo $ GLOBALS ['A'];

?>


Create your own function library
Generally, you can store the function set file in the library folder and call the file. File name conventions
You can name it toolname. library. php, and the tool can be set as needed.

When calling an external file, use the include (), include_once (), require (), or require_once () statements to reference the file.

Include_once () and require_once () only contain files once!

The difference between include () and require (): If the file contained in include () does not exist, run the following statement. if the file contained in require () does not exist, the program will be terminated!

Several magic constants in php (constants whose values change with the environment ):

_ FILE _? : Current file name

_ LINE _: current row number

_ FUNCTION _: current FUNCTION name

_ CLASS _: current CLASS name

_ METHOD _: current METHOD name


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.