Declaration of variables
PHP variable declaration must be named with $(dollar sign)+variable name, and assignment after =(assignment operator)
The declared variable can not only be used in one , it can also be used All open functions on the current page, including files introduced by include and require, are of course local variables in the function, which is another matter. Before using this variable, we usually use the isset() and empty() functions. isset() checks whether the variable is set, empty() checks whether the variable is empty, and unset() releases the variable. It is recommended to use it here! empty() exists and cannot be empty
The naming of PHP variables is case-sensitive , and cannot be the keyword Demo<?php //聲明變量a $a="hello world"; ?> <?php //判斷變量a是否存在,如果存在,就打印,echo為打印函數(shù) if(!empty($a)){ echo "變量存在"; echo $a; } //銷毀變量a unset($a); if(empty($a)){ echo "變量不存在!"; } ?>Variable variableVariable variable means that the variable name of a variable can be set and used dynamically. An ordinary variable is set through declaration, and then the variable variable gets the value of the ordinary variable as the variable name of the variable variable. The variable variable declaration starts with $$. Demo
<?php //聲明變量$a $a="hello"; //聲明可變變量$$a $$a="world"; //將會全部打印"hello world" echo "$a $hello"; echo "$a ${$a}" ?>Reference assignment of variablesThe reference of PHP is to add the & symbol in front of the variable, function, object, etc. It is actually equivalent to an alias of a variable. If the value of any one of the variables is changed, the value of the other variable will change accordingly. But it is not like variable reference assignment in C language. If I use the unset() function to destroy any one of the variables, the other variable still exists. Demo
<?php //聲明變量$a $a="hello"; //聲明變量$b $b=&$a; $b="world"; //將會打印"word world" echo "$a $b"; unset($a); //將會打印world echo $b; ?>Types of variablesPHP supports eight primitive types. Specifically, it is divided into four scalar types: string (string), integer (integer), float (floating point type, and higher-precision double) and boolean (Boolean type), and two composite types: array (array) ) and object (object), two special types resource (resource) and NULL. The declaration of arrays and objects can refer to the format in Demo. We use array() to construct the array here, and its parameters are separated by commas in the key=>value format. Demo
<?php $bool=true; $str="hello"; $int=123; $float=1.2e3;//相當于1.2乘以10的三次方 $arr=array("key1"=>12,"key2"=>true); //聲明對象類型 class Person{ var $name; function say(){ echo "I am happy"; } } $p=new Person(); $p->name="Tom"; $p->say(); //var_dump()直接輸出變量類型 var_dump($bool); var_dump($str); var_dump($int); var_dump($float); var_dump($arr); var_dump($p); //輸出結(jié)果為 //I am happy //bool(true) string(5) "hello" int(123) float(1200) //array(2) { ["key1"]=> int(12) ["key2"]=> bool(true) } //object(Person)#1 (1) { ["name"]=> string(3) "Tom" } ?>Resource typeResource is a special variable that holds a reference to an external resource. Resources are created and used through specialized functions. Since resource type variables hold special handles for opening files, database connections, graphics canvas areas, etc., there is no point in converting values ??of other types into resources. Demo
<?php //以寫的方式打開本目錄下的1.txt文件 $file=fopen("1.txt","w"); //連接本地數(shù)據(jù)庫 $mysql=mysql_connect("localhost","root","root"); ?>NULL typeThere are three situations that are considered to be NULL types in PHPAssign variables directly to NULLDeclared variables are not assigned Variables destroyed by the unset() function There are three pseudo-types in PHP: mixed type, number type and callback type. mixed indicates that a parameter can accept multiple different types, but not all types. For example, str_replace() can accept strings and arrays, and gettype() can accept any type. The number parameter can accept integer and float.
The callback type is a function such as call_user_func() that can receive a user-defined function as a parameter. The callback function can not only be a function, but also a method of an object and a method of a static class. A PHP function is passed as a function name string. Any built-in or user-defined function can be passed, except for example array(), echo(), empty(), eval(), exit(), isset(), list (), print(), unset() and other built-in functions.
Automatic type conversion
This conversion usually occurs when mixing operations of different types. It follows the following principles
If it is a Boolean type, true will become 1 and false will become 0
If it is null, it will become The value 0
If it is a mixed operation of float and int, convert it to float type
If it is a string, extract the numbers in the string, for example, "123.45abc" becomes 123.45, if there is no number, it is 0
Mandatory Type casts
Type casts in PHP are very much like in C: the variable to be converted is preceded by the target type enclosed in parentheses. The allowed casts are:
(int), (integer) - converted to integer type
(bool), (boolean) - converted to Boolean type
(float), (double), (real) - converted to Floating point type
(string) - Convert to string
(array) - Convert to array
(object) - Convert to object
At the same time, we can determine the variable type through some functions during use. Commonly used functions to determine variable types include the following:
gettype() returns variable type, is_array(), is_bool(), is_float(), is_double(), is_integer(), is_null(), is_numeric(), is_object( ), is_resource(), is_string() and is_callable() to determine whether it is a valid function

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP development, we often encounter the error message PHPNotice:Undefinedvariable. This error message means that we have used an undefined variable in the code. Although this error message will not cause the code to crash, it will affect the readability and maintainability of the code. Below, this article will introduce you to some methods to solve this error. 1. Use the error_reporting(E_ALL) function during the development process. In PHP development, we can

Solution to PHPNotice:Undefinedvariable:arrin In PHP programming, we often encounter the error message "Notice:Undefinedvariable". This error message is usually caused by accessing an undefined variable or the variable has not been initialized. For this problem, we need to find the problem and solve it in time. In this article, we will focus on PHPNotice:Undefin

How to use numeric variables in PHP In PHP, a numeric variable is a variable type that is used directly without declaration. You can use numeric variables to perform mathematical calculations, data comparisons, and other numerical operations. This article will explain how to use numeric variables in PHP and provide specific code examples. Defining numeric variables In PHP, defining numeric variables is very simple, just assign a number directly to the variable. Here is an example: $number=10; In the above code, we define a value called $numb

In PHP, you can use the ampersand (&) symbol to pass variables by reference instead of by value. This allows the original variable to be modified within a function or method. There are mainly two ways to pass PHP variables by reference: Using ampersand symbol Using ampersand symbol in function/method declaration Using ampersand symbol in function/method declaration When passing variables to function/method In PHP, you can use function/ The ampersand symbol (&) in a method declaration passes variables by reference. Here is the updated explanation: To pass a reference variable by using the & symbol in a function/method declaration, you need to include the & symbol before the parameter name in the function/method definition. This indicates that parameters should be passed by reference, allowing

When developing a PHP application, if you encounter the "Undefined variable: sql" prompt, it usually means that you are referencing an undefined variable. This can be due to many reasons, such as misspellings of variable names, scoping issues, or syntax errors in the code, etc. In this article, we will explore the various causes of this problem and provide some ways to solve it. 1. Variable name is misspelled In your PHP code, if the variable name is incorrect or misspelled, the system

How to quickly eliminate PHP variable undefined errors? In PHP development, we often encounter undefined variable errors. This is because an unassigned variable is used in the code. When encountering this kind of error, we need to quickly find the cause of the error and solve it. Here are some ways to quickly troubleshoot PHP variable undefined errors to help you locate and fix errors faster. Turn on error reporting: When we turn on error reporting, PHP will display all error and warning messages, including variable undefined errors. We can do this by opening the code

PHPNotice:Undefinedvariable:result means that an undefined variable result is called in the PHP program, which will cause the program to generate a Notice-level warning. This situation is generally caused by programmers not correctly defining variables or the scope of variables when writing PHP code. If not resolved in time, this Notice level warning may cause problems in the operation of the program. So, how to resolve PHPNotice:

In PHP programming, variables are the basic unit of storing values ??and are used to store and use data during program execution. In PHP, variables can be assigned different data types, including integers, floating point, strings, arrays, etc. In this article, we will introduce common variables and their usage in PHP programming. Simple variables are the most common variable type. They can store regular data types such as integers, floating point numbers, and strings. In PHP, the initial value of undefined variables is NULL. Here are a few examples: Integer variable: $
