Issue with isset() inside nested ternary operator [duplicate] - php

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'));

Related

Checking if variable exist and if it has a certain value in one line [duplicate]

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) { }

Which kind of purpose was for returning 1 in print from PHP lenguage? [duplicate]

This question already has answers here:
Why does print return (int) 1?
(6 answers)
Closed 2 years ago.
Could you explain why 'print' in PHP return 1?
For what? Was it the result of simple creativity? Or maybe, "we have created, and you can do with that whatever you wish"?
For example,
$a = print('4');
var_dump($a);
From the docs
Returns 1, always.
Why Should I use print? What kind of Advantage?
Both print and echo are language constructs and echo is even faster than print. But the advantage of print is that it is more flexible than echo and it can do anything that echo can do.
The simplest example is, print can be used in an expression like ternary.
($someValue) ? print('somevalue is True') : print('someValue is false');
This is because as I stated above print always returns 1 and echo doesn't have a return value.
Read Official Documentation Here:
PHP print
PHP echo

PHP isset(null['?']) [duplicate]

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?

What's the best way of checking if an object property in php is undefined? [duplicate]

This question already has answers here:
Check if a variable is undefined in PHP
(8 answers)
Closed 4 years ago.
What's the best way of checking if an object property in php is undefined?
You can use is_null
Or
!isset($object)
Example :
I want to check if the input is undefined. So, I can show errors.
if (!isset($_POST['myInput'])) { echo "error"; } else { // do the code }

PHP nested IF ELSE Short hand [duplicate]

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 6 years ago.
I'm trying to display a correct results based on the value of the variable but I'm using a short hand,
I have tried the following but its seems to ignore the first check even if the value is correct.
(!empty($national_ID_number)) ?
$national_ID_number :
(!empty($foreign_ID_number)) ?
$foreign_ID_number :
$temporal_permit_number)
Thanks
It's populating correctly for me with this code, you seem to have added additional bracket.
$national_ID_number = '';
$foreign_ID_number = 2;
$temporal_permit_number = 3;
$finalID = (!empty($national_ID_number)) ? $national_ID_number : (!empty($foreign_ID_number ) ? $foreign_ID_number : $temporal_permit_number);
echo $finalID;
I just went for a bit deep research - apparently each condition needs to be inside brackets and the below fix works fine.
(!empty($national_ID_number)) ?
$national_ID_number :
((!empty($foreign_ID_number)) ?
$foreign_ID_number :
$temporal_permit_number));
Reference and PHP Operator precedence

Categories