PHP Basic Learning Notes, PHP Basic Learning Note 12_php Tutorial

Source: Internet
Author: User
Tags arithmetic operators bitwise operators comparison table constant definition scalar

PHP Basic Learning Notes (12), PHP Basic Learning Notes


PHP Basic Syntax

 
  PHP    // This is PHP single    -line Comment/ *         This is    a multiline comment *    ///  Each PHP statement ends with a semicolon--must be used!     // PHP is a case-sensitive language-except    for the name of the function // any one of the variable names in PHP will start with a $ character. ?>

Variables and constants

In PHP, variables start with a $ sign. Variables are usually directly assigned (this is the definition if the first assignment).

Constants: Sets a data identifier (data name) whose value does not and should not change, such as the "PI" in mathematics.

$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 assignment

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

The difference between a constant and a variable:

    • No dollar sign ($) before constant
    • Constants can only be defined with the Define () function and cannot be assigned by an assignment statement
    • Constants can be defined and accessed anywhere without regard to the rules of variable scope
    • Constants cannot be redefined or undefined once defined
    • The value of a constant can only be a scalar (that is, an integer, a floating-point number, a string, Boolean)

Data type:

Base type (scalar type):

Boolean type (Bool,boolean): Only two data: true false

Character type (string): Can be single or double quotes, or "delimiter" format

Integer type (int,integer):

$v 1 = 10;

$v 2 = 012; A number starting at 0 represents the "8 binary" integer, which is equivalent to 10 binary 10

$v 3 = 0x1F; A number starting at 0x represents a 16-binary integer, which is equivalent to 10 binary 31

Floating-point type (float,double)

$v 4 = 12.34;

$v 5 = 12.34e5; 12.34 Times 10 of the 5 squared

Composite type:

Array type:

Object type:

Special types:

Resource type: Represents a type of "external data".

Empty type (NULL): Only one data, which is "null"

3 different types of strings:

" Abc\ndefg1 ";    // The following escape characters can be recognized in a double-quote string: \ \ \ \ \ \ \ \  \$'a\bc\ndefg2\\' ;        // only the following escape characters are recognized in a single quote string: \ \  \. where "\ \" is usually used only for the last character // The following is a delimiter string, where "ABCD" is a symbol that is arbitrarily named. The string is also used to end the symbol.   The following escape characters are recognized in the//delimiter string: \ n \ r \ \ \  \$$str 3 = <<<  Abcdabc\nde"F ' G3 This is called the delimiter string this range of any content is belong to str3 this string of content wrapping can also be changed directly can also write HTML and JS and so on ... For example:
 
  ABCD; // Special Note: The Terminator of the preceding line delimiter string can only appear with the name itself and a semicolon, and cannot have any other content, such as spaces, indents (tab characters) // Note: In a double-quote string and delimiter string, you can directly identify the variable and fill its contents with the value of that variable.  // If you want to not be recognized, you need to use "\$" to escape the $ symbol    5;     " $i = " . $i;

Pass-through Value method:

Value Pass (Assignment value): Copies the value of one variable and assigns it to another variable, at which time the values of the two variables are equal (the same), but two variables are completely independent of the variables that are not associated.

By default, in PHP, simple data types and arrays, and null types use values to pass.

Reference passing (reference pass-through): Copies one copy of the "address" of a variable and assigns it to another variable, which is equivalent to two variables pointing (corresponding) to an identical address--this address also represents the location of the data. At this point the values of the two variables are equal, which means that the two variables actually refer to a common piece of data.

By default, in PHP, object types and resource types are passed using references.

But:

We can also use reference passing values for data that is passed by default using assignments. The syntax is:

Variable 1 = & variable 2;

Example:

$v 1 = 10;

$v 2 = & $v 1; $v 1 and $v2 represent "one data content" at this time

$v 1++;

echo $v 2;

Compare JS:

JS, only the default mode of transmission, there is no artificial way to set

JS: The default values are passed in the following types: numbers, characters, Booleans, two special types

The types of default reference values are: Array, object

Operator:

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

    • The + number does only "arithmetic operations" and has no double meanings.
    • + +--the symbolic meaning and usage are exactly the same as JS.

L Write before variable, then: Add (Subtract) first, then do other operations (such as assignment)

L write in the variable, then: do other operations (such as assignment), and then add (self-subtract)

    • The% number only takes and operations on integers, and if they are not integers, they are automatically converted to integers and then to the remainder.

L comparison operators: = = = = > >= < <= = = = (all equals)!== (not all equals)

    • = = usually denotes "fuzzy equality", = = = = Exact equality (only data types and data values are equal)
    • For various cases of fuzzy equivalence, please check the manual: Appendix Type Comparison Table

L conditional Operator: (EXPR1)? (EXPR2): (EXPR3), for example:

    • Meaning: Evaluate the expression exp1, judge the result if true, the result of the whole expression is exp2, otherwise the result of the whole expression is EXP3
    • $v 1 = $fs >60? "Pass": "Retake"; V1 There are two conditions depending on the value of FS
    • Supplemental knowledge: Unary operators are:!, + +,--, ~, the two-dollar operator is the operator that we use the most, the ternary operator on this one.

L logical operator:&& (with) | | (OR)! Non-

    • Logic and &&: Only two data is true, the result is true
    • Logical OR | | : As long as a data is true, the result is true
    • Logical Non! : Takes the opposite value of the logical data.

L string operator:. (i.e. "+ ="). = (i.e. "+ =")

    • . = Just a "shorthand", $v 1 = $v 1. "ABC"; È $v 1. = "ABC";

L-bit operator:& (with) | (or) ~ (non) ^ (XOR) << (shift left) >> (shift right)

    • Bitwise operators are based on the binary of numbers.
    • Bitwise AND &: Only two digits on the corresponding digits are 1, and the result is 1.
    • Bitwise OR |: As long as there is a number on the corresponding bit is 1, the result is 1
    • Bitwise NON ~: Reverse, that is, 1 becomes 0, 0 becomes 1
    • Bitwise XOR ^: two digits on the corresponding digits are different when the result is 1.

L Assignment Operator: = + = = *=/=%=. =

Operator precedence problem: basic with JS.

Data type conversions

In JS, there is basically no concept of data type conversion, the actual conversion is "automatic"-The default conversion.

JS has two "similar" type Conversion functions: parseint (...) parsefloat (...)

In PHP, there are both default and cast conversions.

Default Conversion Example:

Ten -"3";        // 7;    Ten + "3";        // ;     + "3ABC";    // ;     + "3. 5abc";    // 13.5;    Ten + "ABC3";    // Ten;    Ten + "abc";    // Ten    $v 5 = "ten" + "abc";    // Ten    $v 5 = "10ABC" + "5ABC";    //  the    $v 5 = "ABC10" + "ABC5";    // 0    $v 5 = "abc" + "abc";    // 0

... when you use arithmetic operators to perform operations, you will "look at" numbers, and if they do not turn out to be a number, they are calculated as a sum of.

cast :

Syntax: Data (target type to be converted);    example:    = (int) "3. 5abc";    // 3;int type    $v 1 = (float) "3. 5abc";    // 3.5;     // This is actually a float type    $v 1 = (string3;        // "3";    $v 1 = (bool) "3.5";        // true

--particularly recommended when converting a data to a bool type: Check the Manual: Appendix type Comparison table) compare variable $x 〉if ($x) column

http://www.bkjia.com/PHPjc/973827.html www.bkjia.com true http://www.bkjia.com/PHPjc/973827.html techarticle PHP Basic Learning Notes (three), PHP base Learning notes PHP fundamental syntax? PHP//This is a PHP single-line Comment/* This is a multiline comment *///each PHP statement using the semicolon end must make ...

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