PHP-000- [Data type] - Introduction

Learning points
PHP data types in an 8-
string, integer, float, logic arrays, objects, NULL, resource
fact, the so-called resource object types of


PHP string

Use single or double quotes can be

<?php 
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = 'Hello world!';
echo $x;
?>

 PHP integer

Integer is no decimal numbers.

Integer rules:

Integer must have at least one number (0-9)

Integer can not contain commas or spaces

Integer can not have a decimal point

Integer positive or negative can be

Integer can be specified in three formats: decimal, hexadecimal (prefix 0x) or octal (prefixed with 0)

In the following example, we will test different numbers. PHP  var_dump () returns the data type and value of a variable :

<?php 
$x = 5985;
var_dump($x);
echo "<br>"; 
$x = -345; // 负数
var_dump($x);
echo "<br>"; 
$x = 0x8C; // 十六进制数
var_dump($x);
echo "<br>";
$x = 047; // 八进制数
var_dump($x);
?>

  


PHP floating point

There is a decimal point or floating-point exponential numbers.

In the following example, we will test different numbers. PHP var_dump () returns the data type and value of a variable:

<?php 
$x = 10.365;
var_dump($x);
echo "<br>"; 
$x = 2.4e3;
var_dump($x);
echo "<br>"; 
$x = 8E-5;
var_dump($x);
?>

 


 PHP logic

Logically true or false.

$x=true;

$ Y = false;


PHP array

An array of a plurality of values ​​stored in a variable.

In the following example, we will test different arrays. PHP var_dump () returns the data type and value of a variable:

<?php 
$cars=array("Volvo","BMW","SAAB");
var_dump($cars);
?>

 


 PHP object

The object is to store data and information about how to handle the data of data types.

In PHP, you must explicitly declare the object.

First, we must declare an object class . In this regard, we use the  class keyword . Class containing properties and methods of construction.

Then we define the data types in the object class, then the use of this data type class instance :

<?php
class Car
{
  var $color;
  function Car($color="green") {
    $this->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";
   }
}

// instantiate one object
$herbie = new Car("white");

// show herbie properties
echo "\herbie: Properties\n";
print_vars($herbie);

?>

 


 PHP NULL value

Special  NULL value represents a variable has no value . NULL is the only possible value of the data type NULL .

Whether NULL values are indicated variable is empty. It is also used to distinguish an empty string with a null value database .

The value can be set to NULL, variable empty:

<?php
$x="Hello world!";
$x=null;
var_dump($x);
?>

 

 

Published 19 original articles · won praise 2 · Views 1418

Guess you like

Origin blog.csdn.net/yueyekonglong/article/details/103987279