How to delete elements in an object in php

藏色散人
Release: 2023-03-10 18:50:01
Original
3049 people have browsed it

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

Related introduction:

unset - Release the given variable

Description

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

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

The above routine will output:

bar
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!