Data types for beginners to PHP

Scalar data type: It is the most basic unit in the data structure and can only store one data

Boolean type: boolean String type: string Floating point type: float Integer type: integer

Two composite types:

array() Array

object Object

Two special types

resource Resource type

null Empty

The function to determine the type gettype();


##Integer type: Integer is A number without decimals,

1. The integer must have at least one digit (0-9)

2. The integer cannot contain commas or spaces

3. The integer is not The decimal point

4. The integer can be a positive or negative number

5. The integer type can be specified in three formats: decimal, hexadecimal (prefixed by 0x) or octal (prefixed by 0x is 0)

<?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);
?>

Note: In the above code, we test different numbers. The PHP var_dump() function returns the data type and value of the variable

String type:

A string is a sequence of characters, like "Hello world!"

<?php 
	$x = "Hello world!";
	echo $x;
	echo "<br>"; 
	$x = 'Hello world!';
	//echo $x;
	var_dump($x);
?>

Note: Using var_dump() output, we have been told that 'hello wordl!'

Yes A string type with a length of 12

Floating point type

A floating point number is a number with a decimal part, or in exponential form

<?php 
	$x = 10.365;
	var_dump($x);

	echo "<br>"; 

	$x = 2.4e3;
	var_dump($x);

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

Note: 2.4e3 This kind of thing is written in scientific notation

2.4e5 = 2.4*10^5 That is equal to 2.4 times 10 raised to the fifth power

Boolean type :

The values ​​of Boolean type are only true and false

<?php
	$x = true;
	$y = false;
	var_dump($x);
	echo "</br>";
	var_dump($y);
?>

Note: Boolean type is generally used for conditional judgment

Array type array

Arrays can store multiple values ​​in one variable

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

Note: This is just a simple array, we will talk about array traversal later

Object: object

1. The object data type can also be used to store data

2. In PHP, the object must be declared

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

		  function what_color() {
		    return $this->color;
		  }
	}
?>

Note: First, you must use class Keyword declares a class object. Classes are structures that can contain properties and methods.

Then we define the data type in the class, and then use the data type in the instantiated class:

In the above example, the PHP keyword this is a pointer to the current object instance, not to any Other objects or classes

NULL:

1. The special NULL value indicates that a variable has no value. The only possible value of the NULL type is NULL

2. A variable is considered NULL

in the following situations

(1). Assigned to NULL

(2). Not yet assigned

(3). Unset()

3.NULL cannot be written to null

is_null

is_null() function detects whether the variable is NULL. If the variable is NULL, it returns TRUE, otherwise it returns FALSE

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码 
	$var = NULL;
	if(is_null($var)){
	    echo '$var 为 NULL';
	} else {
	    echo '$var 变量非 NULL';
	}
?>

Resource: resource

Resource is a special variable type that saves a reference to an external resource: such as an open file, database connection, graphics canvas area, etc. Resources are created and used through specialized functions

<?php
	if(!file_exists("test.txt")){
	    $fh = fopen("test.txt","w");	    //打开文件
	    echo get_resource_type($fh);    // 输出:stream
	    fclose($fh);		    //关闭文件
	}
?>

Because resource type variables save special handles for opening files, database connections, graphics canvas areas, etc., other types of values ​​cannot be converted into resources


Continuing Learning
||
<?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); ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!