Backend - Lession 01 PHP foundation


Lession 01 php base

1. php base

  1. phpinfo (): output version number

    1. echo: Output text
    2. php.ini: php configuration file

2. php variable

  1. Naming variables must begin with $

  2. Strictly case-sensitive

  3. Special characters and can not start with a number (you can use an underscore)

  4. unset: Destruction Variables

  5. isset (): determines whether the variable exists isset(var1, var2)

  6. 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

  1. Variable does not recognize single quotes, double quotes variable identification

  2. 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
    
        // 结论:单引号不会转义特殊字符, 双引号可以
    ?>
    1. And a string variable, with a point .stitching together
  3. Double quotes may be spliced ​​together variables and strings "{$name}好好学习" -> "张三好好学习"

    1. Sets of double quotation marks single set of variables, output variables outside single quotes "'$name'" -> '张三'
  4. 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

  1. Detection Data Type: gettype ()
  2. Type Conversion
    1. intval (): is converted to an integer, integer
    2. floatval (): is converted to decimal, double
    3. strval (): converts a string, string
    4. boolval (): Converting to boolean, boolean
  3. Analyzing the data type of commonly used functions
    1. is_array()
    2. is_string()
    3. is_bool()
    4. is_float()
    5. is_object()
    6. is_int()
    7. is_numeric (): number string will be converted to digital, and then determine, but not mixed non-numeric character
    8. is_resource (): determine whether the resource
    9. is_null()
    10. is_scalar (): determining whether a scalar

6. Constant

  1. Define constants:define('abc', 'abc');

  2. Constant attention to points:

    * 常量可以直接用大写字母和下划线定义,不必加`$`符号
    • Assigned only scalar

    • Assigned only scalar
      * string constants can not be written

  3. Determining whether the constant is defined:defined('常量名')

    1. System constants:

    2. __FILE__: Find your files

    3. __LINE__: Get the code where the number of lines

    4. __DIR__: Find files in the current directory

    5. PHP_OS: Get system information

    6. PHP_VERSION: Get Version Information

    7. __FUNCTION: Get the current function name

    8. M_PI:PI

    9. To 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)

  1. Strings and numbers:
    • ''0'0'0.000
    • String '0.000'is true
  2. 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
  1. Variables and scope to see the title 2 variables
  2. 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

  1. func_get_args() : Get the parameters passed to form an array.
  2. 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

  1. random number
    • rand
    • mt_rand : 4 times better performance than the rand
  2. Decimal
    • floor: rounding down
    • ceil: roundup
    • round: rounding rounding
  3. 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


  1. 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
  2. Blank processing
    • trim: remove the first space
    • ltrim: remove head space
    • rtrim / chop: remove trailing spaces
  3. 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)
  4. 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
  5. order

    • str_shuffle: sequential random string upset
    • strrev: string in reverse order
  6. Change

    • chr: The ASCII value into character

    To be continued. . .


12.3 array of built-in functions

Guess you like

Origin www.cnblogs.com/mhxs/p/11222719.html