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.

977 questions
599
votes
25 answers

JavaScript isset() equivalent

In PHP you can do if(isset($array['foo'])) { ... }. In JavaScript you often use if(array.foo) { ... } to do the same, but this is not exactly the same statement. The condition will also evaluate to false if array.foo does exists but is false or 0…
Bart van Heukelom
  • 40,403
  • 57
  • 174
  • 291
193
votes
17 answers

Best way to test for a variable's existence in PHP; isset() is clearly broken

From the isset() docs: isset() will return FALSE if testing a variable that has been set to NULL. Basically, isset() doesn't check for whether the variable is set at all, but whether it's set to anything but NULL. Given that, what's the best way to…
chazomaticus
  • 14,786
  • 4
  • 28
  • 31
113
votes
18 answers

In where shall I use isset() and !empty()

I read somewhere that the isset() function treats an empty string as TRUE, therefore isset() is not an effective way to validate text inputs and text boxes from a HTML form. So you can use empty() to check that a user typed something. Is it true…
shin
  • 28,830
  • 62
  • 170
  • 248
107
votes
15 answers

If isset $_POST

I have a form on one page that submits to another page. There, it checks if the input mail is filled. If so then do something and if it is not filled, do something else. I don't understand why it always says that it is set, even if I send an empty…
Nrc
  • 8,463
  • 14
  • 50
  • 95
93
votes
8 answers

Check if value isset and null

I need to check if value is defined as anything, including null. isset treats null values as undefined and returns false. Take the following as an example: $foo = null; if(isset($foo)) // returns false if(isset($bar)) // returns…
Tatu Ulmanen
  • 115,522
  • 31
  • 176
  • 180
87
votes
2 answers

PHP shorthand for isset()?

Is there a shorthand way to assign a variable to something if it doesn't exist in PHP? if(!isset($var) { $var = ""; } I'd like to do something like $var = $var | "";
brentonstrine
  • 17,958
  • 23
  • 61
  • 112
77
votes
11 answers

is there something like isset of php in javascript/jQuery?

Is there something in javascript/jQuery to check whether variable is set/available or not? In php, we use isset($variable) to check something like this. thanks.
gautamlakum
  • 11,035
  • 22
  • 61
  • 87
67
votes
3 answers

Test if an argument of a function is set or not in R

I have a function f that takes two parameters (p1 and p2): If for the parameter p2 no value was passed to the function, the value of p1^2 should be used instead. But how can I find out within the function, if a value is given or not. The problem is…
R_User
  • 9,332
  • 22
  • 68
  • 115
45
votes
6 answers

What's the difference between 'isset()' and '!empty()' in PHP?

I don't understand the difference between isset() and !empty(). Because if a variable has been set, isn't it the same as not being empty?
Vitalynx
  • 757
  • 1
  • 6
  • 14
41
votes
13 answers

PHP: Check if variable exist but also if has a value equal to something

I have (or not) a variable $_GET['myvar'] coming from my query string and I want to check if this variable exists and also if the value corresponds to something inside my if statement: What I'm doing and think is not the best way to…
Mariz Melo
  • 740
  • 1
  • 11
  • 18
40
votes
7 answers

Calling a particular PHP function on form submit

I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)
Piklu Guha
  • 519
  • 2
  • 7
  • 8
35
votes
8 answers

isset PHP isset($_GET['something']) ? $_GET['something'] : ''

I am looking to expand on my PHP knowledge, and I came across something I am not sure what it is or how to even search for it. I am looking at php.net isset code, and I see isset($_GET['something']) ? $_GET['something'] : '' I understand normal…
user1625186
  • 353
  • 1
  • 3
  • 4
34
votes
11 answers

What is the PHP shorthand for: print var if var exist

We've all encountered it before, needing to print a variable in an input field but not knowing for sure whether the var is set, like this. Basically this is to avoid an e_warning. How can I…
bart
  • 13,952
  • 20
  • 65
  • 93
32
votes
9 answers

Check if an array item is set in JS

I've got an array var assoc_pagine = new Array(); assoc_pagine["home"]=0; assoc_pagine["about"]=1; assoc_pagine["work"]=2; I tried if (assoc_pagine[var] != "undefined") { but it doesn't seem to work I'm using jquery, I don't…
Gusepo
  • 737
  • 1
  • 9
  • 25
29
votes
3 answers

PHP - Checking if array index exist or is null

Is there a way to check if an array index exists or is null? isset() doesn't tell you whether the index doesn't exist or exists but is null. If I do : isset($array[$index]) || is_null($array[$index]) it won't work because if the index doesn't exist…
Virus721
  • 7,156
  • 8
  • 49
  • 110
1
2 3
65 66