Php basic learning notes (12) and php basic learning notes 12

Source: Internet
Author: User

Php basic learning notes (12) and php basic learning notes 12
Php basic syntax

<? Php // This is a single php line comment/* This is a multi-line comment * // each php statement ends with a semicolon (;). It must be used! // Php is a case-sensitive language -- except function names // any variable name in php must start with a $ character.?>
Variables and constants

In php, the variable starts with a $ symbol. A variable is usually directly assigned a value (if it is the first assignment, it is defined ).

Constant: Set a data identifier (Data name) whose value should not be changed, such as the mathematical "pi ".

$ Pi = 3.14; // This is a variable

Define ("PI", 3.14); // This is the definition of a constant named "PI" with a value of 3.14, which is similar to a value assignment here

Constant definition is also a value assignment, and only this one-time!

Differences between constants and variables:

  • No dollar sign before a constant ($)
  • Constants can only be defined by the define () function, but cannot be assigned by the value assignment statement.
  • Constants can be defined and accessed anywhere, regardless of the variable range rules.
  • Once defined, a constant cannot be redefined or undefined.
  • Constant values can only be scalar (I .e. integer, floating point, String, Boolean)
Data Type:

Basic type (scalar type ):

Boolean (bool, boolean): Only two data types: true false

Character type (string): it can be single quotation marks, double quotation marks, or "delimiters" format.

Integer type (int, integer ):

$ V1 = 10;

$ V2 = 012; // a number starting with 0 represents an "octal" integer, which is equivalent to 10 in decimal format.

$ V3 = 0x1F; // a hexadecimal integer starting with 0x, which is equivalent to 31 in decimal format.

Floating point type (float, double)

$ V4 = 12.34;

$ V5 = 12.34e5; // 12.34x10 to the power of 5

Composite Type:

Array type:

Object Type:

Special Type:

Resource Type: indicates an external data type.

Null: only one data type is null"

 

Differences between the three string types:
$ Str1 = "abc \ ndefg1"; // The following escape characters can be recognized in double quotation marks: \ "\ n \ r \ t \ $ str2 = 'a \ bc \ ndefg2 \ '; // only the following escape characters can be recognized in a single quotation mark string: \'\\. "\" Is usually used only for the last character. // The following is the separator string, where "ABCD" is a random name. This symbol is used to end a string. // The Escape Character string can recognize the following escape characters: \ n \ r \ t \ $ str3 = <ABCDabc \ nde "f'g3 this is called the delimiters. Any content within the range of the string is a line break of the str3 string. you can also directly change to html, js, and so on... For example: 

By default, in php, object types and resource types are passed by reference.

However:

We can also manually use the data passed by the default value to use the reference to pass the value. Syntax:

Variable 1 = & Variable 2;

Example:

$ V1 = 10;

$ V2 = & $ v1; // $ v1 and $ v2 represent "one data content"

$ V1 ++;

Echo $ v2;

Comparison js:

In js, only the default value passing method is available, and no manual setting is available.

In js: the default values are passed in the following types: Numbers, characters, Boolean, and two special types.

Default reference value types include: array, Object

OPERATOR:

L Arithmetic Operator: +-*/% + + --

  • + Is used only for "Arithmetic Operations" and has no double meanings.
  • ++ -- The meaning and usage of the symbol are exactly the same as those of js.

L written before the variable, then: First Auto-increment (auto-Subtract), then other operations (such as value assignment)

L after the variable is written, perform other operations (such as assigning values) and then auto-increment (auto-subtraction)

  • Only the integer is obtained and calculated. If it is not an integer, it is automatically converted to an integer and then the remainder is obtained.

L comparison operator: =! ==>=<<=== (All equals )! = (Not all equal)

  • = Usually indicates "Fuzzy equality", = indicates exact equality (only when the data type and value are the same)
  • For more information about Fuzzy Equivalence, see Appendix> type comparison table.

L conditional OPERATOR: (expr1 )? (Expr2): (expr3), for example:

  • Meaning: evaluate the expression exp1. If the result is true, the result of the entire expression is exp2. Otherwise, the result of the entire expression is exp3.
  • $ V1 = $ fs> 60? "Pass": "retake"; // v1 has two conditions based on the fs value.
  • Additional knowledge: unary operators include :!, ++ ,--,~,, Binary operators are the most commonly used operators.

L logical operators: & (and) | (OR )! (Not)

  • Logic and &: The result is true only when both data are true.
  • Logic or |: The result is true if one data is true.
  • Non-logical! : Obtain the opposite value of the Logical Data.

L string OPERATOR:. (that is, English periods). = (that is, similar to "+ = ")

  • . = Is just a "shorthand", $ v1 = $ v1. "abc"; è $ v1. = "abc ";

L-bit operator: & (and) | (OR )~ (Not) ^ (exclusive or) <(left shift)> (right shift)

  • Bitwise operators are used for binary numbers.
  • Bitwise AND &: The result is 1 only when the numbers on the two corresponding bits are 1.
  • Bitwise OR |: as long as the number on the corresponding digit is 1, the result is 1.
  • By-bit non ~ : Reverse, that is, 1 is changed to 0, 0 is changed to 1.
  • Bitwise OR ^: when the numbers on two corresponding bits are different, the result is 1.

L value assignment operator: = + =-= * =/= % =. =

 

Operator priority: basically the same as js.

 

Data Type Conversion

In js, there is basically no concept of data type conversion, and the actual conversion is "automatic"-Default conversion.

Js has two "similar" type conversion functions: parseInt (....) ParseFloat (...)

In php, both default conversion and forced conversion are supported.

Default conversionExample:

$v1 = 10 - “3”;        //7;    $v2 = 10 + “3”;        //13;    $v3 = 10 + “3abc”;    //13;    $v4 = 10 + “3.5abc”;    //13.5;    $v5 = 10 + “abc3”;    //10;    $v5 = 10 + “abc”;    //10    $v5 = “10” + “abc”;    //10    $v5 = “10ABC” + “5abc”;    //15    $v5 = “ABC10” + “abc5”;    //0    $v5 = “ABC” + “abc”;    //0

...................... Any operation using arithmetic operators will be regarded as a number. If it cannot be converted into a number, it will also be calculated as 0.

Forced conversion:

Syntax: (target type to be converted) data; example: $ v1 = (int) "3.5abc"; // 3; int type $ v1 = (float) "3.5abc "; // 3.5; // float Type $ v1 = (string) 3; // "3"; $ v1 = (bool) "3.5"; // true

-- In particular, it is recommended to convert a data type to the bool type: query manual: Appendix> type comparison table> compare the variable $ x> if ($ x) Column

 

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.