Data types of PHP development basic tutorial

Summary:

PHP has 8 data types, namely: String (string), Integer (integer), Float (floating point), Boolean (Boolean) type), Array (array), Object (object), NULL (null value), resource (resource). The following are introduced one by one:


1. PHP string

A string is a period of characters, more popularly speaking, it is a paragraph. This paragraph can be very Long or very short.

1. There are three ways to declare strings in PHP

  • Declare with single quotes

Use single quotes in English half-width state to surround the string

<?php
//声明字符串变量$str
$str= 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言';
//输出字符串
echo $str;
?>
  • Use double quotes to declare

<?php
//声明字符串变量$str
$str= “PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言”;
//输出字符串
echo $str;
?>
  • Declare with character delimiters (used when a very large string needs to be entered)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
$str = <<<DJ
PHP 是
一种创建动态交互<br/>
性站点的强有力的服务器端
    <i>脚本语言</i>
    我想说:<h1>PHP简介</h1>
DJ;
echo $str;
?>
</body>
</html>

2. What is the difference between double quotes and single quotes

  • Double quotes parse variables, but single quotes do not parse variables.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量
$name="小明";
$age="24";
//放入字符串中
$str1="我叫$name ,今年$age 岁,很高兴认识大家";
$str2='我叫$name ,今年$age 岁,很高兴认识大家';
//输出字符串
echo $str1;
echo "<br/>";
echo $str2;
?>
</body>
</html>

It can be concluded from the above: double quotes execute variables, while single quotes do not execute variables

Note: Double quotes need to parse variables, so double quotes The efficiency of quotation marks is not as high as that of single quotation marks

  • Insert a variable inside double quotation marks. If there are English or Chinese characters behind the variable, it will splice the character and the variable together and treat it as an entire variable. Be sure to separate the variables with special characters, such as spaces.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量
$a="php.com";
$str1="$a欢迎您";
$str2="$a 欢迎您";
echo $str1;
echo "<br/>";
echo $str2;
?>
</body>
</html>

It can be seen from the error report that the first one treats "aWelcome" as a variable, and the second one outputs normally.

  • If you insert a variable within double quotes and do not want spaces behind it, you can wrap the variable in braces.

Slightly change the above example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量
$a="php.com";
$str1="$a 欢迎您";
$str2="{$a}欢迎您";
echo $str1;
echo "<br/>";
echo $str2;
?>
</body>
</html>
  • Double quotes parse escape characters, single quotes do not parse escape characters. However, single quotes can parse \' and \

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明字符串
$str1 = "改变世界之前\n请先\t改变你自己";
$str2 = '改变世界之前\n请先\t改变你自己';
echo $str1;
echo "<hr/>";
echo $str2;
?>
</body>
</html>

Note: Regarding "single quotes can parse \' and \", please try to do it yourself

  • Single quotes are more efficient than double quotes, use single quotes as much as possible

  • Double quotes and single quotes can be inserted into each other! ! ! Insert single quotes between double quotes, insert variables between single quotes, and the variable will be parsed.

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
$a='黄河';
$str="白日依山尽,'$a'入海流";
echo $str;
?>
</body>
</html>

Note: You can try what will happen if you put double quotes inside double quotes

  • Magic String splicing glue - (.) dot, used to splice strings.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量
$name="小明";
$age="24";
//放入字符串中
$str1="我叫$name ,今年$age 岁,很高兴认识大家";
$str2='我叫$name ,今年$age 岁,很高兴认识大家';
//输出字符串
echo $str1;
echo "<br/>";
echo $str2;
?>
</body>
</html>

This is a previous example. As mentioned earlier, double quotes can parse variables, but the efficiency is not as high as single quotes. Now I want to be able to parse variables with high efficiency, so I can only rely on (.) is here to help us

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量
$name="小明";
$age="24";
//放入字符串中
$str='我叫'.$name.' ,今年'.$age.' 岁,很高兴认识大家';
//输出字符串
echo $str;
?>
</body>
</html>
  • (.) dot can also be used to connect multiple strings

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量
$str1 = '白日依山尽<br/>';
$str2 = '黄河入海流<br/>';
$str3 = '欲穷千里目<br/>';
$str4 = '更上一层楼<br/>';
echo $str1.$str2.$str3.$str4;
?>
</body>
</html>
  • We treat the delimiter declaration string as the same function as double quotes.

Example:

<?php
$a = '宣布对用户提现收费';
$b = '支付行业巨头的收费行动';
$str = <<<DJ
   支付宝
            $a
        这也是\t微信在年初宣布提现收费之后
        又一
            '$b'
        很<br />
       多人\n质疑
    <i>互联网公司</i>
    所提倡的免费<b>商业模式</b>难道走到了尽头
DJ;
echo $str;
?>

You can execute it and find that $,$,\t\n can be executed, and double quotes and single quotes can be displayed. This is the characteristic of delimiters.

3. Two practical functions about strings

strlen(string) returns the length of the string (number of characters)

strpos (The string being searched for, the character being searched for, or text) If a match is found in the string, the function returns the first matching character position. If no match is found, returns FALSE.

Example:

<?php
//输出一个字符串的长度
echo strlen("Hello world");
echo "<br/>";
//在字符串 "Hello world!" 中查找文本 "world",字符串中第一个字符的位置是 0,而不是 1。
echo strpos("Hello world","world")
?>

Note: The position of the first character in the string is 0 instead of 1

For complete string functions, please see the PHP reference Manual


2. PHP integer type

An integer is a number without decimals.

Integer rules:

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

  • The integer cannot contain commas or spaces

  • Integers have no decimal point

  • Integers can be positive or negative

  • Integers can Specify in three formats: decimal, hexadecimal (prefixed by 0x) or octal (prefixed by 0).

In the following examples we will test different numbers. The PHP var_dump() function returns the data type and value of the variable:

Note: var_dump() is a function. Insert variables between brackets (). This function will print out the data type, and also display the length and value of the variable accordingly

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//定义变量
$x = 5985;
$y = -345; // 负数 
$z = 0x8C; // 十六进制数
$w = 047; // 八进制数
//输出变量
var_dump($x);
echo "<br>";
var_dump($y);
echo "<br>"; 
var_dump($z);
echo "<br>";
var_dump($w);
?>
</body>
</html>

3. Floating point type

The so-called floating point type is the decimal in our mathematics.

The following is an example to test different numbers

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php 
//定义变量
$x = 10.365;
$y = 2.4e3;
$z = 8E-5;
//输出变量
var_dump($x);
echo "<br>"; 
var_dump($y);
echo "<br>"; 
var_dump($z);
?>
</body>
</html>

4. Boolean type

The so-called Boolean , can be understood as the two sides of things, there is truth and there is falsehood, there is right and there is wrong, the current computer system basically uses a binary system, that is, a combination of 0 and 1

There are only two Boolean types Value: TRUE or FALSE

$x=true;
$y=false;

is often used for conditional judgment. You will often deal with it in future studies.

Note: Do not add quotes outside true and false

Type conversion:

The following situations will be converted to false

  • Boolean value=false

  • Integer=0

  • ## Floating point number=0.0

  • Empty array

  • Object without variable idiom

  • Variable value is empty

In other cases, it will basically be converted to true


5. Array

The variables defined above can only be stored A value, if you want to store multiple values, you need to use an array

In the following example, an array is created, and then the PHP var_dump() function is used to return the data type and value of the array:

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php 
//定义变量
$fruits=array("苹果","香蕉","梨子");
//输出变量
var_dump($fruits);
echo gettype($fruits);
?>
</body>
</html>

Everyone must first have an understanding of arrays, master the basics of declaring an array, and determining whether it is an array type. There is a special chapter to discuss arrays later.


6. Object

  • Object data type can also be used to store data.

  • In PHP, objects must be declared.

  • First, you must declare a class object using the class keyword. 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:

Let’s look at an example:

<?php
//定义一个Car 类
class Car
{
  var $color;
  function Car($color="green") {
    $this->color = $color;
  }
  function what_color() {
    return $this->color;
  }
}
//实例化Car类
$car = new Car;
//输出查看类型
var_dump($car);
?>

You don’t need to go into details about the object for the time being. You will gradually understand it in the future. For the time being, you only need to see this object and know that it is an object.


7. NULL value

NULL value means that the variable has no value. NULL is a value of data type NULL. Null is not false, not 0, and not a space.

1. Three situations where null occurs:

  • Explicitly specify that the value of the variable is NULL through variable assignment

  • A variable does not have any value

  • Use the function unset() to destroy the variable

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//声明变量为null
$n = null;
var_dump($n);
//var_dump显示输出变量$meiyou,看看结果是什么?
var_dump($a);
//声明一个变量$b的值为字符串的手机
$b = '手机';
//unset销毁掉一个变量
unset($b);
var_dump($b);
?>
</body>
</html>

2. The difference between empty and isset functions

  • empty() function

empty( ) can pass a variable into the middle of the brackets. If the value of this variable is false or null, it returns true.

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
$a=null;
if(empty($a)){
	echo '这个变量是空';
}else{
	echo '这个变量不是空';
}
?>
</body>
</html>

empty returns true when $a=null.

  • isset() function

isset() can pass one or more variables into the middle of the brackets. Use commas to separate variables. . As long as there is a variable that is null, it returns false. Otherwise, returns true.

Example:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php.cn</title>
</head>
<body>
<?php
//传入一个变量
$a=false;
$b=isset($a);
var_dump($b);
echo "<hr/>";
//传入多个变量
$b=10;
$c=false;
$d=null;
$e=isset($b,$c,$d);
var_dump($e);
echo PHP_VERSION;
echo __LINE__;
?>
</body>
</html>

Note: The function of unset() is to destroy variables. Insert the name of the variable you want to destroy between the unset (variable) brackets, and the variable will be destroyed. You can destroy it yourself, and then use the var_dump() function to see the effect.


8. Resource Type

Resource type Many beginners find it difficult to understand. Because when the resource type is printed, only one English resource can be seen. Nothing else can be displayed on the computer, but it has an objective existence.

Actually, the resource type is very simple. Let's explain it with words.

The resources in the computer refer to:

  • word, excel and other files

  • Some people have collected beautiful pictures and our selfies and other pictures

  • Music

  • Some people love AVI short movie to watch

  • Open web page

  • Database
    ... ...and so on

We open an image and use PHP to operate it. We are a resource to operate.
The database connection we open, we need to connect to the database, the database is the resource we operate.
Including network connection, sending emails after connection, we can also think of it as a resource.

Note: Understanding resources means operating our visible and invisible files, networks and data. In the following chapters, everyone will have a deeper impression when operating pictures and networks.


9. Check and determine the data type

1. Check the data type

In future study and work, if you need to know the data type of a data, you can know it through the following two functions

  • gettype (pass in a variable) Ability to obtain the type of variable

  • var_dump (pass in a variable) Output variable type and value

Example:

<?php
//声明变量,输出其类型
$str = 9.99;
$type = gettype($str);
echo $type;
echo "<br/>";
$str1 = true;
$type1 = gettype($str1);
echo $type1;
echo "<br/>";
//声明变量,输出其类型和值
$str2 = "霸都";
var_dump($str2);
echo "<br/>";
$arr = array(1,23,4);
var_dump($arr);
?>

Note: You can try to output several other types

2. Determine the data type

We use the is_* series of functions. The is_types series of functions are used to determine whether something is of a certain type. Returns true if it is of this type, false if it is not.

  • Is is_int is an integer type

  • is_bool Is it a Boolean

  • is_float Is it a floating point

  • is_string Is it a string

  • is_array Is it an array

  • is_object Is it an object

  • is_null Is it empty?

  • is_resource Is it a resource

  • is_scalar Is it a scalar

  • Is is_numeric a numeric type?

  • Is_callable a function

Example:

<?php
$fo = false;
var_dump(is_bool($fo));
var_dump(is_null($fo));
$str='18.8';
var_dump(is_string($str));
?>

Note: You can try a few others and see the output results

Continuing Learning
||
<?php //声明字符串变量$str $str= 'PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言'; //输出字符串 echo $str; ?>
submitReset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!