name);var_dump($user);" method."/> name);var_dump($user);" method.">

Home  >  Article  >  Backend Development  >  How to delete elements in an object in php

How to delete elements in an object in php

藏色散人
藏色散人Original
2021-06-19 11:17:273055browse

php method to delete elements in an object: first create a PHP sample file; then delete it through the "var_dump($user);unset($user->name);var_dump($user);" method Just a property of the object.

How to delete elements in an object in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to delete elements in an object with php? How to delete an attribute of an object in php?

unset

<?php
define(&#39;CLI_SCRIPT&#39;, true);
$user = new stdClass();
$user->id = 1;
$user->name = &#39;tony&#39;;
$user->age = 23;
var_dump($user);
unset($user->name);
var_dump($user);

Related introduction:

unset - Release the given variable

Description

unset(mixed $var, mixed $... = ?): void

unset() Destroy the specified variable variable.

The behavior of unset() in a function will vary depending on the type of variable you want to destroy.

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().

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

The above routine will output:

bar

Recommended learning: "PHP Video Tutorial"

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

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