Reputation: 153
Hi can someone help me on what i have done wrong in my conditional statement.
if(isset($_GET['input'] or $_GET['input2'] or $_GET['input3']))
{
$release=$_GET['input'].$_GET['input2'].$_GET['input3'];
echo $release;
}
EDIT: ERROR IS syntax error, unexpected T_LOGICAL_OR, expecting ',' or ')'
Upvotes: 0
Views: 261
Reputation: 2014
This will fix your error:
if(isset($_GET['input']) or isset($_GET['input2']) or isset($_GET['input3']))
{
$release=$_GET['input'].$_GET['input2'].$_GET['input3'];
echo $release;
}
But because you're testing OR instead of AND, all you would need is 1 of those OR tests to come back true to get into the if condition - and if either of the other 2 were unset, you'd still throw a notice / error.
To check that all three exist, you'd do something like
if (isset($_GET['input']) && isset($_GET['input2']) && isset($_GET['input3'])) {
//...
}
This probably does what you're trying to achieve without the messy test:
// count will return however many key=>val pairs are in the array,
// 0 will fail
if (count($_GET)) {
// an empty array and an empty string to hold iterated results
$get = array();
$release = '';
// iterate through $_GET and append each key=>val pair to the local array,
// then concat onto the string
foreach($_GET as $key=>$val) {
$get[$key] = $val;
$release .= $get[$key];
}
echo $release;
}
HTH :)
EDIT: As others indicated, if you need the test to ensure all of those array keys are set, skip the foreach
loop and just do this:
// extract imports all of the array keys into the symbol table.
// It returns the number of symbls imported, and automatically
// sets vars using keys for var name:
if (extract($_GET) && isset($input,$input2,$input3)) {
echo $release = $input.$input2.$input3;
}
(Using multiple arguments passed to isset.)
Upvotes: 2
Reputation: 237837
All the current answers to this question miss an important feature of isset
: it can check for multiple variables at the same time. From the manual:
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 in this case you can do something like this:
if (isset($_GET['input'], $_GET['input2'], $_GET['input3'])) {
This conditional will only pass if all the _GET
elements are set.
Upvotes: 3
Reputation: 16905
You need to make multiple calls to isset
, one for every parameter you want to check!
Keep in mind that or
has a different precedence from ||
, you usually want the latter.
You get a parse error because you pass a compound expression to isset
and not a variable. It's not a regular function, but a separate language construct which is why it has its own arbitrary rules as pointed out by the docs:
isset() only works with variables as passing anything else will result in a parse error.
Perhaps you think that PHP emulates natural languages and you therefore read it as "Is input or input2 or input3 set?"
.
That would be a misconception. If it were valid syntactically, it would logically OR
the values together at first, and then it would pass that one value to isset
—by that time, all connection to your variables is lost and the only thing that is left is either true
or false
onto which isset
would then be applied.
Upvotes: 4
Reputation: 3543
You have to call isset
for each parameter separately. isset()
accepts single parameter and respond accordingly..
For more details check isset.
Bonus Tip: If you are using these variables in database queries, you must use mysqli_real_escape_string() to avoid possible sql injection
Upvotes: 3
Reputation: 21739
if(isset($_GET['input']) || isset($_GET['input2']) || isset($_GET['input3']))
But I actually think you need AND here, not OR, so change || to && if I am correct.
Upvotes: 4