PHP syntax, String type?

Source: Internet
Author: User
PHP syntax, there is no rule: when defining a string, the string must be enclosed in single quotation marks, double quotes or no symbols.

Such as:
$x = 123;
$y = "123";
$z = ' 123 ';

These 3 variables, are they not the same?


Reply to discussion (solution)

The first is the same as the second of the integer type, which is the string type.

But a string of pure numbers can sometimes be used as numbers in PHP.

For example $x = "123";
echo $x;
The result is 369.

You can use the GetType () function to check the type

$x = 123;
This statement assigns the shape to the $x

$x = "123"; and $x= ' 123 ' is the same as the result of the assignment operation.
The string "123" is assigned to the $x string type.

For explanations of single and double quotation marks, see this post
http://bbs.csdn.net/topics/390535456

In the absence of any symbols, how does PHP determine what type it is?
$x = 123;
$x =abc;
Are they the same type?

$x = 123;
No objection, the balance will be treated as an integer
Echo GetType ($x); Integer
Var_dump ($x); Int (123)

$x = ABC;
PHP first check whether this is a constant, if it is the value of the constant ABC
Otherwise warn Notice>: Use of undefined constant abc-assumed ' abc '
Then interpreted as a string
Echo GetType ($x); String
Var_dump ($x); String (3) "ABC"

See Xu Big also use pinyin Input method immediately very happy

$x = 123;
No objection, the balance will be treated as an integer
Echo GetType ($x); Integer
Var_dump ($x); Int (123)

$x = ABC;
PHP first check whether this is a constant, if it is the value of the constant ABC
Otherwise warn Notice>: Use of undefined constant abc-assumed ' abc '
Then interpreted as a string
Echo GetType ($x); String
Var_dump ($x); String (3) "ABC"

$x = 123; Automatic type conversion to: integral type

$x = ABC; Automatic type conversion to: string

Right?

I've made it very clear.

I've made it very clear.

However, the view is not very clear!

If you have to follow the "non-white and black" mindset to understand, it really does not understand

The process of identifying PHP is like this
Is it a number? Is the end of assignment
Is it a constant? Is the end of assignment
That's like a string.

PHP is used to writing character type is single quotation mark, variable value is double quotation mark

First of all, you write the source code is a text character, when PHP to analyze the source code, will follow the character of the law to identify the corresponding data format.

$x = 123; 1
$y = "123"; 2
$z = ' 123 '; 3

For PHP Analysis:
1 will be interpreted as an assignment statement, because there is = this text character, then the left $x is a variable identifier, the right is "123" consecutive 3 text characters.
Detects that the first text character is a number, and uses the data format of int to match the subsequent text character (simply, the continuous number consists of a regular representation of \d+), and when it is found to match, the variable is of type int.

2 the same $y and "123" "(2" characters contain contiguous text), then detects that the first is ", the beginning of a double quotation mark rule that conforms to the string data format, then the double quotation mark rule (which is simply the beginning is", the end is ", and there is no" or "preceded by \, the regular is represented as" ^ "]|\\") + ") match, found to match on the string type

This is the logic of PHP to identify the source code, using a scientific name is called a parser, after the parser, you will know what type of variable.

PHP is a weakly typed language, and variables do not require a declaration type.

First of all, you write the source code is a text character, when PHP to analyze the source code, will follow the character of the law to identify the corresponding data format.

$x = 123; 1
$y = "123"; 2
$z = ' 123 '; 3

For PHP Analysis:
1 will be interpreted as an assignment statement, because there is = this text character, then the left $x is a variable identifier, the right is "123" consecutive 3 text characters.
Detects that the first text character is a number, and uses the data format of int to match the subsequent text character (simply, the continuous number consists of a regular representation of \d+), and when it is found to match, the variable is of type int.

2 the same $y and "123" "(2" characters contain contiguous text), then detects that the first is ", the beginning of a double quotation mark rule that conforms to the string data format, then the double quotation mark rule (which is simply the beginning is", the end is ", and there is no" or "preceded by \, the regular is represented as" ^ "]|\\") + ") match, found to match on the string type

This is the logic of PHP to identify the source code, using a scientific name is called a parser, after the parser, you will know what type of variable.

How do you analyze the third one?

PHP syntax specifies that strings must be double or single quotes, and the contents can be numbers, letters, and characters. Right?

Such as:
$x = ' a*12 '; Right
$x = "a*12"; Right
$x =a*12; Syntax error.

  • Related Article

    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.