Skip to main content
Active reading [<https://en.wikipedia.org/wiki/%3F:> <https://en.wikipedia.org/wiki/Null_coalescing_operator> <https://en.wikipedia.org/wiki/Sentence_clause_structure#Run-on_sentences>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Using a ternary operator is simple, readable, and clean:

Pre PHP 7

Pre PHP 7
Assign Assign a variable to the value of another variable if it's set, else assign null (or whatever default value you need):

$newVariable = isset($thePotentialData) ? $thePotentialData : null; 

PHP 7+

PHP 7+
The The same except using the Null Coalescing Operatornull coalescing operator. There's no longer a need to call isset() as this is built in, and no need to provide the variable to return as it's assumed to return the value of the variable being checked:

$newVariable = $thePotentialData ?? null; 

Both will stop the NoticesNotices from the OPOP's question, and both are the exact equivalent of:

if (isset($thePotentialData)) { $newVariable = $thePotentialData; } else { $newVariable = null; } 

 
If If you don't require setting a new variable then you can directly use the ternary'sternary operator's returned value, such as with echo, function arguments, etc.:

Echo:

Echo:

echo 'Your name is: ' . isset($name) ? $name : 'You did not provide one'; 

Function:

Function:

$foreName = getForeName(isset($userId) ? $userId : null); function getForeName($userId) { if ($userId === null) { // Etc } } 

The above will work just the same with arrays, including sessions, etc., replacing the variable being checked with e.g.:
  

$_SESSION['checkMe']
or

Or however many levels deep you need, e.g.:
  

$clients['personal']['address']['postcode']


 

 

Suppression:

Suppression:

It is possible to suppress the PHP Notices with @ or reduce your error reporting level, but it does not fix the problem, it. It simply stops it being reported in the error log. This means that your code still tried to use a variable that was not set, which may or may not mean something doesn't work as intended - depending on how crucial the missing value is.

You should really be checking for this issue and handling it appropriately, either serving a different message, or even just returning a null value for everything else to identify the precise state.

If you just care about the Notice not being in the error log, then as an option you could simply ignore the error log.

Using a ternary is simple, readable, and clean:

Pre PHP 7
Assign a variable to the value of another variable if it's set, else assign null (or whatever default value you need):

$newVariable = isset($thePotentialData) ? $thePotentialData : null; 

PHP 7+
The same except using Null Coalescing Operator. There's no longer a need to call isset() as this is built in, and no need to provide the variable to return as it's assumed to return the value of the variable being checked:

$newVariable = $thePotentialData ?? null; 

Both will stop the Notices from the OP question, and both are the exact equivalent of:

if (isset($thePotentialData)) { $newVariable = $thePotentialData; } else { $newVariable = null; } 

 
If you don't require setting a new variable then you can directly use the ternary's returned value, such as with echo, function arguments, etc:

Echo:

echo 'Your name is: ' . isset($name) ? $name : 'You did not provide one'; 

Function:

$foreName = getForeName(isset($userId) ? $userId : null); function getForeName($userId) { if ($userId === null) { // Etc } } 

The above will work just the same with arrays, including sessions etc, replacing the variable being checked with e.g.:
 $_SESSION['checkMe']
or however many levels deep you need, e.g.:
 $clients['personal']['address']['postcode']


 

 

Suppression:

It is possible to suppress the PHP Notices with @ or reduce your error reporting level, but it does not fix the problem, it simply stops it being reported in the error log. This means that your code still tried to use a variable that was not set, which may or may not mean something doesn't work as intended - depending on how crucial the missing value is.

You should really be checking for this issue and handling it appropriately, either serving a different message, or even just returning a null value for everything else to identify the precise state.

If you just care about the Notice not being in the error log, then as an option you could simply ignore the error log.

Using a ternary operator is simple, readable, and clean:

Pre PHP 7

Assign a variable to the value of another variable if it's set, else assign null (or whatever default value you need):

$newVariable = isset($thePotentialData) ? $thePotentialData : null; 

PHP 7+

The same except using the null coalescing operator. There's no longer a need to call isset() as this is built in, and no need to provide the variable to return as it's assumed to return the value of the variable being checked:

$newVariable = $thePotentialData ?? null; 

Both will stop the Notices from the OP's question, and both are the exact equivalent of:

if (isset($thePotentialData)) { $newVariable = $thePotentialData; } else { $newVariable = null; } 

If you don't require setting a new variable then you can directly use the ternary operator's returned value, such as with echo, function arguments, etc.:

Echo:

echo 'Your name is: ' . isset($name) ? $name : 'You did not provide one'; 

Function:

$foreName = getForeName(isset($userId) ? $userId : null); function getForeName($userId) { if ($userId === null) { // Etc } } 

The above will work just the same with arrays, including sessions, etc., replacing the variable being checked with e.g.: 

$_SESSION['checkMe']

Or however many levels deep you need, e.g.: 

$clients['personal']['address']['postcode']


Suppression:

It is possible to suppress the PHP Notices with @ or reduce your error reporting level, but it does not fix the problem. It simply stops it being reported in the error log. This means that your code still tried to use a variable that was not set, which may or may not mean something doesn't work as intended - depending on how crucial the missing value is.

You should really be checking for this issue and handling it appropriately, either serving a different message, or even just returning a null value for everything else to identify the precise state.

If you just care about the Notice not being in the error log, then as an option you could simply ignore the error log.

Source Link
James
  • 4.8k
  • 5
  • 39
  • 50

Using a ternary is simple, readable, and clean:

Pre PHP 7
Assign a variable to the value of another variable if it's set, else assign null (or whatever default value you need):

$newVariable = isset($thePotentialData) ? $thePotentialData : null; 

PHP 7+
The same except using Null Coalescing Operator. There's no longer a need to call isset() as this is built in, and no need to provide the variable to return as it's assumed to return the value of the variable being checked:

$newVariable = $thePotentialData ?? null; 

Both will stop the Notices from the OP question, and both are the exact equivalent of:

if (isset($thePotentialData)) { $newVariable = $thePotentialData; } else { $newVariable = null; } 

 
If you don't require setting a new variable then you can directly use the ternary's returned value, such as with echo, function arguments, etc:

Echo:

echo 'Your name is: ' . isset($name) ? $name : 'You did not provide one'; 

Function:

$foreName = getForeName(isset($userId) ? $userId : null); function getForeName($userId) { if ($userId === null) { // Etc } } 

The above will work just the same with arrays, including sessions etc, replacing the variable being checked with e.g.:
$_SESSION['checkMe']
or however many levels deep you need, e.g.:
$clients['personal']['address']['postcode']


 

Suppression:

It is possible to suppress the PHP Notices with @ or reduce your error reporting level, but it does not fix the problem, it simply stops it being reported in the error log. This means that your code still tried to use a variable that was not set, which may or may not mean something doesn't work as intended - depending on how crucial the missing value is.

You should really be checking for this issue and handling it appropriately, either serving a different message, or even just returning a null value for everything else to identify the precise state.

If you just care about the Notice not being in the error log, then as an option you could simply ignore the error log.

Post Made Community Wiki by James