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

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 }

Related

How can I check if a method is defined? [duplicate]

This question already has answers here:
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I try to set the value of my variable to "", in the case that getLove is not defined:
`$money = $dollar->getCash()->getLove ?? "";`
But I get still the error message:
Attempted to call an undefined method named "getLove" of class
"Proxies__CG__\App\Entity\Happiness".
Using method_exists, note that this does not take "__call()" in consideration.

Try if there is any $_GET value [duplicate]

This question already has answers here:
How to check if $_GET is empty?
(8 answers)
Closed 4 years ago.
There is a way to determine if there is any $_GET value (not with a specific key) passed in a GET request ? I'm not sure the syntax
isset($_GET)
is valide
$_GET is array so use count instead of isset.
<?php
if(count($_GET) >0){
//valid
}
?>

php check if any of 30 form fields is empty [duplicate]

This question already has answers here:
Check if multiple strings are empty [duplicate]
(7 answers)
Closed 5 years ago.
I have more than 30 fields in a form, and can't believe that I must write an array to check if any of them is empty (likehere)
I need something like js
$_POST[].each function{
if this is empty...
}
and hope that 'php' as so glorified language has something like this or similar.
Try this:
foreach($_POST as $post)
{
if( !empty($post) )
{
// your code here
}
}

shortest way to check, if constant defined and not empty [duplicate]

This question already has answers here:
How to check if a defined constant exists in PHP?
(6 answers)
Closed 6 years ago.
EDIT:
THIS QUESTION hasnt' been already asked, so, before CLOSING, read it at first.
question:
there are 3 possible variations:
define('my_constant', 'something');
// or
define('my_constant', '');
// or
'my_constant' not defined at all
is there shortest way, rather than:
if (defined('my_constant') && my_constant!='')
p.s. if(!empty(my_constant)) throws error if not defined..
if (!defined('MY_CONSTANT')) {
die("constant is not defined");
}
if (empty(MY_CONSTANT)) {
die("constant is defined, but empty");
}
OR
if (!defined('MY_CONSTANT') || empty(MY_CONSTANT)) {
die("constant is not defined or empty");
}
Joomla use this all over the place:
defined('_JEXEC') or die;
Documentation on php.net

Getting Can't use function return value in write context [duplicate]

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

Categories