Why learn PHP?
PHP is a back-end language
Why do you need to learn a back-end language for front-end development?
The current market demand requires front-end personnel to master a back-end language
Facilitate interaction with back-end developers
PHP file
When we are writing JavaScript
, it is a .js
file
When we are writing html
, it is a .html
file
The PHP code is written in a .php
file with a suffix
PHP file writing
All PHP code must be written in a PHP scope
Requirements <?php
start with
Request to ?>
end
<?php
?>
PHP Chinese garbled
Just tell the browser what character set format to use to parse the text content before outputting
Tell the browser character set format: header (character set format);
<?php
header ( 'content-type:text/html; charset=utf-8;' ) ;
?>
PHP import syntax
Just bring another php file to me for execution
grammar: include "你要导入的文件路径";
path:
Write the file name directly to indicate the same level directory, but this is not recommended
./
It also means the same level directory, and it is recommended to write
../
Indicates the upper level directory
<?php
include "base.php" ;
echo "你好,世界" ;
?>
PHP syntax
Each language will have its own grammar
Next, we will briefly understand the PHP grammar rules
PHP
There is a point that must be paid attention to. Every sentence must be followed by ;
Three kinds of notes
<?php
?>
Output syntax
echo 输出内容;
Only basic data types can be output, complex data types will report errors
Encountered Boolean type, true output 1, false output blank content
print_r(输出内容);
Can output all data types
Encountered Boolean type, true output 1, false output blank content
var_dump(输出内容);
Can output all data types
Encountered Boolean type, true output true, false output false
And will carry the data type and data information
variable
In php, we define variables without keywords
Start with $ means a variable
$ Is not a keyword to define a variable, it is part of the variable name
<?php
$num = 100 ;
$boo = true ;
$str = '您好,PHP' ;
echo $str ;
?>
String
There are two definitions of strings in php
Single quotation mark: is a normal string
Double quotes: is a special string in which variables can be directly parsed
In PHP, string splicing is no longer used +
for splicing, but used .
for splicing
<?php
$age = 18 ;
$s1 = 'i am $age years old' ;
echo $s1 ;
$s2 = "i am $age years old" ;
echo $s2 ;
$str = 'hello ' ;
$str = 'world' ;
$str3 = $str . $str2 ;
echo $str3 ;
?>
Conditional statements
The use of conditional statements in PHP is basically the same as js
<?php
$boo = true ;
if ( $boo ) {
echo '您好,欢迎光临!' ;
} else {
echo "你还没有登录" ;
}
?>
loop statement
The loop statement in PHP is basically the same as js
<?php
$num = 5 ;
for ( $i = 0 ; $i < $num ; $i ++ ) {
echo 'hello php' ;
}
?>
PHP array
php index array is equivalent to the inside js array
grammar: $arr = array(数据1,数据2,数据2,...);
Sort by index subscript
Get a certain data in the array: 数组名称[对用的索引]
PHP associative array is equivalent to objects in js
grammar: $arr = array(key1=>valeu1,key2=>value2,....)
Whether it is key or value, it needs to be wrapped in quotation marks, otherwise it will be used as a variable (error report)
Numbers and Boolean values, do not need to be quoted, can be used directly
Get a certain data in the array: 数组名称['你要获取的属性名']
Array method, just understand
in_array(value,$arr)
: Does a value exist in the array
count($arr)
: Calculate the length of the array
array_key_exists(key,$arr)
: Does a key exist in the array
unset($arr[key])
: Delete a key-value pair in the array
<?php
$arr = array ( 1 , 2 , 3 ) ;
print_r ( $arr ) ;
$arr2 = array ( 'name' = > 'jack' , 'age' = > 18 , 'gender' = > '男' ) ;
print_r ( $arr2 ) ;
?>
PHP and json format conversion
Why do json format conversion
The composition of the numbers in php is different from that in js
For example: array("jack","rose","lucy");
Can be passed to the front end normally, but the front end does not recognize
Transform into a look everyone knows
All I know is the json string format
Because the json format of all languages is exactly the same
For more convenient front-end interaction
Two methods for php to convert json format
<?php
header ( 'content-type:text/html;charset=utf-8;' ) ;
$arr = array ( 'name' = > 'jack' , 'sex' = > '男' , 'love' = > 'rose' ) ;
$jsonArr = json_encode ( $arr ) ;
echo $jsonArr ;
$newArr = json_decode ( $jsonArr ) ;
print_r ( $newArr ) ;
?>