Front-end PHP variables, data types and scopes, PHP data type _php Tutorial

Source: Internet
Author: User
Tags echo command

Front-end PHP variables, data types and scopes, PHP data types


Directory

[1] variable variable definition variable assignment [2] data type string integer floating-point Boolean Array Object NULL resource [3] scope global static

Variable

Variable definition

A variable is a container for storing information, beginning with a $ symbol, followed by a variable name. Variable names must start with a letter or underscore and are case sensitive

 
  PHP$x=5; // 5 Echo $x ; // Nothing is output Echo $X ;? >

Assigning values to variables

PHP does not have a command to create a variable, and the variable is created when it is first assigned a value

 
  php$txt= "Hello world!" ; $x=5; $y=10.5;? >

Data type

PHP is a loosely typed language, and without having to tell the PHP variable the data type, PHP automatically converts the variable to the correct data type based on its value. There are 8 types of PHP Data: Four scalar types, two composite types, and two special types. This is: string, Integer, floating-point, Boolean, Array, object, NULL, resource

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

String

The string can be any text within quotation marks, which can be single or double quotation marks

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

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

[1] double quotes embedded in single quotation marks

[2] Single quotation marks embedded in double quotation marks

[3] using the escape character "\"

 
  $str _string1 = ' "Test"; $str _string2 = "' Test '"; $str _string3 = ' \ ' test\ '; Echo $str _string1 ; Echo "
"; Echo $str _string2 ; Echo "
"; Echo $str _string3 ;? >

<情况2>When the quotation marks of a string encounter a variable, there are two cases:

[1] When a variable is enclosed in double quotes, the variable is concatenated with the contents of the double quotation mark

[2] When the single quotation mark contains a variable, the variable is treated as a string output

 
  $test = 1; $str _string1 = ' $test '; $str _string2 $test "; $str _string3 = ' $test '; $str _string4 = "'$test'"; Echo $str _string1; // $test Echo "
"; Echo $str _string2; // 1 Echo "
"; Echo $str _string3; // "$test" Echo "
"; Echo $str _string4; // ' 1 '?>

<情况3>When a string is long, a method using the Heredoc structure is used, first the delimiter represents the string (<<<), followed by an identifier (any name) after (<<<), a string after wrapping, and the end of the string with this identifier. Note The identifiers do not have extra spaces around

 
  $str = <<< g123G; Echo $str; // 123?>

Integer

PHP integers must have at least one number, not a comma or a space, no decimal points, plus or minus, you can specify integers in three formats: decimal, hexadecimal (prefix is 0x) or octal (prefix is 0)

 
  $x = 5985; // Int (5985) Var_dump ($x); Echo "
"$x = -345// int ( -345)var_dump($x ); Echo "
"$x = 0x11// int (var_dump) ($x ); Echo "
"; $x = 011// int (9)var_dump($x);? >

Floating point number

PHP floating point number is a decimal or exponential form of numbers

 
  $x = 10.365; // float (10.365) Var_dump ($x); Echo "
"$x = 2.4e3; // float (2400) Var_dump ($x); Echo "
"$x = 8E-1; // Float (0.8) Var_dump ($x);? >

Boolean type

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

 
  PHP     $man = "male";     $flag $man = = "Male"; // Output 1    Echo $flag  ;     Echo "
" ; $flag $man = = "female"; // Nothing is output Echo $flag ; Var_dump ($flag); // bool (false)?>

Array

PHP arrays can store multiple values in a variable

 
  $cars=array("Volvo", "BMW", "SAAB"); // Array (3) {[0]=> string (5) "Volvo" [1]=> string (3) "BMW" [2]=> string (4) "SAAB"} Var_dump ($cars);? >

Object

A PHP object is a data type that stores data and information about how the data is processed. In PHP, you must explicitly declare an object, but first you must declare the class of the object. For this, using the class keyword, classes are structures that contain properties and methods. Then define the data type in the object class, and then use this data type in an instance of the class

 
  PHPclass  car{    var$color;     function Car ($color= "Green") {      $this$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";p rint_vars ($herbie);? >

Null

Null in PHP is a null type, insensitive to case, NULL type has only one value, indicating that a variable has no value, when it is assigned to NULL, or has not been assigned, or is unset (), the variables are considered null in three cases

 
  PHP  error_reporting// suppress PHP warning prompt $varvar_dump($ var); // NULL $var 1 NULL  var_dump($var 1); // NULL $var 2 NULL  var_dump$var 2); // NULL $var 3 = "Happy Holidays!" "unset($var 3var_dump($var 3);   NULL?>

Resources

PHP resources are created and used by specialized functions, such as open files, data connections, and graphical canvases. You can manipulate resources (create, use, and release). Any resources should be released in a timely manner when they are not needed. If we forget to release the resource, the system automatically enables the garbage collection mechanism to reclaim the resources after the page has finished executing to avoid the memory being exhausted

 
  php$file=fopen("Data/webroot/resource/f.txt", "R");   // Open File $con=mysql_connect("127.0.0.1", "root", "root");  // connecting to a database if ($file _handle) {    // followed by a while loop (described in detail in the loop structure in the following language structure statement) reads the file line by row, Then output each line of text while     (!  Feof($file _handle// to determine whether to the last row        $linefgets( $file _handle // reading a line of text        Echo $line // output a line of text        Echo "
// line Break }}fclose($file _handle); // Close File ?>

[Note]memory_get_usage () Gets the memory consumed by the current PHP unit in byte

!--? 
  
    php 
    echo  
    $m 1  = 
    memory_get_usage ();  
    Echo  "
   
" ; $var _string = ' 123 '; // from Echo $m 2 = Memory_get_usage ()- $m 1 ; Echo "
" ; $n = 123; // 272 Echo $m 3 = Memory_get_usage ()- $m 1 - $m 2 ; Echo "
" ; $f = 123.00; // 272 Echo $m 4 = Memory_get_usage ()- $m 1 - $m 2 - $m 3 ; Echo "
" ; $var _array = array (' 123 '); // 576 Echo $m 5 = Memory_get_usage ()- $m 1 - $m 2 - $m 3 - $m 4 ; ?
 
  PHP   $string = "is just"var_dump($string);   String (12) "That's theecho ."
"$string = 9494var_dump($string); // Int (9494) Echo "
";? >

Scope

PHP has three different variable scopes: local (local), Global, static

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

 
  PHPerror_reporting// suppress PHP warning prompt $x//  global scope function   myTest () {  $y//  local scope  echo "

To test the variables inside the function:

"; Echo "Variable x is:$x
"; Echo "Variable y is:$y"// no output//10myTest (); Echo "

To test variables outside of a function:

"; Echo "Variable x is:$x
"; // 5 Echo "Variable y is:$y"; // No output ?>

Global keywords

Used to access global variables within a function

 
  PHP$x=5; $y=10; function myTest () {  global$x,$y;   $y=$x+$y;} MyTest (); Echo $y // Output?>

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

 
  PHP$x=5; $y=10; function myTest () {  $GLOBALS[' y ']=$GLOBALS[' x ']+$GLOBALS[' y '];} MyTest (); Echo $y // Output?>

Static keyword

In general, when a function is completed or executed, all variables are deleted, but sometimes a local variable needs not be deleted. To do this, you need to use the static keyword when declaring the variable for the first time. Whenever a function is called, the information stored by the variable is the information contained in the last Call of the function, but it is important to note that the variable is still a local variable of the function.

 
  PHPfunction  myTest () {   static$x=0;    Echo $x ;    $x+ +;} MyTest (); // 0 Echo "
"; MyTest (); // 1 Echo "
"; MyTest (); // 2 Echo "
"; MyTest (); // 3 Echo "
"; MyTest (); // 4?>

http://www.bkjia.com/PHPjc/1087040.html www.bkjia.com true http://www.bkjia.com/PHPjc/1087040.html techarticle front-end learning PHP variables, data types and scopes, PHP data type directory [1] variable 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.