PHP Basics: Variables, operators, control structures

Source: Internet
Author: User
Tags php basics

Variable definition: The variable is derived from mathematics. is an abstract concept that can store computational results or represent values in a computer language. That is, when the program is running, its value is the amount allowed to change.

Naming conventions for variables: variable names are made up of A to Z, A to Z, number 0-9, and _, and are case-sensitive and cannot begin with a number.

There are two assignment values and reference assignments for variable assignment.

1. Value assignment, value assignment is when the value of an expression is assigned to a variable, the new variable in memory pointer points to the value of the original variable, when the value of one variable is assigned to another variable, change the value of one of the variables, will not affect the other variable. such as $a = 2; (assigns the value 2 to the right of the equal sign to the value $ A to the left of the equals sign)

2. Reference assignment, the reference assignment is the new variable in memory pointer to the original variable in memory store address, arbitrarily change the value of one of the variables, the other variable value will also change, such as ($a = 8; $b = & $a;)

Example is: If 2 people in TV, value assignment is 2 people in 2 TV to see the same station, a person changed the platform, does not affect the other person to look at the front of the station. Reference assignment is 2 people watching the same TV, a person to change the station, 2 people have to see the new station.

There are 8 types of variables, and in-memory variable values do not only store variable values, but also store their variable types

1. Integers in integer (integer) mathematics such as 1,789;

2 string type (string) A string of words such as ' adbcdf ';

3 floating-point (float,double) mathematics with decimals such as ' 3.115926 ';

4 Boolean (Boolean) True and false false and true;

5.null there is no value value that labels the field whose value is null, it is empty, and Null does not have a value.

6 array key value pairs conform to data such as $ A = Array (name=> ' Zhansan ',age=> ' 23 ');

7. An Object object is a data type that stores data and information about how the data is processed. objects are composed of 2 parts properties and methods

8 Resource (Resource) is a readable and writable set of data

Detection of variables:

echo a non-existent variable, will be reported notice (note, warning) error, so to detect this variable is not there;

1.isset () function--detects whether a variable is set (declared), a variable that has already been declared, and a variable with a value that is not NULL returns true otherwise all returns False if multiple variables are detected, each individual meeting requirement returns true, otherwise the result is false,isset () function can only be used to detect variables, passing any other argument will parse the error,

Detects if a constant is set (declared) can be used with the defined () function

2.empty () function--detects if a variable is empty, returns TRUE if the variable does not exist, if the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties, the TURE is returned.
If the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var; And an object that does not have any properties, it returns FALSE. Empty () can only be used for variables, and passing any other parameter will cause Paser error and terminate the run.

The following values are understood as pseudo-Boolean values, ' 0 ', 0,0.0,false,null,array (); And the other values are treated as Boolean true

Detection of variable types:

Detect a variable, PHP is to store it as a kind of what kind of type

GetType ()--Gets the type of the variable but try not to use GetType () to test a type, because the string it returns may need to be changed in a future release. In addition, it is slower to run because it contains a comparison of strings. Use the is_* function instead.

is_float () [is_ Double detection variable is floating point
is_int () [is_integer] Detect if the variable is integer

Is_string() detects if a variable is a string
Is_object() detects if the variable is an object
Is_array() detects if a variable is an array
Is_resource Detecting If a variable is a resource type
Is_bool Whether the detection variable is a Boolean type
Is_null Detecting if the variable is null

To debug a print variable:

When developing the program, it is unavoidable to print the value of the variable

echo Print string and numeric boolean true to print out 1,false and null nothing is displayed

Print_r print hierarchical data, such as objects and arrays do not print Boolean values with Echo and Print_r because it interferes with the var_dump Print Boolean and null

Var_dump type and value of the print variable (debugging code is convenient)

Type conversions

PHP Basics: Variables, operators, control structures

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.