table of Contents
- Lession 01 php base
- 1. php base
- 2. php variable
- 3. php difference between single and double quotes
- 4. Data Type
- The data type conversion
- 6. Constant
- 7. Operators
- 8. several cases of false (conditional)
- 9. Process Analyzing
- 10. cycle
- 11.1 Functions
- 11.2 line arguments and return values of the function
- 12. Common Functions
Lession 01 php base
1. php base
phpinfo (): output version number
- echo: Output text
- php.ini: php configuration file
2. php variable
Naming variables must begin with $
Strictly case-sensitive
Special characters and can not start with a number (you can use an underscore)
unset: Destruction Variables
isset (): determines whether the variable exists
isset(var1, var2)
Scope of simple variables : function
内部作用域
and外部作用域
is not related<?php // 变量的作用域 $test = 'hello'; function test () { echo $test; // 报错, 变量未定义 } test();
2.1 Static variables:
static
You can define static variables- Only initialized once, the previous value will be saved
<?php // 静态变量 function total() { static $num = 2; $num *= 2; echo $num; } total(); total(); total();
2.2 superglobals:
- $ GLOBALS: anywhere for PHP scripts to access the global variables
- $ _SERVER: holds information about headers, paths, and script locations
- $ _REQUEST: receiving data HTML form submission
- $ _POST: receiving request data post
- $ _GET: receiving a get request data
- $ _FILES: receiving a data file
- $ _ENV: is an array of server-side environment variables include
- $ _COOKIE: COOKIE brought the acquisition request
- $ _SESSION: acquisition request brought SESSION
3. php difference between single and double quotes
Variable does not recognize single quotes, double quotes variable identification
Single quotes will not escape special characters, double quotation marks
<?php // 单双引号区别 $name = '张三'; $age = 18; $sex = 'man'; echo "$name"; // 张三 echo '$name'; // $name // 结论:单引号不解析变量,双引号解析变量 echo "\n"; // 换行 echo '\n'; // \n // 结论:单引号不会转义特殊字符, 双引号可以 ?>
- And a string variable, with a point
.
stitching together
- And a string variable, with a point
Double quotes may be spliced together variables and strings
"{$name}好好学习" -> "张三好好学习"
- Sets of double quotation marks single set of variables, output variables outside single quotes
"'$name'" -> '张三'
- Sets of double quotation marks single set of variables, output variables outside single quotes
Efficient than single quotes double quotes
4. Data Type
1. 标量
1. 整型:1 2
2. 浮点型:1.2 1.3
3. 布尔类型:true false
4. 字符串:单双引号引起来的都是字符串
2. 混合类型(重点)
1. 数组:array
2. 对象:object
3. 特殊类型
1. 空:null
2. 资源:resource
The data type conversion
- Detection Data Type: gettype ()
- Type Conversion
- intval (): is converted to an integer, integer
- floatval (): is converted to decimal, double
- strval (): converts a string, string
- boolval (): Converting to boolean, boolean
- Analyzing the data type of commonly used functions
- is_array()
- is_string()
- is_bool()
- is_float()
- is_object()
- is_int()
- is_numeric (): number string will be converted to digital, and then determine, but not mixed non-numeric character
- is_resource (): determine whether the resource
- is_null()
- is_scalar (): determining whether a scalar
6. Constant
Define constants:
define('abc', 'abc');
Constant attention to points:
* 常量可以直接用大写字母和下划线定义,不必加`$`符号
Assigned only scalar
Assigned only scalar
* string constants can not be written
Determining whether the constant is defined:
defined('常量名')
System constants:
__FILE__
: Find your files__LINE__
: Get the code where the number of lines__DIR__
: Find files in the current directoryPHP_OS
: Get system informationPHP_VERSION
: Get Version Information__FUNCTION
: Get the current function nameM_PI
:PITo understanding:
__MHTHOD__
: Get the current members of the method name__NAMESPACE__
: Get the name of the current namespace__TRAIT__
: Get the current TRAIT name (multiple inheritance)__CLASS__
: Gets the current class name
7. Operators
And other similar language
8. several cases of false (conditional)
- Strings and numbers:
''
、0
、'0'
、0.000
- String
'0.000'
is true
- Array:
[]
(with different js)
9. Process Analyzing
Basically the same as with other languages
expand:
1. Random Number: mt_rand (1, 10);
10. cycle
As with other languages, variables within the loop is not local variables
11.1 Functions
definition:function name() {}
1. function into the function library and custom functions
2. Custom library no longer function
3. Line Default parameters may be provided , with similar es6
4. When the default parameters not set, the argument being given less
The function is not case-sensitive
<?php
function Name($name = '刘程', $sex = '男') {
echo '我是:'.$name.' . 性别:'.$sex;
return true;
}
$temp = name('liucheng'); // 我是:liucheng . 性别:男
echo '<br />'.$temp; // 返回值 true
- Variables and scope to see the title 2 variables
- Line arguments of type constraints (important)
11.2 line arguments and return values of the function
11.2.1 Line Type parameter constraints
After the line type parameter constraints, the function will convert the incoming data into the corresponding data, if not convert an error.
<?php
// 约束函数行参的类型
function test(string $name, int $num) {
echo gettype($name).'<br />';
echo gettype($num).'<br />';
}
test('liucheng', 20);
test(520, 20);
// test(520, "a"); // 报错, 'a' 不是整数
11.2.2 return value constraint type
变量括号后面加一个 `:string` , 约束其返回值为 string ,不是 string 就会报错
<?php
// 约束函数返回值
function test(string $name, int $num):string {
return $name.$num;
}
print_r(test('liucheng', 20).'<br>');
print_r(test(520, 20).'<br>');
echo test(520, "a").'<br>';
11.2.3 function of the variable parameter
Do not set the line parameters, you can get the parameters passed
func_get_args()
: Get the parameters passed to form an array.func_get_arg(n)
: Obtaining parameters corresponding to the target position.
<?php
// 可变参数
function test() {
$temp = func_get_args();
echo func_get_arg(1).'<br>';
return $temp;
}
print_r(test('liucheng', 20)); // 输出:Array ( [0] => liucheng [1] => 20 )
12. Common Functions
12.1 Mathematical Functions
- random number
- rand
- mt_rand : 4 times better performance than the rand
- Decimal
- floor: rounding down
- ceil: roundup
- round: rounding rounding
- other
- abs: absolute value
- pi: pi
- M_PI: constants, and pi () function returns the same value
- pow: exponential expression
- max: maximum
- min: minimum value
12.2 String built-in functions
- Case conversion
- strtolower: uppercase lowercase turn
- strtoupper: uppercase lowercase turn
- lcfirst: the first letter lowercase
- ucfirst: capitalize the first letter
- ucwords: capitalize the first letter of each word
- Blank processing
- trim: remove the first space
- ltrim: remove head space
- rtrim / chop: remove trailing spaces
- Find positioning
- strstr / strchr: detecting the first occurrence of the string in another string to the end of the content (case sensitive)
- strrchr: detecting the last occurrence of the string to another string at the end of the content
- stristr: strstr ignore case version
- strpos: Returns the position of the first occurrence of the string (case-sensitive)
- stripos: strpos ignore case version
- strrpos: Returns the last position of occurrence of the string (case sensitive)
- strripos: strrpos ignore case version
- sbustr: substring extraction
- strpbrk: return (any character search) content at the end of the first time to the (case-sensitive)
- Compare
- strcmp: binary comparison string
- strcasecmp: strcmp case-insensitive comparison
- strnatcmp: the use of a " natural ordering " algorithm to compare two strings (case sensitive), in the natural algorithm, the digital number less than 2 10. Ordering computer, 2 is less than 10, because the first number is less than 2 10.
- strnatcasecmp: strnatcmp: ignore case version
order
- str_shuffle: sequential random string upset
- strrev: string in reverse order
Change
- chr: The ASCII value into character
To be continued. . .