Php learning notes-php variables and constants

Source: Internet
Author: User
Php learning notes-php variables and constants

I. Directory

Chapter 1 variables

1.1 Variable Declaration

1.2 variable name

1.3 Data type of the variable

1.4 Conversion between variable data types

1.5 Some common functions related to variables and types

Chapter 2 constants

2.1 Declaration and use of constants

2.1 predefined constants and magic constants

Chapter 1 variables

1.1 Variable Declaration

1. a variable starts with $ and consists of $ and identifier. the identifier is the name of the variable. (The names of functions and classes are also Identifiers)

$ Variable name = value

1.2 variable name

1. you must use "$" before the variable. this symbol must be included in the declaration and use.

2. it can be of any length and may contain any letters, numbers, and underscores. it cannot start with a number.

3. you cannot use the PHP operator number +-*/% &.

4. PHP can use the system keyword as the variable name

5. PHP variables are case sensitive. (only variables and l-grams are case sensitive. Others are not case sensitive)

6. the variable name must be meaningful. you can use English words or Chinese pinyin.

7. $ aaaBbbCcc variable naming style, hump method.

Note: variable (variable names can be dynamically set and used)

Assign values to variables by reference (use the "&" symbol before the variable to be assigned (source variable ))

Notes on using PHP variable names for arrays and classes

Like C ++, you can add & before the variable, for example, $ a = & $ B to pass the value of a variable.

1.3 Data type of the variable

1. PHP is a weak language, and the variable type is determined by the stored value.

2. PHP has eight types

Four scalar types:

Integer: int integer

Boolean: bool boolean

Float, double, real

String: string

Two composite types

Array: array

Object

Two special types

Resource Type: resource

Null type: null

Note:

Assign a value of 0123 to a variable, but the value of the output variable is always another number. what is the problem?
The PHP interpreter regards the number starting with 0 as octal, so its value is changed to octal.

$ A = 0.2 + 0.7; $ B = 0.9; var_dump ($ a = $ B); the result is: bool (false ). That is to say, the calculation result of 0.2 + 0.7 is not the same as that of 0.9. How can this problem be solved?

PHP official manual description: Obviously, a simple decimal score such as 0.2 cannot be converted to an internal binary format without losing a little bit of precision. This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example, decimal 1/3 is changed to 0. 3333333 .... We print the above variables in double precision format: $ a = 0.2 + 0.7; $ B = 0.9; printf ("% 0.20f", $ a); echo'
'; Printf ("% 0.20f", $ B );

The output result is as follows:

0.899999999999999911180.90000000000000002220

Obviously, as a floating point data, its accuracy has lost some of it, and it cannot be completely accurate. Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. It should be noted that this is not a PHP issue, but a problem of floating point processing inside the computer! The same problem occurs in C, JAVA, and other languages.

Therefore, to compare two floating point numbers, we need to control them within the range of precision we need and then compare them. Therefore, we use the bcadd () function to add and convert the floating point numbers (as strings ):

Var_dump (bcadd (0.2, 0.7, 1) = 0.9); // output: bool (true) floating point integer

In the article "ceil and floor of the entire function in PHP", there were examples:

After the discussion of floating point number calculation, we know that this is caused by the incomplete accuracy of floating point calculation results:

After the discussion of floating point number calculation, we know that this is caused by the incomplete accuracy of floating point calculation results. so we can use the round () function to process it:

Although the round () function is rounded to the nearest decimal point based on the specified precision, it does not affect the result.

1.4 Conversion between variable data types

One is forced conversion:
1. setType (variable, type); // This function changes the type of the original variable
2. $ a = (int) "123abc"; // use the (type) format before the value assignment, without changing the type of the original variable
3. $ variable = intval (variable or value); // form a new variable. the original variable remains unchanged.
Note:
1. bool type conversion
2. the integer occupies 4 bytes in the memory, and the maximum value is 2.147e9.
3. the floating point type occupies 8 bytes and 64 bits in the memory.
4. overflow/loss of precision during conversion between integer and floating point types
5. conversion between string and integer and floating point
One is automatic conversion:
This is the most common method, because we do not need to manage the type during development. variables are automatically converted according to the running environment.

4. notes for data type conversion in PHP

$ Str = "100.123abc ";
SetType ($ str, bool );
Var_dump ($ str );


Integer: 4 bytes, 2 characters, 32 characters, 2.147e9
Float: 8 bytes, 4 characters, 64-bit

$ Float1 = 123.456;
$ Float2 = 2.147e9;
$ Int1 = (int) $ float1;
$ Int2 = (int) $ float3;
Var_dump ($ int1 );
Var_dump ($ int2 );
(Int) (10*(0.7 + 0.1) // The result is 7.
--------
$ Amount = 19.99*100;
Printf ("%. 13f", $ amount );
---------
$ Num=-1000;
Print ($ num. "\ n ");
$ I _str = sprintf ("% u", $ num );
Print ($ I _str. "\ n ");
$ I1 = intval ($ I _str );
Print ($ i1. "\ n ");
$ I2 = intval (floatval ($ I _str ));

Print ($ i2. "\ n ");

Output result
-1000
4294966296
2147483647
-1000
Integer = float type Note: Overflow/precision loss
5. string conversion to integer and floating point

$ A = "100abc"; $ a = "a100bc ";
$ A = "abc"; $ a = "100.123zbc"
$ A = "100 eabc"; $ a = "100e5abc"

6. Automatic variable conversion

$ A = 10;
$ B = "100abc ";
$ C = true;
USD d = 12.34;
$ Sum = $ a + $ B + $ c + $ d;

Var_dump ($ sum );


1.5 Some common functions related to variables and types

Isset (); // test whether a variable exists. if the returned value is null, it also indicates null.

Empty (); // determines whether a variable is null and "" null

Unset ();

SetType ();

GetType ();

Var_dump ();

Variable type test function:

Is_bool ()

Is_int () is_integer () is_long ()

Is_string ()

Is_float () is_double () is_real ()

Is_array ()

Is_object ()

Is_resource ()

Is_null ()

Is_scalar ()

Is_numberic ()

Is_callable ()

Chapter 2 constants

2.1 Declaration and use of constants

1. constant is a simple value identifier 2. after a constant is defined, its value cannot be changed, or unset () can be used to cancel 3. constants can be defined and accessed anywhere regardless of the variable range rules. declare a constant using define ("constant name", value); 5. "$" 6. constant names are used in uppercase 7. constant values can only be of the scalar type (int, float, bool, string) 8. A constant must be given a value of 9 at the time of declaration. defined ("constant"); // check whether a constant exists

2.2 predefined constants and magic constants

$ _ GET [];

$ _ POST [];

$ _ REQUEST [];

$ _ COOKIE [];

$ _ SESSION [];

$ _ FILES []; // Obtain the upload form data

$ _ SERVER [];

$ _ ENV [];

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.