This question already has answers here:
Why does PHP not complain when I treat a null value as an array like this?
(4 answers)
php undefined index notice not raised when indexing null variable
(2 answers)
Closed 2 years ago.
The isset() documentation clearly tells me that
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
So, echo (isset(null)) ? 'YES' : 'NO'; is giving the fatal error as excepted;
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Question;
Why does isset(null['?']) does not trow the same error;
echo (isset(null['?'])) ? 'YES' : 'NO';
NO
I'm expecting the same error, since isset() can't be used on non-variables.
I'm aware that
<?php
$x = NULL;
$x['?'] = 'hi';
echo gettype($x); // array
will set $x to an array, however, (as expected) echo #gettype(null['1']); stays null?
Related
This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 9 months ago.
I have always thought that if I want to check if a variable exists and has a certain value I have to use two if conditions:
if(isset($x)){
if($x->age==5){}
}
But I realized its also possible to do it in one line this way:
if(isset($x) && ($x->age==5)){}
Can someone tell me why the second variation will not result in an error if $x is null. Given that $x is null and doesn't have the property age? Would it be trying to access a property that doesn't exist?
$x=null;
Because $x is null, isset($x) is false. Then, because of the logical operator "AND" (&&), the condition cannot be fully validated, so, the test is stopped here and ($x->age==5) is not executed.
For a shorter code, as of PHP 8.0.1, you can use the NullSafe Operator (?->)
if ($x?->age == 5) { }
This question already has answers here:
Trying to access array offset on value of type null
(3 answers)
Closed 3 years ago.
I'm getting this error on multiple occasion in a script (invoiceplane) I have been using for a few years now but which hasn't been maintained unfortunately by its creators:
Message: Trying to access array offset on value of type null
My server has been upgrade to PHP 7.4 and I'm looking for a way to fix the issues and maintain the script myself since I'm very happy with it.
This is what's on the line that gives the error:
$len = $cOTLdata['char_data'] === null ? 0 : count($cOTLdata['char_data']);
$cOTLdata is passed to the function:
public function trimOTLdata(&$cOTLdata, $Left = true, $Right = true)
{
$len = $cOTLdata['char_data'] === null ? 0 : count($cOTLdata['char_data']);
$nLeft = 0;
$nRight = 0;
//etc
It's included in mpdf btw, but simply overwriting the files from the github repository did not fix the errors.
This happens because $cOTLdata is not null but the index 'char_data' does not exist. Previous versions of PHP may have been less strict on such mistakes and silently swallowed the error / notice while 7.4 does not do this anymore.
To check whether the index exists or not you can use isset():
isset($cOTLdata['char_data'])
Which means the line should look something like this:
$len = isset($cOTLdata['char_data']) ? count($cOTLdata['char_data']) : 0;
Note I switched the then and else cases of the ternary operator since === null is essentially what isset already does (but in the positive case).
This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I am trying to show a input field value using if-else login and I want to use single line ternary operator for it but its always returning the error. However if I use if-else it is working fine. My code is :
Working Fine
if(isset($_COOKIE['m_name'])){
echo $_COOKIE['m_name'];
}else if(isset($this->userdata)){
echo $this->userdata[0]->first_name;
}else{
echo 'No Data';
}
Returning Notice Always
echo isset($_COOKIE['m_name'])? $_COOKIE['m_name']: isset($this->userdata)?$this->userdata[0]->first_name:'No Value';
Error :
Notice: Trying to get property of non-object in /home/.... on line...
I don't know what i am missing?
Any help would be greatly appreciated.
I'm not sure, but I think it should be
echo (isset($_COOKIE['m_name'])? $_COOKIE['m_name']: (isset($this->userdata)?$this->userdata[0]->first_name:'No Value'));
This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 6 years ago.
I get the error in the subject. Also I spent ages on google and found dozens of resources having the same error, but still I can't figure out what the issue is.
This is my code:
<?php
if(empty(trim($_POST["user"])) || empty(trim($_POST["text"]))) {
echo "no luck";
}
?>
PHP Fatal error: Can't use function return value in write context in
/var/www/test.php on on line 2
If you refer to a manual, you will see
Determine whether a variable is considered to be empty.
The result of trim passed to empty is not a variable.
So your options are:
$user = trim($_POST['user']);
if (!empty($user)) { }
Or php5.5, in which
empty() now supports expressions
This question already has answers here:
Can't use method return value in write context
(8 answers)
Closed 8 years ago.
I have a if statement check to see if a string is empty
if(empty(strlen(trim($_POST['name'])))){
$error_empty = true;
}
gives me this error:
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\requestaccess\index.php on line 51
empty is not a function -- it's a "language construct" that prior to PHP 5.5 can only be used to evaluate variables, not the results of arbitrary expressions.
If you wanted to use empty in exactly this manner (which is meaningless) you would have to store the result in an intermediate variable:
$var = strlen(trim($_POST['name']));
if(empty($var)) ...
But you don't need to do anything like this: strlen will always return an integer, so empty will effectively be reduced to checking for zero. You don't need empty for that; zero converts to boolean false automatically, so a saner option is
if(!strlen(trim($_POST['name']))) ...
or the equivalent
if(trim($_POST['name']) === '') ...