Is it possible to add the "OR" operator to php isset submit form code when there are two submit buttons with different names?
Eg:
if (!isset($_POST['submitRequest'] || $_POST['submit']))
The above code prompts and an error. Please advice.
try:
if(isset($_POST['submitRequest']) || isset($_POST['submit'])){
//do something
}
No, it isn't possible. You need each variable in its own isset functioYou cannot combine them.
That's not how to use the or operator. You have to put it between two isset calls.
if ( ! isset($_POST['submitRequest']) || isset($_POST['submit']) ) { }
You always have to take a good look at the syntax. Remember that the or operator is used to make an entire condition evaluate to true if the left condition or the right condition is true. As such, you always need to use the or operator with two conditions.
Also, if you want to do this with the and operator, you can just use one isset call.
if ( isset($_POST['username'], $_POST['password']) ) { }
You need to call the isset function on respective variables individually. You have an option of using either of the two syntax, depending on the convention you follow.
if(isset($_POST['submitRequest']) || isset($_POST['submit'])){
//do something
}
if(isset($_POST['submitRequest']) OR isset($_POST['submit'])){
//do something
}
You can use isset() on multiple variables:
if(isset($_POST['submitRequest']) || isset($_POST['submit'])) {
// One of them was pushed
}
Be careful when passing multiple arguments to isset(). As the manual documents:
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.
So isset($_POST['submitRequest'], $_POST['submit']) would be the equivalent of isset($_POST['submitRequest']) && isset($_POST['submit']). However, that second sentence can be useful if you want to check !isset($_POST['submitRequest']) || !isset($_POST['submit']). Since when passed multiple arguments, isset() will return false for the first unset one, you can use !isset($_POST['submitRequest'], $_POST['submit']).
to elaborate a bit:
isset(), empty() and a few other are not functions, but language constructs. As such, they don't work in all the ways one might expect according to them looking like functions.
The important part here is: they only check variables, they can't and don't evaluate the results of expressions.
The use of || makes $_POST['submitRequest'] || $_POST['submit'] an expression. The result is only calculated temporarily, not stored in defined variable which can be set or empty.
I know I had misunderstood the original post.
if(isset($_POST['submitRequest'])||isset($_POST['submit'])){
// you can do something here
}
Original (incorrect)
You can use this:
if(!isset($_POST['submitRequest'], $_POST['submit'])){
die("Fail");
}
If multiple parameters are supplied, the function will check all of them. This will return TRUE only if all of the parameters are set.
Reference: http://php.net/manual/en/function.isset.php
Related
A guy learning PHP sent me some code that has me scratching my head. He's getting $_POST input, putting it in variables, and then:
if( !empty($id && $name && $email) ) {
//do something
}
My first inclination was that passing multiple variables as an argument would throw an error, but it evaluates successfully. Am I incorrect that empty() should NOT take a boolean expression? Or - if I'm right - why does it work?
You can pass an expression to empty rather than a variable (since PHP 5.5), but with an expression like that you lose half of the benefit of using empty. empty checks that variables are set as well as evaluating their "truthiness". When you give it an expression like that, the individual variables within the expression are not checked to exist by empty. The expression is just evaluated as a boolean.
So if you used separate empty checks, you would get the check that the variables exist as well as the check that they are != false
if(!empty($id) && !empty($name) && !empty($email))
But when you use
if (!empty($id && $name && $email))
You will still get into the if block if all the variables are set and have non-false values, but you'll get undefined variable notices if any of them are not set. It's basically the same thing as not using empty at all, like this:
if ($id && $name && $email)
But if your guy is setting these variables from $_POST, they will be set, (if they weren't in $_POST he'd get undefined index warnings at that point) and the empty here is pointless anyway.
I actually found out how to solve this particular problem on my own, but it's still driving me crazy wondering why the problem came about to begin with. I had a conditional statement:
if($_SESSION['authenticated'] = 1) {
DOSTUFF;
}
Now prior to this if statement I know that $_SESSION['authenticated'] is empty by using print_r(). However, after executing this code block this conditional statement assigns 1 to $_SESSION['authenticated'], which makes the if statement evaluate to true no matter what! I found a way around this using isset(), but I still have no clue why a conditional statement would assign a value to a variable in the first place when it should only evaluate whether or not the condition is true or false.
Because = is assignment. You want == or === which test for equality. === checks that the operands are both equal and of the same type. == only checks for equality.
You have a semantic (or syntactic) (or typing) error. You should use double equal sign for equality comparison like this:
if($_SESSION['authenticated'] == 1) {
DOSTUFF;
}
If you use single equality sing, that means assignment, and the assigned value gets evaluated in the if statement.
Both these statements are true:
$_POST['foo'] = $_POST['bar'] = 'some string';
//1. with '&&' operator
if(isset($_POST['foo']) && isset($_POST['bar'])) {
echo true;
}
//2. with a comma
if(isset($_POST['foo'], $_POST['bar'])) {
echo true;
}
What is the difference (if any) between them?
There IS a difference, in practice. The meaning should be the same, however the "comma operator" version implements a "complete boolean evaluation" in this case. That is, if the first variable is not set, php won't look at the second since they're in a && relationship and the result can't be true anymore. (This is called a "short circuit" eval) In the second case, php must calculate both arguments before calling isset(...) so both values will be checked.
It's just the principle, yes, but sometimes it's very important, for example if the operands are function calls.
(Just a short reply to the commenter saying "isset does not take function calls" - it's not about isset, it's about implementing expressions in general. Stop calculating things as soon as the result is obvious, and spare yourself as many partial results as you can. Function arguments will do the opposite: they all get calculated before they get passed to the subroutine.)
There's no difference according to the PHP documentation: isset() function. Indeed, isset can take an infinity of argument and returns true if every variable passed exists. It's similar to test if each isset() of each variable is true.
The theory should be check, but the function takes only variable in argument as said by the doc:
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 there's no problem about priority of the compute of arguments.
Finally, be aware that the comma here isn't an operator. The comma here is used to separated arguments of the isset function. The previous explanation doesn't work with empty() for example since the empty function only takes 1 argument.
TL;DR: isset($a, $b) == isset($a) && isset($b), but empty($a, $b) is a syntax error.
The isset() function can accept multiple arguments. If multiple arguments are supplied, then it only returns true if all of them are set.
http://www.php.net/manual/en/function.isset.php
There's no difference, except for the fact that you are calling isset() twice in 1., effectively evaluating both returning values with the && operator, in 2. you are just using isset() with two arguments instead of one, separated with a comma.
i recently had to do a "test" for a job, and i got feed back saying that this statement was incorrect:
$images = $flickr->get_images(5, !empty($_GET['pg']) ? $_GET['pg'] : 1);
The "supposed" error was generated via the ternary operator on the first time the page was loaded, as there was no "?pg=1" (or whatever) passed via the query string.
The feed back said i should have used isset instead. I have looked at various posts both here (question 1960509) and blogs, but cannot find any definitive answer.
Is this really an error? How can i replicate this issue? do i need to put on E_STRICT or something in my php.ini file? Or might this be due to an older version of php?
Note: please don't tell me about how i should validate things.. i know this... it was a test to just see if i could use the flickr api calls.
This is perfectly fine. empty is not an actual function, it's a language construct. It does not issue a warning if a variable is not set (in that case the variable is considered empty, thus the 'function' returns TRUE just as you want), and additionally it checks for empty or zero values.
You could see empty as a normal isset check with an additional loose comparison to FALSE:
empty($var) === (!isset($var) || $var == FALSE)
$images = $flickr->get_images(5, (isset($_GET['pg']&&($_GET['pg']))) ? $_GET['pg'] : 1);
without isset you'll get error so combine them
I'd use
$images = $flickr->get_images(5, array_key_exists('pg', $_GET) ? $_GET['pg'] : 1);
Combine with !empty($_GET['pg']) if needed (i.e. array_key_exists('pg', $_GET) && !empty($_GET['pg'])), but array_key_exists is the intended function for this job.
I think in a situation like this isset is the correct function to use as it is checking the existence of the array element rather than checking if the value of the element has been set. As Martin notes, the best thing to do here is combine them as this will only check the value if the element exists, meaning that the error will not occur on the first page load.
Also, I think this will only give a warning if E_NOTICE is on (or perhaps E_WARNING as well)
The reason you would get an error is because the empty function is designed to check the value of an existing variable, whearas isset() is designed to tell you whether a variable has been instantiated, however because empty() is a language construct technically it doesn't throw an error or create a warning so most people don't see the difference.
From the docs:
empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.
isset — Determine if a variable is set and is not NULL. So "isset" is the correct function to use for checking for value is set or not.
More details :http://php.net/manual/en/function.isset.php
I have a URL that would be like so:
http://mysite.com/index.php?somename=somevalue
I usually check that somename exists by doing:
if (isset($_GET['somename'])) {
However, I have seen a lot of people do this lately:
if ($_GET['somename'] != NULL) {
My question is, is either way better than the other?
Unlike the null check,
isset($_GET['somename']
will not throw an "undefined index" notice, so it's definitely the more preferable of the two.
array_key_exists("somename", $_GET);
would also be valid.
You may want to combine this with a null check however if you want to disallow empty values.
These statements are different.
isset() will check if a variable contains a value, whereas the second statement checks if the variable is equal to NULL or not.
In your case, use isset() to determine if $_GET['somename'] exists or not.
The best way is usually to do both. The isset checks if the variable exists, while you should also check to see if it is not NULL so that it won't break any later checks.
if(isset($_GET['somevalue']) && $_GET['somevalue'] != NULL)