PHP variables, data types, and scopes for the front-end, php Data Type _ PHP Tutorial

Source: Internet
Author: User
Variables, data types, and scopes of PHP for the frontend. Frontend PHP variables, data types, and scopes, php Data type Directory [1] variable definition variable assignment [2] data type string integer floating-point Boolean array object NUL frontend PHP variable, data type and scope, php data type
Directory [1] variable definition variable assignment [2] data type string integer floating point number Boolean array object NULL resource [3] Scope global static variable definition

A variable is the container that stores information. it starts with the $ symbol and is followed by the variable name. The variable name must start with a letter or underline and is case sensitive.

 

Variable assignment

PHP does not have a command to create a variable. the variable will be created when it is assigned a value for the first time.

 

Data type

PHP is a loose language that does not need to tell the data type of the PHP variable. PHP automatically converts the variable to the correct data type based on its value. PHP Data types include eight: four scalar types, two composite types, and two special types. Specifically: string, integer, floating point number, Boolean, array, object, NULL, resource

[Note] var_dump () returns the data type and value of the variable.

String

A string can be any text in a pair of quotation marks, or a pair of single or double quotation marks.

 "; $x = 'Hello world!';//Hello world!echo $x;?>

<情况1>When a string contains quotation marks, there are three solutions:

[1] double quotation marks embedded in single quotes

[2] embedding single quotes in double quotes

[3] use the escape character "\"

 ";echo $str_string2;echo "
";echo $str_string3;?>

<情况2>When the string quotes encounter variables, there are two situations:

[1] When double quotation marks contain variables, the variables are connected with the content in double quotation marks.

[2] When single quotes contain variables, the variables are output as strings.

 ";echo $str_string2;//1echo "
";echo $str_string3;//"$test"echo "
";echo $str_string4;//'1'?>

<情况3>When the string is very long, the Heredoc structure is used. the delimiter first represents the string (<), and then an identifier (any name) is provided after (<), after a line break, it is a string and ends with this identifier. Note that no extra spaces are left or right of the identifier.

 

Integer

PHP integers must have at least one number, and cannot contain commas or spaces. they cannot have decimal places, plus or minus digits. three formats can be used to specify integers: decimal and hexadecimal (prefix: 0x) or octal (prefix: 0)

 "; $x = -345; // int(-345)var_dump($x);echo "
"; $x = 0x11; // int(17)var_dump($x);echo "
";$x = 011; // int(9)var_dump($x);?>

Floating point number

PHP floating point number is a number with a decimal point or an exponential form

 "; $x = 2.4e3;//float(2400)var_dump($x);echo "
"; $x = 8E-1;//float(0.8)var_dump($x);?>

Boolean

PHP Boolean has only two values: true or false (case-insensitive). it is often used for conditional testing. When the echo command is used to output a Boolean type, if it is true, "1" and "false" are output, and nothing is output.

 "; $ Flag = $ man =" "; // echo $ flag; var_dump ($ flag); // bool (false)?>

Array

PHP array can store multiple values in one variable

  string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(4) "SAAB" }var_dump($cars);?>

Object

PHP Objects are data types that store data and information about how to process data. In PHP, objects must be explicitly declared, but classes of objects must be declared first. In this regard, class keywords are used. classes are structures that contain attributes and methods. Then define the data type in the object class, and then use this data type in the instance of this class

 color = $color;    }    function what_color() {      return $this->color;    }}function print_vars($obj) {   foreach (get_object_vars($obj) as $prop => $val) {     echo "\t$prop = $val\n";   }}$herbie = new Car("white");echo "\herbie: Properties\n";print_vars($herbie);?>

NULL

The NULL type in PHP is NULL and case insensitive. The NULL type has only one value, indicating that a variable has no value. if it is assigned NULL or has not been assigned a value, or unset (). in these three cases, the variable is considered NULL.

 

Resources

PHP resources are built and used by specialized functions, such as opening files, data connections, and drawing canvases. You can create, use, and release resources ). Any resources should be released in time when they are not needed. If we forget to release resources, the system will automatically enable the garbage collection mechanism to recycle resources after the page is executed to avoid memory consumption.

 "; // Line feed} fclose ($ file_handle); // close the file?>

[Note] memory_get_usage () obtains the memory consumed by the current PHP, measured in bytes.

 ";$var_string = '123';//320echo $m2 = memory_get_usage()-$m1; echo "
";$n = 123;//272echo $m3 = memory_get_usage()-$m1-$m2; echo "
";$f = 123.00;//272echo $m4 = memory_get_usage()-$m1-$m2-$m3; echo "
";$var_array = array('123');//576echo $m5 = memory_get_usage()-$m1-$m2-$m3-$m4; ?>
 "; $ String = 9494; var_dump ($ string); // int (9494) echo"
";?>

Scope

PHP has three different variable scopes: local, global, static)

Variables declared outside the function have a global scope and can only be accessed outside the function. variables declared inside the function have a local scope and can only be accessed inside the function.

 Internal variables of the test function:

"; Echo" variable x is: $ x
"; Echo" variable y: $ y ";} // no output // 10 myTest (); echo"

Variables other than the test function:

"; Echo" variable x is: $ x
"; // 5 echo" variable y: $ y "; // no output?>

Global keywords

Used to access global variables in a function

 

PHP also stores all global variables in the array named GLOBALS [index], and the subscript contains the variable name. This array can also be accessed in the function and can be used to directly update global variables.

 

Static keywords

Generally, after a function is completed or executed, all variables are deleted. However, you do not need to delete a local variable. To accomplish this, you need to use the static keyword when declaring the variable for the first time. Every time a function is called, the information stored in this variable is the information contained in the last call of the function. However, note that this variable is still a local variable of the function.

 ";myTest();//1echo "
";myTest();//2echo "
";myTest();//3echo "
";myTest();//4?>

Values Directory [1] variable definition variable assignment [2] data type string integer floating point number Boolean array object NUL...

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.