Similarities and differences between include and require:
Same point: both can import other php files.
Difference: When an import file error occurs in include, only a warning will appear, but the program will still continue to execute. When an error occurs in require, an error will be reported and the program will terminate.
Generally speaking, include_once and require_once are used instead of include and require. The statement that introduces the file should be placed first, which is similar to C language.
PHP variables are case-sensitive, but function names are not. And it cannot start with a number. For example, Abc and abc are the same. If these two functions coexist in the same file, an error will be reported.
Analysis of situations where local variables and global variables have the same name;
Look at the following piece of code:
$a=45;
function abc(){ //Define function
$a+= 10;
}
abc(); //Call function
echo ‘$a=’.$a;
What is the output result? The result is still 45, why? Inside the function body are local variables, which have the same name as the external variable $a, but they are not the same variable, but two completely unrelated variables. The variable $a within the function is released as the function completes execution. The output is still an external variable. If you want to use an external variable (global variable) within the function body, you must add a global statement before the internal variable with the same name. In fact, from the perspective of the compiler, when declared as global, the address of the external variable with the same name is actually assigned to this local variable. At this time, the internal variable and the external variable with the same name have the same address, so they naturally become the same variable. Formal parameters in the function body or local variables that are not declared as global are used as local variables, and the scope is within the function body. For example, the output result of the following code is 45, not 55.
$a=45;
function abc($a){ //Define function
$a += 10;
}
abc($a); //Call function
echo ‘$a=’.$a;
There are three levels of errors in php:
First level error: notice, the lightest error, can still be executed
Level 2 error: warn, warning, you can also execute
The third level error: error, the program cannot be executed. This error is the most serious error
unset function explanation:
(PHP 3, PHP 4)
unset -- Release the given variable. We can release this variable when we don't want to use it.
Description
void unset ( mixedvar [, mixed var [, ...]])
unset() destroys the specified variable. Note that in PHP 3, unset() will return TRUE (actually the integer value 1), while in PHP 4, unset() is no longer a real function: it is now a statement. There is no return value, and trying to get the return value of unset() will result in a parsing error.
If you unset() a global variable in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset(). That is, the global variable remains unchanged outside the function. If you unset() a variable passed by reference in a function, only the local variable is destroyed, and the variables in the calling environment will retain the same value before calling unset(). That is, the formal parameter is an address character.
php is somewhat similar to the C language. It does not support polymorphism and does not allow the existence of functions with the same name. However, it is more flexible for formal parameters. You can omit or write fewer formal parameter values when calling. You can also assign default values to formal parameters. For example, function diyMethod($a,$b=3) assigns a default value of 3 to the formal parameters. The default value transfer method of PHP functions is value transfer. If you want to use reference transfer (address transfer), add the address character before the formal parameters. & that’s it. Passing by reference is actually equivalent to turning the incoming parameters corresponding to the formal parameters of the function into global variables in disguise. Although there is no global statement. Passing by reference changes the value of the passed parameter! But please note that the reference here is not a pointer in C language. Because the pointer itself is a variable, and the reference in php is actually an alias of the variable. To put it bluntly, one address can have multiple variable names. Quoting in PHP means accessing the same variable contents with different names. When you declare a variable with global $var you actually create a reference to the global variable.
How to define variables and constants in php
1. Define constants define("CONSTANT", "Hello world.");
Constants can only contain scalar data (boolean, integer, float and string).
When calling a constant, you only need to simply use the name to get the value of the constant, without adding the "$" symbol, such as: echoCONSTANT;
Note: Constants and (global) variables are in different namespaces. This means for example TRUE and $TRUE are different.
2. Ordinary variable $a = "hello";
3. Variable variables (use two dollar signs ($))
$$a = "world";
Both variables are defined:
The content of $a is "hello" and the content of $hello is "world".
Therefore, it can be expressed as:
echo "$a ${$a}"; or echo "$a $hello"; they will both output: hello world
To use mutable variables with arrays, an ambiguity must be resolved. This is when writing $$a[1], the parser needs to know whether it wants $a[1] as a variable, or whether it wants $$a as a variable and extracts the variable with index [1] value. The syntax to solve this problem is to use ${$a[1]} for the first case and ${$a}[1] for the second case.
4. Static variables
static $a =0 inside the function;
Note: Assigning it with the result of an expression in the declaration will result 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 value will not be lost and can be used for recursive calls
5. Global variables
Global variables defined inside the function body can be used outside the function body. Global variables defined outside the function body cannot be used inside the function body. To access variables in the global scope, you can use the special PHP custom $GLOBALS array:
For example: $GLOBALS["b"] = $GLOBALS["a"] +$GLOBALS["b"];
A real global variable imported using the global statement within a function scope actually establishes a reference to the global variable
global $obj;
Note: The static and global definitions of variables are implemented in an application manner
6. Assign a value to a variable: assign by address (simple reference):
$bar = &$foo; //Add & sign before the variable to be assigned
Changing new variables will affect the original variables, and this assignment operation is faster
Note: Only named variables can be assigned by address
Note: If
$bar = &$a;
$bar = &$foo;
Changing the value of $bar can only change the value of variable foo, but not the value of a (the reference has changed)
7.PHP super global variable $GLOBALS: Contains a reference pointing to variables that are valid in the global scope of each current script. The keys of this array are labeled with the names of global variables. The $GLOBALS array exists since 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 deprecated).
$_GET: Variables submitted to the script via the HTTP GET method.
$_POST: Variables submitted to the script via the HTTP POST method.
$_COOKIE: Variable submitted to the script via the HTTP Cookies method.
$_FILES : Variables submitted to the script via HTTP POST file upload.
The file upload form must have enctype="multipart/form-data"
$_ENV: Variables submitted to the script by the execution environment.
$_REQUEST: Variables submitted to the script via GET, POST and COOKIE mechanisms, so this array is not trustworthy. The presence, absence, and order of all variables contained in this array are defined according to the variables_order configuration directive in php.ini. This array does not directly emulate earlier versions of PHP4.1.0. See import_request_variables().
Note: As of PHP 4.3.0, file information in $_FILES no longer exists in $_REQUEST.
$_SESSION: Variable currently registered for the script session.
How to disable phpinfo():
php.ini
disable_functions = phpinfo()
Restart the web server.
Constant in php
Constants can only be defined (constant name, constant value);
Constants can only contain scalar data (boolean, integer, float and string).
You can get the value of a constant simply by specifying its name. Do not add the $ sign in front of the constant. If the constant name is dynamic, you can also use the function
constant() to read the value of a constant. Use get_defined_constants() to get a list of all defined constants.
Note: Constants and (global) variables are in different namespaces. This means for example TRUE and $TRUE are different.
If an undefined constant is used, PHP assumes that what is wanted is the name of the constant itself, as if calling it with a string (CONSTANT corresponds to "CONSTANT"). An E_NOTICE level error will be issued. See the manual for why $w3sky[bar] is wrong (unless bar is defined as a constant with define() beforehand). If you just want to check whether a certain constant is defined, use the defined() function.
Constants and variables are different:
* There is no dollar sign ($) in front of the constant;
* Constants can only be defined using the define() function, not assignment statements;
* Constants can be defined and accessed anywhere regardless of variable scope rules;
* Once defined, a constant cannot be redefined or undefined;
* The value of a constant 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 give scores, remove the highest score and the lowest score, and then calculate the average of the remaining judges' scores as the athlete's score. Try to output the score to the judge with the highest score and the judge with the lowest score. The judges and the athlete’s score.
The answer is as follows:
<?php
$arr = array("arial"=>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 为 true时输出最小值,为false 则输出最大值,还有平均数
$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]."<br />";
unset($newArr);
$newArr=getMinMax($arr,false);
$sum -= $newArr[1];
echo '<br />the highest degree people is '.$newArr[2];
echo '<br />the average degree is '.$sum/(count($arr)-2);
?>
Copy after login
http://www.bkjia.com/PHPjc/532689.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532689.htmlTechArticleSimilarities and differences between include and require: Similar points: Both can introduce other php files. Difference: When an import file error occurs with include, only a warning will appear, but the program will still continue to execute...