Table of Contents
1.1: Type
1.2: Variable detection
1.3: Type detection
1.4: Debugging Print variables
1.5: Type conversion
1.6: Assignment
1.7: Destruction
1.8: Dynamic variable name
Home Backend Development PHP Tutorial Introduction to PHP variables

Introduction to PHP variables

Aug 04, 2017 pm 05:08 PM
php introduce variable

1.1: Type

There are 8 types of variables. There is no need to memorize them by rote. Deepen your understanding in practical applications

1) 整型 [integer] 数学中的整数
2) 浮点型 [float,double] 数学中的小数
3) 字符串 [string] 一串字符
4) 布尔 [boolean] 真假
5) 数组 [array] 键值对复合数据
6) 对象 [Object] [在后面的面向对象中会学到] 
7) NULL 没有值
8) 资源 [resource] “吸管”
Copy after login

What is NULL? The NULL type only marks its type as NULL
Its value field is empty, NULL has no value

$a = 3;//整型
$b = 3.14;//浮点型
$c = null;//null型
$d = 'hello';//字符串
$e = true;//布尔型
echo $D;
Copy after login

Naming convention of variable names:
[a-zA-Z0-9] and underscore (_)
1) Variable names are case-sensitive
2) They cannot start with a number

1.2: Variable detection

echo A non-existent variable will report a notice-level error,
So we need to check whether this variable exists
How to check whether the variable exists?
isset - Check whether the variable is set
Declared variables return true, undeclared variables return false.
Detect whether a variable exists: just check whether the variable name exists in the roster

$b = null; $c = false; $d=0; $e='';
//分别检测上述变量是否存在 
if(isset($a)) {
  echo '变量b存在';
 }else{
  echo '变量b不存在'; 
}
Copy after login

For variables with a value of NULL, false is also returned, because null has no value. Undeclared variables certainly do not exist.

1.3: Type detection

Detect a variable and what type PHP stores it as. It is very simple for PHP to get its variable type because it is already stored in the box. Its variable type
gettype — Get the type of variable [ready-made system function]

$a = false;
echo gettype($a);
$b = "1";
echo gettype($b);
$c = 1.11;
echo gettype($c);
$d = 'hello';
echo gettype($d);
$e = null;
echo gettype($e);
Copy after login

Judge whether the variable is a certain type

is_float()[is_double] 检测变量是否为浮点型
is_int()[is_integer] 检测变量是否为整型
is_string() 检测变量是否为字符串
is_object() 检测变量是否为对象
is_array() 检测变量是否为数组
is_resource 检测变量是否为资源类型
is_bool 检测变量是否是布尔型
is_null 检测变量是否为 NULL
$a = 'hello';
if(is_string($a)) {
  echo 'a是字符串'; 
}else{
  echo 'a不是字符串';
 }
Copy after login

1.4: Debugging Print variables

echo strings, numbers
print_r Print hierarchical data, such as: arrays, objects
var_dump Print the type and value of variables [convenient for debugging code]

$a = 'hello';
$b = array(1,2,"3"); $c = false;
$d = null;
$e=18;
$f = true;
//布尔型的true会打印出1,false和null什么都不显示
 echo $a,$b,$c,$d,$e,$f,&#39;<hr>&#39;;
//print_r 打印层次化的数据,比如数组和对象 
print_r($b);
print_r($c);
print_r($d);
print_r($f);
//不要用echo和print_r打印布尔型的值,因为会干扰我们 
//用var_dump打印布尔和null
var_dump($c);
var_dump($d);
Copy after login

1.5: Type conversion

In PHP, the type of variables can be converted at any time. It is very flexible. The most common one is the conversion between characters and numbers, or numbers/strings-> Conversion of Boolean values

Conversion from string to number
Intercept from left to right, until an illegal number is encountered, the intercepted part is converted into a number

$a = &#39;12&#39;; $b=$a+3; 
var_dump($b);
$a = &#39;12.5hello&#39;; 
//$a = &#39;12.5hello99&#39;; 
$b=$a+3;
var_dump($b);
$a = &#39;word12.5hello&#39;;
$b=$a+3; 
var_dump($b);
Copy after login

Conversion of numbers to strings

$a = 123;
$b = $a . &#39;hello&#39;;
var_dump($b);
Copy after login

Conversion of numbers/strings/arrays to Boolean values

$b=3; 
if($b){
echo &#39;b is true&#39;;
 }else{
    echo &#39;b is false&#39;;
}
Copy after login

if judged It should be a Boolean value. If the number 3 is converted into a Boolean value, then should it be understood as true or false?
The following values ​​are all understood as false as Boolean values
'','0',0,0.0,false,NULL,array(); and other values ​​are treated as Boolean true

if(&#39;&#39; == false) {
echo &#39;空字符串果然假&#39;;
}
Copy after login

empty(var) — Check a Whether the variable is empty
If var is a non-empty or non-zero value, empty() returns FALSE
In other words, "", 0, 0.0, "0", NULL, FALSE, array( ); and objects without any attributes will be considered empty. If var is empty, TRUE will be returned

$arr = array();
if(empty($arr)) {
echo &#39;变量为空&#39;;
 }
Copy after login

1.6: Assignment

There are two ways to assign values:
1 .Assignment by value; (Two people watch the same TV station)
2. Assignment by reference; (Two people watch the same TV)
1. Assignment by value
Variable In fact, the name is not posted on the box, but there is a variable table (like a class roster). The variable value and variable type are placed in the box; the variable name in the variable table points to its corresponding box.

$li = 23;
$wang = $li;
echo $li, &#39;~&#39;, $wang;
Copy after login

Change the value of $li, will the value of $wang change?

$li = 99;
echo $li, &#39;~&#39; ,$wang;
Copy after login

This assignment process is to assign the value of $li to $wang
2. Reference assignment

$a = &#39;tvb&#39;;
$b = &$a; 
//$a,$b共同指向同一个值 
echo $a,&#39;~&#39;,$b;
Copy after login

Change the value of $a

$a = &#39;btv&#39;;
echo $a,&#39;~&#39;,$b;
Copy after login

1.7: Destruction

Why should we destroy variables?
Because, sometimes there are larger arrays or larger objects Special GD drawing consumes resources. Unset it and you can release the memory in time
unset (variable name); To destroy the specified variable, first delete the variable name from the variable table (roster), and then find its corresponding The box is also deleted.

$a=99; //unset($a);
if(isset($a)) {
 echo &#39;a存在&#39;;
}else{
  echo &#39;a不存在&#39;;
}
Copy after login

Note: Reference assignment, if two variables point to the same box, the box cannot be destroyed when one of the variables is destroyed.

$a=99;
$b = &$a;
unset($a);
echo $a,$b;//报一个notice的错误
Copy after login

Reassign a new value to $a

$a=18; 
echo $a,$b;
Copy after login

1.8: Dynamic variable name

Dynamic variable name can reflect the very flexibility of PHP
Use the value of the variable to make another variable Name

$laoda = &#39;liubei&#39;;
echo $laoda , &#39;<br >&#39;;
$paihang = &#39;laoda&#39;;
echo $paihang , &#39;~&#39; , $$paihang;
//排行
$rank = &#39;paihang&#39;; 
echo $$$rank;
Copy after login

The above is the detailed content of Introduction to PHP variables. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles