note 128903 added to function.compact
| From: | c dot smith at fantasticmedia dot co dot uk | Date: | Wed, 20 Sep 2023 15:23:44 +0000 |
| Subject: | note 128903 added to function.compact | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220863@lists.php.net to get a copy of this message | ||
If you must utilise this knowing that a variable may be unset, then you need to use an alternative
method.
So instead of the following:
<?php
$var1 = "lorem";
$var2 = "ipsum";
$result = compact('var1', 'var2', 'unsetvar');
?>
Consider the following:
<?php
$var1 = "lorem";
$var2 = "ipsum";
$result = [];
foreach( ['var1', 'var2', 'unsetvar'] as $attr ) {
if ( isset( $$attr ) ) {
$result[ $attr ] = $$attr;
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 213.107.111.58)
----
Manual Page -- https://php.net/manual/en/function.compact.php
Edit -- https://main.php.net/note/edit/128903
Del: integrated -- https://main.php.net/note/delete/128903/integrated
Del: useless -- https://main.php.net/note/delete/128903/useless
Del: bad code -- https://main.php.net/note/delete/128903/bad+code
Del: spam -- https://main.php.net/note/delete/128903/spam
Del: non-english -- https://main.php.net/note/delete/128903/non-english
Del: in docs -- https://main.php.net/note/delete/128903/in+docs
Del: other reasons-- https://main.php.net/note/delete/128903
Reject -- https://main.php.net/note/reject/128903
Search -- https://main.php.net/manage/user-notes.php