Questions tagged [isset]

Determine if a variable is set and is not NULL

Description [source: https://php.net/manual/en/function.isset.php]

bool isset (mixed$var [,mixed$... ] )

Determine if a variable is set and is not NULL.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

If multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.

1015 questions
13
votes
3 answers

How to implement __isset() magic method in PHP?

I'm trying to make functions like empty() and isset() work with data returned by methods. What I have so far: abstract class FooBase{ public function __isset($name){ $getter = 'get'.ucfirst($name); if(method_exists($this, $getter)) …
Alex
  • 66,732
  • 177
  • 439
  • 641
13
votes
9 answers

What is the difference between null and empty?

I am new to the concept of empty and null. Whilst I have endeavoured to understand the difference between them, I am more confused. I came across an article at http://www.tutorialarena.com/blog/php-isset-vs-empty.php however I still don't see when…
PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103
13
votes
2 answers

PHP object isset and/or empty

Is there a way to check if an object has any fields? For example, I have a soap server I am querying using a soap client and if I call a get method, I am either returned an object containing fields defining the soap query I have made otherwise I am…
Chris
  • 11,780
  • 13
  • 48
  • 70
13
votes
6 answers

Why do I need the isset() function in php?

I am trying to understand the difference between this: if (isset($_POST['Submit'])) { //do something } and if ($_POST['Submit']) { //do something } It seems to me that if the $_POST['Submit'] variable is true, then it is set. Why would I…
zeckdude
  • 15,877
  • 43
  • 139
  • 187
13
votes
4 answers

How to check if php://input is set?

I need to check if php://input exists/isset. Does it work with PHP isset() ? What is the proper way to check it?
Renato Probst
  • 5,914
  • 2
  • 42
  • 45
12
votes
3 answers

Check if variable is set and then echo it without repeating?

Is there a concise way to check if a variable is set, and then echo it without repeating the same variable name? Instead of this: variable)) { echo 'Link'; } ?> I'm thinking…
Juha Untinen
  • 1,806
  • 1
  • 24
  • 40
11
votes
9 answers

Is there a shortcut for the "isset construct"?

I'm writing quite often this line of code: $myParam = isset($params['myParam']) ? $params['myParam'] : 'defaultValue'; Typically, it makes the line very long for nested arrays. Can I make it shorter?
MartyIX
  • 27,828
  • 29
  • 136
  • 207
11
votes
4 answers

Deep Javascript check if undefined without TypeError

I'm tired to write something like if ( typeof Foo != 'undefined' && typeof Foo.bar != 'undefined' && typeof Foo.bar.baz != 'undefined' && Foo.bar.baz == 'qux' ) {...} In PHP it's a little bit better: if (!empty($foo['bar']['baz']) &&…
luchaninov
  • 6,792
  • 6
  • 60
  • 75
10
votes
5 answers

Will isset() trigger __get and why?

class a { function __get($property){...} } $obj = new a(); var_dump(isset($obj->newproperty)); Seems the answer is nope but why?
ORM
  • 465
  • 1
  • 4
  • 8
10
votes
5 answers

Which is better in PHP: suppress warnings with '@' or run extra checks with isset()?

For example, if I implement some simple object caching, which method is faster? 1. return isset($cache[$cls]) ? $cache[$cls] : $cache[$cls] = new $cls; 2. return @$cache[$cls] ?: $cache[$cls] = new $cls; I read somewhere @ takes significant time…
mojuba
  • 11,842
  • 9
  • 51
  • 72
9
votes
5 answers

PHP if multiple variables exist

So i'm having a bit of a problem with having my PHP run a command if multiple variables exist. I made a simple version for people to see what i'm trying to fix easier. Thanks in advance to anyone who can help :)
Spencer May
  • 4,266
  • 9
  • 28
  • 48
9
votes
5 answers

Check if field exists in Input during validation using Laravel

I want to make sure that certain fields are posted as part of the form but I don;t mind if some are empty values. The 'required' validation rule won't work as I am happy to accept empty strings. I have tried the below, but as the 'address2' field is…
eski009
  • 371
  • 1
  • 6
  • 14
8
votes
2 answers

How to know if a PHP variable exists, even if its value is NULL?

$a = NULL; $c = 1; var_dump(isset($a)); // bool(false) var_dump(isset($b)); // bool(false) var_dump(isset($c)); // bool(true) How can I distinguish $a, which exists but has a value of NULL, from the “really non-existent” $b?
FMaz008
  • 11,161
  • 19
  • 68
  • 100
8
votes
5 answers

PHP reading 'get' variable that may or may not have been set

If you try to read the value of a 'get' variable, what happens if said variable had not been set in the URL. Example: you request the page test.php, in that file it tries to read the value of $_GET['message']. What happens in this case? dose the…
thecoshman
  • 8,394
  • 8
  • 55
  • 77
7
votes
1 answer

Equivalent of php isset in python

Here is my python code: if onerow[0]['result'] is None or not result['GetOdds'][0]['result']is None: When result was empty, it returns this error: if onerow[0]['result'] is None or not result['GetOdds'][0]['result']is None: KeyError: 0 I want a…
lord_viper
  • 1,139
  • 1
  • 9
  • 13
1 2
3
67 68