Php basic knowledge notes. Similarities and differences between include and require: similarities: other PHP files can be introduced. Difference: When a file import error occurs in include, only the warning is displayed, but the program will continue to execute include and require again and again:
Similarities: you can introduce other PHP files.
Difference: When a file import error occurs in include, only warnings are reported, but the program continues to run. When an error occurs in require, an error is reported and the program is terminated.
In general, include_once and require_once are used instead of include and require. The statement for introducing files should be placed at the beginning, which is similar to the C language.
Php variables are case-sensitive, but function names are case-insensitive. It cannot start with a number. For example, Abc and abc are the same. if such two functions exist in a file, an error is returned.
Analysis of local variables and global variables with the same name;
See the following code:
$ A = 45;
Function abc () {// define a function
$ A + = 10;
}
Abc (); // call a function
Echo '$ a ='. $;
What is the output result? The result is still 45. why? In a function, local variables have the same name as the external variable $ a, but not the same variable, but two completely unrelated variables. The variable $ a in the function is released after the function is executed. The output is still an external variable. if you want to use an external variable (global variable) in the function body, you must add a global declaration before the variable with the same name. In fact, in the compiler's opinion, when declared as global, the address of the variable with the same external name is actually assigned to this local variable. in this case, the internal variable and the external variable with the same name are the same address, so it naturally becomes the same variable. The parameter in the function body or the local variable that is not declared as global is used as a local variable, and the scope is in the function body. For example, the output result of the following code is 45, not 55.
$ A = 45;
Function abc ($ a) {// define a function
$ A + = 10;
}
Abc ($ a); // call a function
Echo '$ a ='. $;
There are three levels of errors in php:
Level 1 error: notice, the lightest error, can be executed
Second-level error: warn, warning, and executable
Level 3 error: error. the program cannot be executed. this error is the most serious one.
Unset function description:
(PHP 3, PHP 4)
Unset -- release a given variable. if we don't want to use this variable, we can release it.
Description
Void unset (mixedvar [, mixed var [,...])
Unset () destroys the specified variable. Note that in PHP 3, unset () will return TRUE (actually an integer value of 1). in PHP 4, unset () is no longer a real function: it is now a statement. In this way, no return value is returned. an attempt to obtain the return value of unset () will cause a parsing error.
If unset () is a global variable in the function, only the local variable is destroyed, and the variables in the call environment will keep the same value before the call of unset. That is, the global variable remains unchanged outside the function. If unset () is a variable passed by reference in the function, only the local variable is destroyed, and the variables in the call environment will keep the same value before the call of unset. That is, the form parameter is an address character.
Php is a bit similar to the C language, does not support polymorphism, and does not allow the existence of functions with the same name, but is more flexible for the form parameters, you can do not write or write a few shape parameters when calling. You can also assign a default value to the parameter. For example, function diyMethod ($ a, $ B = 3) is to assign the default value of 3 to the form parameter, and the default value passing method of the php function is value passing. to use reference transfer (address transfer ), add the address character & before the parameter. In fact, the reference transfer is a bit equivalent to converting the input parameter that calls the function corresponding to the form parameter into a global variable. Although there is no global declaration. Passing a reference changes the value of the input parameter! Note that the reference here is not a pointer to the C language. because the pointer itself is a variable, and the php reference is actually an alias of the variable. to put it bluntly, an address can have multiple variable names. Referencing in PHP means accessing the same variable content with different names. When a variable is declared with global $ var, a reference to the global variable is actually created.
Definition of variables and constants in php
1. define the CONSTANT define ("CONSTANT", "Hello world .");
Constants can only contain scalar data (boolean, integer, float, and string ).
When calling a constant, you only need to obtain the value of the constant by name, instead of adding the "$" symbol, for example, echoCONSTANT;
Note: constants and (global) variables are in different namespaces. This means that for example, TRUE and $ TRUE are different.
2. common variable $ a = "hello ";
3. variable (using two dollar signs ($ ))
$ A = "world ";
Both variables are defined:
$ A's content is "hello" and $ hello's content is "world ".
Therefore, it can be expressed:
Echo "$ a $ {$ a}"; or echo "$ a $ hello"; both outputs: hello world
To use variables in an array, you must solve an ambiguous problem. When writing $ a [1], the parser needs to know that $ a [1] is used as a variable, you still want $ a as a variable and retrieve the value of [1] in the variable. The syntax for solving this problem is to use $ {$ a [1]} for the first case and $ {$ a} [1] for the second case.
4. static variables
In the function, static $ a = 0;
Note: assigning values to the results of expressions in the declaration results in parsing errors such as static $ a = 3 + 3; (error)
Static variables only exist in the local function domain (inside the function). after the function is executed, the variable values are not lost and can be used for recursive calls.
5. global variables
Global variables defined in the function body can be used in vitro. global variables defined in the function body cannot be used in the function body, you can use a special PHP custom $ GLOBALS array to access variables globally:
For example: $ GLOBALS ["B"] = $ GLOBALS ["a"] + $ GLOBALS ["B"];
A real global variable imported using a global statement in a function domain is actually a reference to a global variable.
Global $ obj;
Note: the static and global definitions of variables are implemented in the application mode.
6. assign a value to the variable: assign a value to the address (simple reference ):
$ Bar = & $ foo; // add the & symbol to the variable to be assigned a value.
Changing new variables will affect the original variables. this assignment operation is faster.
Note: Only named variables can assign values to addresses.
Note: If
$ Bar = & $;
$ Bar = & $ foo;
Changing the value of $ bar can only change the value of the variable foo without changing the value of a (the reference has changed)
7. PHP Super global variable $ GLOBALS: contains a reference variable that is valid globally to each current script. The key of the array is the name of the global variable. $ GLOBALS array exists from PHP 3.
$ _ SERVER: variables are set by the Web SERVER or directly associated with the execution environment of the current script. Similar to the old $ HTTP_SERVER_VARS array (still valid, but not used ).
$ _ GET: the variable submitted to the script through the http get method.
$ _ POST: the variable submitted to the script through the http post method.
$ _ COOKIE: variables submitted to the script through HTTP Cookies.
$ _ FILES: variables submitted to the script by uploading the http post file.
Enctype = "multipart/form-data" must be included in the file upload form"
$ _ ENV: the variable submitted to the script in the execution environment.
$ _ REQUEST: the variables submitted to the script through GET, POST, and COOKIE mechanisms. Therefore, this array is not trustworthy. All the existence or not of the variables contained in the array and the Order of the variables are defined according to the variables_order configuration instructions in php. ini. This array does not directly simulate earlier versions of PHP4.1.0. See import_request_variables ().
Note: Since PHP 4.3.0, the file information in $ _ FILES no longer exists in $ _ REQUEST.
$ _ SESSION: the variable currently registered to the script SESSION.
How to disable phpinfo ():
Php. ini
Disable_functions = phpinfo ()
Restart the web server.
Constants in php
A constant can only use define (constant name, constant value );
Constants can only contain scalar data (boolean, integer, float, and string ).
You can simply get the value of a constant by specifying its name. do not add the $ symbol before the constant. If the constant name is dynamic, you can also use the function
To read the value of a constant. Use get_defined_constants () to obtain a list of all defined constants.
Note: constants and (global) variables are in different namespaces. This means that for example, TRUE and $ TRUE are different.
If an undefined CONSTANT is used, PHP assumes that it wants the name of the CONSTANT, just like calling it with a string (CONSTANT corresponds to "CONSTANT "). At this time, an E_NOTICE-level error will be issued. See why $ w3sky [bar] is incorrect in the manual (unless bar is defined as a constant with define ). If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different:
* There is no dollar sign before a constant ($ );
* Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
* Constants can be defined and accessed anywhere regardless of the variable range rules;
* Once defined, a constant cannot be redefined or canceled;
* The constant value can only be a scalar.
Define constants
Define ("CONSTANT", "Helloworld .");
Echo CONSTANT; // outputs "Helloworld ."
Echo Constant; // outputs "Constant" and issues a notice.
?>
In the diving competition, 10 judges scored the score, removing one highest score and one lowest score, and then finding the average score of the remaining judges as the score of the athlete, the score is output to the highest score, the lowest score, and the athlete's score.
The answer is as follows:
12, "ms" => 9.0, "OK" => 7.1, "song" => 5.0, "bold" => 3.8, "know" => 2.7, "wow" => 1.7, "serial" => 7.9, "moder" => 7.6, "froke" => 6.7); function getMinMax ($ arr, $ k = true) {// $ k is the minimum output value when it is true. if it is false, the maximum output value is displayed. the average $ num = $ arr ["arial"]; $ sum = 0; $ s = "arial"; foreach ($ arr as $ I => $ value) {if ($ k) {if ($ value <$ num) {$ num = $ value; $ s = $ I ;}} else {if ($ value> $ num) {$ num = $ value; $ s = $ I ;}}$ sum = array_sum ($ arr); return array ($ sum, $ num, $ s) ;}$ newArr = getMinMax ($ arr, true); $ sum = $ newArr [0]-$ newArr [1]; echo 'the lowest degree people is '. $ newArr [2]."
"; Unset ($ newArr); $ newArr = getMinMax ($ arr, false); $ sum-= $ newArr [1]; echo'
The highest degree people is '. $ newArr [2]; echo'
The average degree is '. $ sum/(count ($ arr)-2);?>
Similarities: you can introduce other PHP files. Difference: When a file import error occurs in include, only a warning is reported, but the program continues to execute...