note 125755 added to function.get-defined-vars

From: Date: Tue, 02 Feb 2021 12:35:58 +0000
Subject: note 125755 added to function.get-defined-vars
Groups: php.notes 
Request: Send a blank email to php-notes+get-219217@lists.php.net to get a copy of this message
get_defined_vars() returns all variables - locally defined vars and global vars (well actually only super globals). If you need local vars only - for example you need to get variables from a file - let's say config.php and you don't want a missing value to be replaced with a global defined somewhere else. <?php /** * Filters out global vars from get_defined_vars() in order to get local vars only * @param array $localVars pass get_defined_vars() here * * @return array local vars only */ function removeGlobals(array $localVars) { return array_diff_key($localVars, $GLOBALS); } define('CONFIG_FILE_PATH', '/path/to/config.php'); function readConfig() { require CONFIG_FILE_PATH; $config = removeGlobals(get_defined_vars()); return $config; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.166 (proxied: 85.11.146.60) ---- Manual Page -- http://php.net/manual/en/function.get-defined-vars.php Edit -- https://master.php.net/note/edit/125755 Del: integrated -- https://master.php.net/note/delete/125755/integrated Del: useless -- https://master.php.net/note/delete/125755/useless Del: bad code -- https://master.php.net/note/delete/125755/bad+code Del: spam -- https://master.php.net/note/delete/125755/spam Del: non-english -- https://master.php.net/note/delete/125755/non-english Del: in docs -- https://master.php.net/note/delete/125755/in+docs Del: other reasons-- https://master.php.net/note/delete/125755 Reject -- https://master.php.net/note/reject/125755 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#219217) next »