PHP constants, defining constants in advance, magic constants, and basic data types

Source: Internet
Author: User
Tags arithmetic operators scalar

Constant

  • Defined:

    • Form One

      // define("PI",常量值);define("PI",3.14);define("school","讲理");
    • Form Two

      // 定义形式二const1234;const"abcd";
  • Use:

    • Form One

      // 直接使用常量名echo"PI的值:".PI;
    • Form Two

      // 通过constant函数,灵活性体现-->常量名也能够变// 利用变量拼凑常量名)echo"PI的值:".constant("PI");

Ps:

    • Infer whether a constant exists: use defined(常量名) function inference.
    • Use a constant that is not defined: in PHP, an error occurs when using a variable and constant that is not defined. Instead of outputting a value that does not define a variable, it outputs a value that does not have a constant defined, and its value is the constant name.

Defining constants in advance

    • definition: Some of the pre-defined constants in the system are about hundreds of. We just need to know how many of them can:

      1. M_PI:???????? Pi
      2. Php_os:?????? The operating system where PHP executes
      3. Php_version:??? PHP version
      4. Php_int_max:??? Maximum number of PHP integers
        ... Many other references to PHP manuals

Magic Constants

    • definition: In fact, it is just a constant form. But there is no constant "constant", its value will actually change, is only a few of the few:

      1. _file_: Represents the full physical path of the current Web page file
      2. _dir_: Represents the directory where the current Web page file resides
      3. _line_: Represents the "line number" where the variable name is currently located

Data type

    • Overall Division:

      There are 8 data types in PHP

    • Base type (scalar type)

      1. Integer type: int integer.
      2. Floating-point type: Float,double,real.
      3. String type: string;
      4. Boolean type: Bool,boolean.

    • Composite type:

      1. Arrays: array;
      2. Objects: Object.
    • Special types:

      1. Empty type: null, such a type has only one data, that is null.
      2. Resource type: resource, such as a connection resource for a database.

Integer type

  • There are 4 types of integers:

    $n1123// 10进制写法$n20123// 8进制写法$n30x123// 16进制写法$n40b11101// 二进制写法
  • To convert:

    • 10 binary to other 3 kinds of binary:

      1. Decbin (a 10 binary number): 10_>2, returns a binary string.
      2. DECOCT (a 10 binary number): 10->8, returns a 8 binary string.
      3. Dechex (a 10 binary number): 10->16, returns a 16 binary string.
    • 3 other kinds of binary to 10 binary

      1. Bindec (a binary string): 2->10, returns a 10 binary string;
      2. Octdec (an octal string): 8->10, returned with 10 binary;
      3. Hexdec (a hexadecimal string): 16->10, the return is 10 binary.

Floating-point types:

  • Two representations of
  • Floating-point numbers:

    1. General notation

      V1 = 123.456   
    2. Scientific notation: with a special symbol

       $v 1  = 123.456e2  Span class= "hljs-comment" >//means: 123.456 times 10 of 2 times, the result is a floating point number   $v 2  = 123.456e3 ; //meaning: 123.456 times 10 of the 3, the result is a floating-point number   $v 3  = 123e4 ;
       //meaning: 123 times 10 of the 4, the result is still floating point number   
  • Details of floating point usage:

    • Floating-point numbers should not be compared in size:

      1. All numbers, finally the expression form, are binary;
      2. The binary form of floating-point numbers cannot be completely accurate.
      3. Therefore, the comparison of floating-point numbers is unreliable.

      WORKAROUND: Convert floating-point numbers to integers (multiplied by 10 multiple sides) and compare

    • The decimal part of a floating-point number may not be accurately represented by binary;

    • When the result of an integer's operation exceeds the representation range of an integer. The system itself actively converts the results to floating-point types.

String

  • Four different forms:

    $str1"字符串内容...";$str2‘字符串内容...‘;$str3 = <<<"标识符A"字符串内容标识符A;$str4 = <<<"标识符B"字符串内容标识符B;
  • Single-argument string:

    // 单引號字符串。=,须要转义或能够识别的转义符有:\和‘$str1‘abcdefg‘;
  • Double-argument string:

    // 双引號字符串,须要转义或能够识别的特殊符号有:\,",\n,\r(回车符),\t(tab符),$。

    // 事实上就是取消了其在双引號字符串中的变量的“起始含义”$str2"abcdefg";

  • Double-argument delimiter string:

    // 双引號定界字符串,须要转义能够识别的特殊符号有:\n,\,//\r,\t。双引號和单引號能够直接写出$str4=<<<"ABCD"abcdef,这里事实上就是字符串的内容ABCD;

    Attention:

    1. The above identifier is our own "name" to obtain a similar constant name, can be arbitrary;
    2. The end line of the string, only the identifier itself and a semicolon can appear. Nothing else can appear, including spaces;
    3. The two arguments of the above identifiers can be omitted (not recommended);
    4. Identifiers are just a form that can recur.

  • Single-Quote bound string:

    // 单引號定界字符串,没有须要转义或者能够识别的特殊字符$str3=<<<‘ABCD‘abcdef,这里事实上就是字符串的内容ABCD;

Boolean type (only two data: TRUE or FALSE)

    • Word: Bool,boolean

    • Common use Forms

      • Direct inference of variables (reference manual);

      • Defines a variable to use as an inferred identity.

Type conversions

  • Self-motivated conversions:

    In any operation, it is assumed that some kind of data is required. The data given is not of that type. It usually happens that you have an active type conversion,

    Analogy: Octdec (), Bindec () ...

    in PHP. Arithmetic operators (including-,*,/,%) are simply calculations of values, such as

    $sum"1""2"// sum的值就是3
  • Enforces type conversions:

    ? is determined by the "operator" operation at the time of the self-initiated type conversion, and the coercion type conversion is the display of the target data type through syntax:

    //in the form of: (data type to convert) data   $i  = Span class= "Hljs-number" >10 ;
      $s  = (string)  $i ;  
    • Common conversion target types are: (int), (float), (string), (bool), (array), (object)

    • Change the data type of the original variable:

      ? The above coercion type conversion does not change the data type of the variable itself. Accordingly, there is a syntax that directly alters the data type of the original variable:

      settype ($ variable name, " target type );  
  • Type-related functions:

    • Var_dump (): "Complete information" for the output variable. Almost only for debugging code;
    • GetType ($ variable name): Gets the type name of the variable, returns a string representing the name of the type, for example: "String:," bool "," double ".

      。。。

    • SetType ($ variable name, "target Type");
    • Isset (), Empty (), Nuset ().
    • IS_XX type ($ variable name): series function to infer whether it is a data type
      1. Is_int ();
      2. Is_string ();
      3. Is_bool ();
      4. Is_float ();
      5. Is_numeric (); Infer whether it is a number
      6. Is_scalar (); Infer whether it is a scalar
      7. Is_array ();
      8. Is_object ();
      9. Is_null (); Infer whether it is empty

PHP constants, defining constants in advance, magic constants, and basic data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.