PHP basic note data type
String
Integer
Floating point number
Boolean value
Array
Object
NULL
Undefined variable. The data type is NULL.
Arrays and objects in PHP are of different types, while arrays in js are objects. (Ps: es6 has built-in class)
Operator
Concatenate strings using the. (dot operator) instead of the + operator. So the two string numbers in PHP can be connected with +, and will be automatically converted to numbers.
$a = "100";$b = "200";$c = $a + $b;echo $a + $b; // 300
Loop statement
Foreach loop only appliesArrayAnd is used to traverse each key/value pair in the array.
$a = array("red", "green", "blue", array(1, 2, 3)); foreach ($a as $i) { var_dump($i);}// string 'red' (length=3)// string 'green' (length=5)// string 'blue' (length=4)// array// 0 => int 1// 1 => int 2// 2 => int 3Double quotation marks and single quotation marks
In JavaScript, double quotation marks and single quotation marks are used basically the same.
The single quotes in php treat the content as plain text without being translated by the server. Double quotation marks, on the contrary, are processed by the server and then output as HTML code.
$a = 10;echo "the value of a is $a"; // the value of a is 10echo 'the value of a is $a'; // the value of a is $a
It is useful when writing database query statements.
It turns out to be like this:
$date = '2016-03-01';$sql = 'select * from adblock where day >= ' . '"' . $date . '"' . ' order by day';
You only need:
$date = '2016-03-01';$sql = "select * from adblock where day >= $date order by day";
Use single quotes (high efficiency) to avoid double evaluation (a bit like eval () in js, and setTimeout callback without passing strings ).
Function parameters
Js functions are passed by value (see here for details). basic input functions do not change the original value, and objects (including arrays) parameters must be passed in with caution, if you don't pay attention to it, you may change the original value.
PHP supports passing by value and by reference. The difference is an & address symbol in function parameters, which is similar to that in C language.
By value (original value unchanged ):
function fn($a) { $a[0] = 10;}$a = array(1, 2, 3);fn($a);var_dump($a);// array// 0 => int 1// 1 => int 2// 2 => int 3
By reference (Change original value ):
function fn(&$a) { $a[0] = 10;}$a = array(1, 2, 3);fn($a);var_dump($a);// array// 0 => int 10// 1 => int 2// 2 => int 3Variable scope
In js, the scope of variables is the unit of functions, and there is no block-level scope. (PS: es6 let keyword implements block-level scope ).
PHP has three different variable scopes:
Local)
Global)
Static)
FunctionBesidesDeclared variables have Global scopes,OnlyAccess outside the function. (Unlike JavaScript, js functions can access variables other than functions)
FunctionInternalDeclared variables have LOCAL scopes,OnlyAccess within the function. (Similar to js)
Variables outside the PHP function can only be accessed outside the function, and variables in the function can only be accessed within the function. You can use the global keyword (or $ GLOBALS []) to break through the internal and external boundaries.
Function access to external variables:
$a = 10;function fn() { global $a; echo $a; // 10}fn();
Or:
$a = 10;function fn() { echo $GLOBALS['a']; // 10}fn();
Access internal variables outside the function (in fact, global variables are declared in the function ):
function fn() { global $a; $a = 20;}fn();echo $a; // 20
Or:
function fn() { $GLOBALS['a'] = 20;}fn();echo $a; // 20Type conversion
In PHP, variables can also be forcibly converted, which is similar to the C language. Add the target type before the variable to be converted, and enclose the target type in parentheses.
Mandatory conversions allowed in PHP include:
(Int), (integer) -- convert to an integer
(Bool), (boolean) -- convert to bool type
(Float), (double), (real) -- convert to floating point type
(String) -- convert to string
(Array) -- convert to an array
(Object) -- convert to object
Or directly use the settype function, which is simple and crude. (Refer to the following section)
Type determination
Determine the type of the variable:
Is_integer ()
Is_string ()
Is_double ()
Is_array ()
Is_null ()
Is_object ()
Is_int ()
Is_numeric ()
Type of the obtained variable:
Gettype ()
Set the variable type:
Settype ()
$a = 10;settype($a, "string");var_dump($a); // string '10' (length=2)
In addition, var_dump () also returnsData typeValue.
Basic debugging
Use echo () (can output more than one string) or var_dump () to print the variable. If the php file is opened directly in the browser, the printed result will appear on the page; if the http request is used, the printed result will be in the http response (f12 view ).
When js debugging is used to the output style on the console, you can try chrome-logger.
Others
Isset () determines whether the variable has been defined
Unset () deletes one or more variables.