php interview error prone summary I

1, empty, isset,is_null difference

Definition: isset() is used to detect whether a variable has been declared and its value is not NULL. In other words, as long as the variable value is not NULL, it returns the true value;
empty() is used to detect whether a variable is empty. True values are returned in the following cases: empty string, false, empty array, NULL, 0, and the value deleted by uset;
is_ The NULL () function is used to determine whether the variable content is a NULL value. That is, the true value is returned only when the variable value is NULL.
Similarities: both return Boolean values, bool(true) or bool(false);
Difference: is_null() and isset() functions are inverse functions; isset() can be applied to undefined variables, and is_null() can only be used for declared variables;

For details, please refer to the php official website: empty,isset,is_null difference

2, Variable scope
definition:
Global variable: define a variable in php that can be accessed anywhere in the script. It is called "global variable".
Local variables: variables defined in methods of functions or classes can only be accessed inside functions;
Example

<?php

$globalName = "Lao Du";
function getvar(){
    $localName = "Raymonde";
    echo "Hello, $localName".PHP_EOL;
}
getvar();
echo "the value of \$globalName is :'$globalName'</br>";
echo "the value of \$localName is :'$localName'</br>";

The output result is:
Hello,Raymonde
the value of $globalName is 'Lao Du';
the value of $localName is '';   $localName Is a local variable, only in getvar Method has a value, so it is null in external output;

php allows internal access to external global variables. You only need to use the global keyword inside the function;

$globalName = "Lao Du";
function getvar(){
    $localName = "Raymonde";
    echo "Hello, $localName".PHP_EOL;
    
    echo "Hello, $globalName";  If you call an external global variable directly, $globalName Empty
	
	global $globalName;
	echo "Hello, $globalName";Output at this time Hello,Lao Du
	perhaps
	echo "Hello".$GLOBALS['globalName']Both methods are acceptable. Here is the difference between them
}
getvar();

Three differences between GLOBAL keyword and GLOBAL array
difference:
$GLOBALS ['var'] is the external global variable itself;
global v a r yes Outside Department var is external var is the same name reference or pointer of external var;
Example

$var1 = 1;
function test(){
	unset($GLOBALS['var1']);
}
test();
echo $var1;

The output is empty

$var1 == 1;
function test(){
	global $var1;
	unset($var1);
}
test();
echo $var1;

The output is 1, unset()Just delete $var1 The reference or pointer of does not really delete the value of itself

Four static variables
Save the results of the last local variable execution during function call for use in the next execution;

function staticMethod(){
     $a = 0;   
    return ++$a;
}
echo staticMethod().PHP_EOL;
echo staticMethod().PHP_EOL;
echo staticMethod().PHP_EOL;
echo staticMethod().PHP_EOL;

The output content is 1;

function staticMethod(){
    static $a = 0;   //When $a is a common variable, the output values are all 1; When it is a static variable, the output values are 1,2,3,4. When the function of a static variable is called, the last run value is saved.
    return ++$a;
}
echo staticMethod().PHP_EOL;
echo staticMethod().PHP_EOL;
echo staticMethod().PHP_EOL;
echo staticMethod().PHP_EOL;

The output content is 1,2,3,4

Five, require,require_once,include,include_once difference
If include() or include is scanned in the php script_ Once() statement, if the containing file fails, a Warning Error will be displayed, and then continue to execute;
If it is require () or require_once() statement, including that a Fatal Error will be thrown after the file fails and the execution of the script will be terminated; For rigorous words, use this; require() is faster than require_once()
6, Difference between = and =
=: assignment
==: judgment value;
===: judgment value and type;

$x = 23;
echo ($x == 23);   -->true
echo ($x === 23);  -->true
echo ($x === "23");-->false   $x by int Type, and "23" is a string type

7, HereDoc and NowDoc
The only difference between NowDoc and HereDoc is that NowDoc uses single quotation marks as delimiters;
Do not parse the contained text, and directly output plain text;

8, return and exit

The return function is specifically used in function bodies and methods. When using functions and methods in a low-level way, using return does not return calls from the function memory;
exit performs interrupt execution in the code;

9, Value passing and reference passing

The variable name is stored in the memory stack. It refers to the address of the specific memory in the heap. The memory in the heap can be found through the variable name.
Common value transfer: after value transfer, different addresses and names point to different memory entities;
Pass by reference: after passing by reference, there are different address names, but they all point to the same memory entity; Change one and the other will be changed;

This is an example of a tal XRS interview

$a = 1;
$b = &$a;
unset($a);  //At this time, only $a is disconnected from the memory, but $b will not be disconnected from the memory area, so $b still points to the memory
echo $b;  

Results: 1

unset does not really destroy variables, but disconnects variables from memory; Memory will not be released as long as it is still referenced;

Keywords: PHP Interview

Added by divinequran on Sun, 12 Sep 2021 00:29:12 +0300