PHP Simple Explanation

Why learn PHP?

  • PHP is a back-end language
  • Why do you need to learn a back-end language for front-end development?
    1. The current market demand requires front-end personnel to master a back-end language
    2. Facilitate interaction with back-end developers

PHP file

  • When we are writing JavaScript, it is a .jsfile
  • When we are writing html, it is a .htmlfile
  • The PHP code is written in a .phpfile with a suffix

PHP file writing

  • All PHP code must be written in a PHP scope
  • Requirements <?phpstart with
  • Request to ?>end
<?php

  # 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);
#该文件名设置为 base.php
<?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
	// 需要解决中文乱码问题,导入base.php文件
    include "base.php";
    echo "你好,世界";
?>

PHP syntax

  • Each language will have its own grammar
  • Next, we will briefly understand the PHP grammar rules
  • PHPThere 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
	# 井号是PHP中的注释
	# 下面就是定义了一个变量,并且赋值为 100
	# 变量名就是$num
	$num = 100;
	$boo = true;
	# 下面是一个字符串
	$str = '您好,PHP';
	# 使用变量
	echo $str;
?>

String

  • There are two definitions of strings in php
    1. Single quotation mark: is a normal string
    2. 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
	// 1 单引号
	$age = 18;
	$s1 = 'i am $age years old';
	echo $s1;
	# 得到的就是 i am $age years old
	
	// 2 双引号
	$s2 = "i am $age years old";
	echo $s2;
	# 得到的就是 i am 18 years old
	
	// 3 字符串的拼接
	$str = 'hello ';
	$str = 'world';
	$str3 = $str.$str2;
	echo $str3;
	# 得到的就是 hello world
?>

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);
	# Array([0]=>1[1]=>2[2]=>3)
	#这个就类似于我们 js 中的数组,按照索引来的

	# 创建一个数组
	$arr2 = array('name'=>'jack','age'=>18,'gender'=>'男');
	print_r($arr2);
	# Array ( [name] => Jack [age] => 18 [gender] => 男 )
    # 这个就类似于我们 js 中的 对象,键值对的形式
?>

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

  • Convert php data into json data format
    • json_encode()
    • grammar:json_encode(要转换的php数据格式)
    • Return value: converted json data format
  • (js里面把对象转成json数据格式:JSON.stringify(js对象))
    
  • Convert json data format to php data format
    • json_decode()
    • grammar:json_decode(要转换的json数据格式)
    • Return value: converted php data format
  • (js里面把json数据格式转成对象:JSON.parse(json数据格式))
    
<?php
// 在输出之前,告诉浏览器要用什么字符集格式来解析文本内容
header('content-type:text/html;charset=utf-8;');

// 0 准备一个php格式的数组
$arr = array('name'=>'jack','sex'=>'男','love'=>'rose');

// 1 把php格式的数据转换成json格式的数据
$jsonArr = json_encode($arr);
echo $jsonArr;

// 2 把json数据格式转成php数据格式
$newArr = json_decode($jsonArr);
print_r($newArr);
?>

Guess you like

Origin blog.csdn.net/qq_45677671/article/details/113740288