Home 类库下载 PHP类库 PHP variables

PHP variables

Oct 09, 2016 pm 12:43 PM
php variables

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为打印函数
if(!empty($a)){
    echo "变量存在";
    echo $a;
}

//销毁变量a
unset($a);

if(empty($a)){
    echo "变量不存在!";
}
?>
Copy after login

Variable variable

Variable 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}"
?>
Copy after login

Reference assignment of variables

The 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;
?>
Copy after login

Types of variables

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

//输出结果为 
//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" }
?>
Copy after login

Resource type

Resource 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");

//连接本地数据库
$mysql=mysql_connect("localhost","root","root");
?>
Copy after login

NULL type

There are three situations that are considered to be NULL types in PHP

Assign variables directly to NULL

Declared 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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 Notice: Undefined variable:Solution PHP Notice: Undefined variable:Solution Jun 25, 2023 pm 04:18 PM

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

PHP Notice: Undefined variable: arr in solution PHP Notice: Undefined variable: arr in solution Jun 22, 2023 am 10:21 AM

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 How to use numeric variables in PHP Sep 13, 2023 pm 12:46 PM

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

How to quickly eliminate PHP variable undefined errors? How to quickly eliminate PHP variable undefined errors? Dec 17, 2023 am 10:23 AM

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

PHP Notice: Undefined variable: sql solution PHP Notice: Undefined variable: sql solution Jun 23, 2023 am 08:51 AM

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 pass PHP variables by reference How to pass PHP variables by reference Aug 26, 2023 am 09:01 AM

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

Solution to PHP Notice: Undefined variable: result Solution to PHP Notice: Undefined variable: result Jun 22, 2023 pm 01:32 PM

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:

What are the common variables in PHP programming? What are the common variables in PHP programming? Jun 12, 2023 am 10:06 AM

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: $

See all articles