Introduction to PHP variables

Source: Internet
Author: User

1.1: Type

There are 8 types of variables, which do not need to be memorized to deepen understanding in practical use.

1) integer [integer] math integers 2) floating-point [float,double] Math decimal 3) string [string] a string of characters 4) Boolean [Boolean] True or false 5) array [array] key value pair composite data 6) Object [Object ] [learned in object-oriented] 7) NULL No value 8) resource [resource] "eyedropper"

What is NULL? The null type only labels its type as null
The field of its value is empty, and null is not a value

$a = 3;//integral type $b = 3.14;//float $c = null;//null type $d = ' hello ';//String $e = true;//Boolean echo $D;

Naming conventions for variable names:
[A-za-z0-9] and underscore (_)
1) Variable names are case-sensitive
2) cannot start with a number

1.2: Variable Detection

echo a non-existent variable that will report the notice level of error,
So check to see if this variable exists.
How do I detect if a variable exists?
isset-detect if the variable is set
The declared variable returns True, and the undeclared variable returns false.
Detect if a variable exists: Just look for the name of the variable in the roster

$b = null; $c = false; $d = 0; $e = ";//detect if the above variable exists if (Isset ($a)) {  echo ' variable B exists ';} else{  echo ' variable B does not exist ';}

For a variable with a value of NULL, it also returns FALSE, because NULL has no value that has not been declared and of course does not exist

1.3: Type Detection

To detect a variable, PHP is to store it as a type of variable type for PHP to get it is very simple because the box has stored its variable type
Gettype-gets the type of the variable [off-the-shelf system functions]

$a = False;echo GetType ($a); $b = "1"; Echo GetType ($b); $c = 1.11;echo GetType ($c); $d = ' Hello '; echo GetType ($d); $e = Null;e Cho GetType ($e);

Determine if a variable is of a certain type

Is_float () [is_double] detects if the variable is a floating-point is_int () [Is_integer] detects whether the variable is an integer is_string () The detection variable is a string Is_object () detects whether the variable is an object Is_array ( Detects if the variable is an array of Is_resource to detect if the variable is a resource type Is_bool the detection variable is a Boolean is_null the detection variable is null$a = ' Hello '; if (is_string ($a)) {  echo ' A is a string '; }else{  Echo ' A is not a string ';}

1.4: Debug Print variable

echo String, numeric
Print_r print hierarchical data, such as arrays, objects
Var_dump type and value of the print variable [debugging code is easier]

$a = ' Hello '; $b = Array (1, 2, "3"); $c = false; $d = null; $e =18; $f = true;//boolean true prints out 1,false and null nothing shows echo $a, $b, $c, $d, $e, $f, ' 

1.5: Type Conversion

PHP, the type of variable can be converted at any time, very flexible the most common is the conversion between the word representable and the number, or the number/string---Boolean value conversion

string-to-number conversions
Interception from left to right, straight to the illegal number, the intercepted part converted to a number

$a = ' 12 '; $b = $a +3; Var_dump ($b); $a = ' 12.5hello '; $a = ' 12.5hello99 '; $b = $a +3;var_dump ($b); $a = ' Word12.5hello '; $b = $a +3; Var_dump ($b);

Conversion of numbers to strings

$a = 123; $b = $a. ' Hello '; var_dump ($b);

Number/string/array wait for conversion of Boolean values

$b = 3; if ($b) {echo ' B is true ';} else{    Echo ' B is false ';}

If the value of the Boolean type should be judged, then the number 3 is turned into a Boolean to understand whether it should be understood as true or false?
The following values, which are interpreted as Boolean values of False
', ' 0 ', 0,0.0,false,null,array (); And the other values are treated as Boolean true

if (' = = False ') {echo ' empty string is false ';}

Empty (Var)-Checks if a variable is empty
Empty () returns FALSE if Var is a non-null or nonzero value
In other words, "", 0, 0.0, "0", NULL, FALSE, Array (); and objects that do not have any properties will be considered empty and return TRUE if VAR is null

$arr = Array (), if (Empty ($arr)) {echo ' variable is empty ';}

1.6: Assign Value

There are two ways of assigning values:
1. Value assignment; (two people watch two TVs with one station)
2. Reference assignment; (Two people watching the same TV)
1. Transfer value Assignment
The variable name is not actually affixed to the box, but there is a variable table (like the class roster) variable value and variable type placed in the box; the variable name in the variable table points to the box it corresponds to.

$li = $wang = $li; echo $li, ' ~ ', $wang;

Change the value of the $li, will the value of the $wang be transformed?

$li = 99;echo $li, ' ~ ', $wang;

The assignment process is to assign the value of the $li to the $wang
2. Reference Assignment

$a = ' TVB '; $b = & $a; $a, $b together point to the same value echo $a, ' ~ ', $b;

Change the value of $ A

$a = ' BTV '; echo $a, ' ~ ', $b;

1.7: Destruction

Why do you want to destroy variables?
Because, sometimes compared to the large array, or the larger object special GD drawing, compared to the cost of resources, it unset off, you can release the memory in time
unset (variable name); Destroying the specified variable first removes the variable name from the variable table (roster) and then finds the corresponding box to delete it.

$a = 99; unset ($a); if (Isset ($a)) {echo ' a exists ';} else{  Echo ' A does not exist ';}

Note: Reference assignment, if two variables point to the same box, the box cannot be destroyed when one of the variables is destroyed.

$a =99; $b = & $a unset ($a); Echo $a, $b;//Report a notice error

Re-assigns a new value to $ A

$a = 18; echo $a, $b;

1.8: Dynamic Variable name

A dynamic variable name that can represent a very flexible place for PHP
Use the value of the variable to make the name of the other variable

$laoda = ' Liubei '; Echo $laoda, ' <br > '; $paihang = ' Laoda '; echo $paihang, ' ~ ', $ $paihang;//ranked $rank = ' Paihang '; echo $$ $rank;
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.