PHP--php variables and constants for learning notes

Source: Internet
Author: User
Tags constant definition scalar variable scope
PHP Variables and constants

First, Directory

First chapter variables

1.1 Declaration of variables

1.2 Naming of variables

1.3 The data type of the variable

1.4 Conversion between variable data types

1.5 Some common functions related to variables and types

Chapter II Constants

2.1 Declaration and use of constants

2.1 Pre-defined constants and magic constants

First chapter variables

1.1 Declaration of variables

1. The variable is defined in $ and consists of $ and identifier, which is the name of the variable. (the names of functions and classes are also identifiers)

$ Variable name = value

1.2 Naming of variables

1. Be sure to use "$" before the variable, declare and use this symbol.

2. Can be any length, there are any letters, numbers, underscores, but cannot start with a number

3. You cannot use PHP's operation symbol +-*/% &.

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

5. PHP variables are case-sensitive (only variables and constant l are case-sensitive, others are not)

6. Variable names must be meaningful, you can use English words, you can also use Hanyu Pinyin.

7. $AAABBBCCC the name of the variable style, hump method.

Note: mutable variables (variable names for a variable can be set and used dynamically)

Variable reference assignment (use a "&" symbol to precede the variable that will be assigned (source variable))

Arrays and classes use PHP's variable name for attention

You can add &amp to a variable in front of it, as in C + +, for example: $a = & $b to pass a value on a variable.

1.3 The data type of the variable

1.PHP is a weakly typed language, and the type of the variable is determined by the stored value

2.PHP There are 8 types of CCP

Four types of scalar:

Integral type: int integer

Boolean: BOOL Boolean

Float type: float, double, real

Strings: String

Two kinds of composite types

Arrays: Array

Objects: Object

Two special types of

Resource type: Resource

NULL type: null

Note:

Assign a value of 0123 to a variable, but the value of the output variable is always a different number, what is the problem?
The PHP interpreter will take a number starting at 0 as an octal, so its value will become an octet.

$a = 0.2+0.7, $b = 0.9;var_dump ($a = = $b), and the result is: BOOL (false). That is to say, the result of 0.2+0.7 here is not equal to 0.9. Can you tell me how to solve this problem?

The official PHP manual explains: Obviously simple decimal fractions 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 fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333 .... We print the above variables in double-precision format: $a = 0.2+0.7; $b = 0.9;printf ("%0.20f", $a); Echo '
';p rintf ("%0.20f", $b);

The output results are as follows:

0.899999999999999911180.90000000000000002220

Obviously here, as a floating-point data, its accuracy has been lost in part, not fully accurate. So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. It should be explained that this is not a problem with PHP, but the internal processing of floating-point number of the computer problem! In C, JAVA and other languages will also encounter the same problem.

Therefore, to compare two floating-point numbers, you need to control them within the range of precision we need, so we use the Bcadd () function to add and make precision conversions (for strings) of floating-point numbers:

Var_dump (Bcadd (0.2,0.7,1) = = 0.9); Output: bool (true) rounding of floating-point numbers

In the article "PHP takes the whole function ceil and floor", there are examples:

In the face of the calculation of floating point number, we know that this is not exactly the result of floating point calculation results:

After the discussion of floating-point number calculation, we know that this is caused by the incomplete calculation result of floating-point number, so we can use the round () function to deal with it:

Although the round () function is rounded with the specified precision, leaving one digit after the decimal point has no effect on our rounding results.

1.4 Conversion between variable data types

One is the cast:
1.setType (variable, type); This function changes the type of the original variable
2. $a = (int) "123ABC"; Use (type) in the form of a value before it is assigned, without altering the type of the original variable
3.$ variable =intval (variable or value); form a new variable, the original variable is unchanged
Attention:
Conversion of Model 1.bool
2. Integer type in memory 4 bytes 32 bits max 2.147e9
   
    4. Overflow/precision loss when converting between integral type and floating point type
5. Conversions between string and integer and floating-point types
One is automatic conversion:
The most common way, because we do not need to manage the type of development, the variables will be automatically converted according to the operating environment

Note In data type conversions in 4.PHP

$str = "100.123ABC";
SetType ($STR, BOOL);
Var_dump ($STR);

Integer 4 bytes 2 words 32 bit 2.147e9
Floating-point type is 8 bytes, 4 characters, 64 bits

$float 1=123.456;
$float 2=2.147e9;
$int 1= (int) $float 1;
$int 2= (int) $float 3;
Var_dump ($int 1);
Var_dump ($int 2);
(int) (10* (0.7+0.1))//result is 7
--------
$amount = 19.99 * 100;
printf ("%.13f", $amount);
---------
$num =-1000;
Print ($num. " \ n ");
$i _str= sprintf ("%u", $num);
Print ($i _str. " \ n ");
$i 1= intval ($i _str);
Print ($i 1. " \ n ");
$i 2= intval (floatval ($i _str));

Print ($i 2. " \ n ");

Output results
-1000
4294966296
2147483647
-1000
Integral type = floating point note: overflow/precision Loss
5. String conversion to integer and floating-point

$a = "100ABC"; $a = "A100BC";
$a = "ABC"; $a = "100.123ZBC"
$a = "100EABC"; $a = "100E5ABC"

6. Automatic variable conversion

$a = 10;
$b = "100ABC";
$c = true;
$d = 12.34;
$sum = $a + $b + $c + $d;

Var_dump ($sum);


1.5 Some common functions related to variables and types

Isset (); Tests whether a variable exists, the value returned if NULL, also indicates an empty

Empty (); Determines whether a variable is empty, "" 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 II Constants

2.1 Declaration and use of constants

1. A constant is an identifier of a simple value 2. The constant definition can no longer change his value or use unset () to cancel 3. Constants can be defined and accessed anywhere by ignoring the rules of variable scope 4. Declare constants using define ("Constant name", value); 5. Constant declaration names do not use "$" for both declaration and use 6. The constant name habit uses uppercase 7. The value of a constant can only be used with a scalar type (int, float, bool, string) 8. Constants must be given a value of 9.defined ("constant") at the time of Declaration; See if a constant exists

2.2 Pre-defined constants and magic constants

$_get[];

$_post[];

$_request[];

$_cookie[];

$_session[];

$_files[]; Get 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.