Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126697 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id A0E5C1A00BC for ; Mon, 10 Mar 2025 17:37:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741628096; bh=E6zi8tIxMzWLpozRpMD76rJdGyGOAHbPXjwPsMy5mBc=; h=Date:From:To:Subject:In-Reply-To:References:From; b=JIAWG8RT3P4ddVTUuFMyHAJwQibK7ixbX3K2N/y2YDuDIhqT7MhShXGOaa/dTGvS/ A9Cr/l1v7RS4ZtVa9o2boRKemNYi3U78ZkTAy8xsZ/DjDmlyrrSiRcdM8hiFrpbCq3 TxvCA10loER1wBBXeE/6zHSdaSGrzI1Fz++nsipGAtWI7wFnuG3ZiEbyhJMyMJKm4Z LoaSTO0TR+eV+POaAQmNwT3GKvMzfKbs3eqGtq4Y/1k3gxXX+LXC2QLvBKxTWh83mH DDtatAhVaGMe7x0BcK+7kmI3eexgbRmo2FgSPmAP7Cd/qvkQk8YZ/fSP3xDMDhdKdx f4GE9H3fXNRGw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 52851180053 for ; Mon, 10 Mar 2025 17:34:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: *** X-Spam-Status: No, score=3.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_PASS, SPF_SOFTFAIL autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from xdebug.org (xdebug.org [82.113.146.227]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 10 Mar 2025 17:34:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741628248; bh=E6zi8tIxMzWLpozRpMD76rJdGyGOAHbPXjwPsMy5mBc=; h=Date:From:To:Subject:In-Reply-To:References:From; b=nTMicRk/wJiKH0mWAV+bhIZiirYwy4gfCD/c/Vel+U36b1AlSCTfAxmeZWEjdRToy X510cx0LAQr0kHvTiipgU1bu112FvN9AgBrySUnsRoFgZ1vnInn7f/JZgwkHxP/p6y furXPRn+OHCpZZbLf3i+mIGidB0/4FOxkG+wt4s7xDfGEZOWMXOrh49R1erjXyU8Lo n35CsvraGCICbhSA1w526qTSQ/6lqmBEqhNAfWAVUCCIe8CLe6v+oJXMOFpclX+x6A knoJqX4/NYo+sglUJenjiXacS4BZk8H0oQMyVPIVHVxCr36RmiM0ydOB/nGj9tviIp fANMCCOo09Gwg== Received: from [127.0.0.1] (unknown [85.255.236.98]) by xdebug.org (Postfix) with ESMTPSA id EDE6610C05F; Mon, 10 Mar 2025 17:37:27 +0000 (GMT) Date: Mon, 10 Mar 2025 17:37:28 +0000 To: internals@lists.php.net, Vinicius Dias , PHP internals Subject: Re: [PHP-DEV] Manual unset and GC User-Agent: K-9 Mail for Android In-Reply-To: References: Message-ID: <52F313B1-8795-4E88-9744-75DFED31DA98@php.net> Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: derick@php.net (Derick Rethans) On 10 March 2025 16:15:16 GMT, Vinicius Dias wrote= : >Hello, folks=2E If this is not the correct place to ask, I apologize=2E > >> TL;DR: Does setting a variable to `null` (or even `unset`ing it) have *= *any** effect if that's the last instruction of a function? > >The full version of the question with context: > >I have a question about the inner workings of PHP that was raised by >some people at work=2E > >In the code we write at the company I work for, there's a guideline to >always assign `null` to the variables of type `PdoStatement`=2E >Something like the following: > >``` >$stm =3D null; >``` > >I understand this removes the reference to the object, calls the >destructor and frees the memory, but the point is: the guideline >mandates that we do that even if it is the last instruction of a >function, for example: > >``` >function example(): void >{ > // run your SQL queries > > $stm =3D null; >} >``` > >I understand that this last line is not needed and removing it would >have literally no effect on the code execution since `$stm` will go >out of scope and the same things (remove reference, call destructor >and free memory) will happen exactly the same=2E > >While discussing this with a few colleagues it was pointed out that >**maybe** PHP will execute the GC immediately when we do `$stm =3D null` >but not when the variable goes out of scope, making the explicit null >as some sort of optimization=2E > >I didn't find any resources on the documentation that could point to >which assumption is correct, so I post the question here: > >Does setting a variable to `null` (or even `unset`ing it) have **any** >effect if that's the last instruction of a function? > >Thank you No=2E It has no effect=2E At the end of the function PHP would remove a ref to t= he variable anyway=2E Setting it to null first is an action that will take = time, unless opcache is turned on and realises it's a useless action=2E cheers Derick