1
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
echo "No";
}
else{ echo "Yes"; }

When I go to index.php?view_quiz, it should give result as Yes, but it results as No. Why?

My Other Tries:

(!isset($_GET['new_quiz'] || $_GET['view_quiz'] || $_GET['alter_quiz']))

( ! ) Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in C:\wamp\www\jainvidhya\subdomains\teacher\quiz.php on line 94


(!isset($_GET['new_quiz'],$_GET['view_quiz'],$_GET['alter_quiz']))

NO

Chirag Jain
  • 1,367
  • 1
  • 11
  • 27
paran
  • 199
  • 1
  • 3
  • 18
  • You have to use isset for each GET parameter. As the error message states you can't use the result of an operation as parameter for isset. Do isset(X) || isset(Y) instead of isset(X || Y) – Fitzi May 04 '19 at 18:48
  • @Fitzi That's was my first try – paran May 04 '19 at 18:50
  • Oh, I'm sorry. I kinda missed that first part. – Fitzi May 04 '19 at 18:52
  • @Firtz: That's wrong, you can use `isset()` with more than 1 param. `ìsset(a,b)` is a short way for `isset(a) && isset(b)`. Or here: `!isset(a,b,c)` is a short form of `!isset(a) || !isset(b) || !isset(c)`. – Wiimm May 04 '19 at 20:19
  • Check index.php?view_quiz=something – iazaran May 04 '19 at 20:50
  • @Wimm I did not say that you can't use isset with more than one parameter. I just stated that you can't use the result of an operation as parameter. OP also wasn't trying to check if all parameters are set but if a single one is. Therfore isset with multiple paramters isn't the right choice anyway. My username is Fitzi btw, not Firtz. – Fitzi May 05 '19 at 11:33

5 Answers5

1

You may find than inverting the logic makes the code easier to read, I also like to have a more positive idea of conditions as it can read easier (rather than several nots means no).

So this says if anyone of the items isset() then the answer is Yes...

if(isset($_GET['new_quiz']) || isset($_GET['view_quiz']) || isset($_GET['alter_quiz'])){
    echo "Yes";
}
else{ echo "No"; }

Note that I've changed the Yes and No branches of the if around.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You are probably looking for

if(!isset($_GET['new_quiz']) && !isset($_GET['view_quiz']) && !isset($_GET['alter_quiz'])){
    echo "No";
}
else {
    echo "Yes";
}

which will print Yes if none of new_quiz, view_quiz and alter_quiz are present in the URL. If this is not your desired outcome, please elaborate on your problem.

Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
0

@paran you need to set a value for view_quiz=yes for example

if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
echo "No";
}
else{ echo "Yes"; }

and the url

index.php?new_quiz=yes
index.php?view_quiz=yes
index.php?alter_quiz=yes

All Will return true

0

isset()allows multiple params. If at least 1 param does not exist (or is NULL), isset() returns false. If all params exist, isset() return true.

So try this:

if( !isset( $_GET['new_quiz'], $_GET['view_quiz'], $_GET['alter_quiz']) ) {
Wiimm
  • 2,971
  • 1
  • 15
  • 25
0

First, to answer your question:

When I go to index.php?view_quiz, it should give result as Yes, but it results as No. Why?

This is becaue this

if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){

checks if either one of your parameter is not set, which will always be the case as long as you are not setting all three parameter simultaneously like this:

index.php?alter_quiz&view_quiz&new_quiz

As @nigel-ren stated, you may wan't to change that logic to

if(isset($_GET['new_quiz']) || isset($_GET['view_quiz']) || isset($_GET['alter_quiz'])){
    echo 'Yes';

which checks if at least one parameter is set.


If you wan't to check if there is only one of the three parameters set, you would have to work with XOR (which is slightly more complicated)

$a = isset($_GET['new_quiz']);
$b = isset($_GET['view_quiz']);
$c = isset($_GET['alter_quiz']);

if( ($a xor $b xor $c) && !($a && $b && $c) ){
    echo 'Yes';

(based on this answer: XOR of three values)

which would return true if one and only one of the three parameters is set.


But - and this is just an assumption, please correct me if I'm wrong - I think what you are trying to achieve are three different pages (one for creating a quiz, one for viewing it and one for editing it). Therefore, you will likely run into a problem with your current setup. For example: What would happen if a user calls the page with multiple parameters, like

index.php?alter_quiz&view_quiz

Would you show both pages? Would you ignore one parameter? I would recommend to work with a single parameter to avoid this problem in the first place. For example site which can take the values alter_quiz, view_quiz or new_quiz. E.g.:

index.php?site=alter_quiz

Then you can work like this:

// check if site is set before getting its value
$site = array_key_exists( 'site', $_GET ) ? $_GET['site'] : NULL;

// if it's not set e.g. index.php without parameters is called
if( is_null($site) ){
    // show the start page or something
}else{

    $allowed_sites = ['new_quiz', 'view_quiz', 'alter_quiz'];

    // never trust user input, check if
    // site is an allowed value
    if( !in_array($site, $allowed_sites, true) ){
        die('404 - This site is no available');
    }

    // here you can do whatever your site should do
    // e.g. include another php script which contains
    // your site
    include('path/to/your/site-' . $site . '.php');

    // or echo yes
    echo 'Yes';

}
Fitzi
  • 1,641
  • 11
  • 17