How to delete a field in php

藏色散人
Release: 2023-03-07 22:36:01
Original
3027 people have browsed it

In PHP, fields can be deleted through the unset function. The syntax of this function is "void unset (mixed $var [, mixed $...])", and its parameter "$var" represents the field to be destroyed. variable.

How to delete a field in php

The operating environment of this tutorial: Windows 7 system, PHP 7.1 version, Dell G3 computer. This method is suitable for all brands of computers.

Recommended: "PHP Video Tutorial"

The unset() function is used to destroy the given variable.

Syntax

void unset ( mixed $var [, mixed $... ] )
Copy after login

Parameter description:

$var: Variable to be destroyed.

Return value

No return value.

Example

<?php
// 销毁单个变量
unset ($foo);
 
// 销毁单个数组元素
unset ($bar[&#39;quux&#39;]);
 
// 销毁一个以上的变量
unset($foo1, $foo2, $foo3);
?>
Copy after login

If you unset() a global variable in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset().

Example

<?php
function destroy_foo() {
    global $foo;
    unset($foo);
}
 
$foo = &#39;bar&#39;;
destroy_foo();
echo $foo;
?>
Copy after login

The output result is:

bar
Copy after login

If you want to unset() a global variable in the function, you can use the $GLOBALS array to achieve this:

Example

<?php
function foo() 
{
    unset($GLOBALS[&#39;bar&#39;]);
}
 
$bar = "something";
foo();
?>
Copy after login

If you unset() a variable passed by reference in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset().

Example

<?php
function foo(&$bar) {
    unset($bar);
    $bar = "blah";
}
 
$bar = &#39;something&#39;;
echo "$bar\n";
 
foo($bar);
echo "$bar\n";
?>
Copy after login

The above routine will output:

something
something
Copy after login

If you unset() a static variable in the function, then the static variable will be destroyed inside the function. However, when this function is called again, this static variable will be restored to the value it had before it was last destroyed.

Example

<?php
function foo()
{
    static $bar;
    $bar++;
    echo "Before unset: $bar, ";
    unset($bar);
    $bar = 23;
    echo "after unset: $bar\n";
}
 
foo();
foo();
foo();
?>
Copy after login

The above routine will output:

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
Copy after login

The above is the detailed content of How to delete a field in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template