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;
}
}
?>