Cannot use isset() in the output of a PHP expression

Asked

Viewed 38 times

-1

I want when my site is accessed with the reference? ref=app show a page, if it is not going to appear another.

But just make that mistake:

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" 
instead) in D:\XAMP\htdocs\baixarmp3\wp-content\themes\b4-baixar-mp3\single.php on line 61

My code from line 61:

if (isset($_GET["ref"] == "app")) {

My complete code:

<?php
    if (isset($_GET["ref"] == "app")) {
        include ('single/single-app.php');
    }
    else {
        include ('single/single-site.php');
    }
?>
  • 3

    'Cause you don’t do if ($_GET["ref"] == "app") {? It makes little sense to check whether a boolean (the result of the expression in question) is defined. An expression will always be "defined"...

1 answer

3


The method isset() only checks whether or not the variable has some value.

<?php

isset($a); // retorna false, pois $a não possui valor, ou seja, é null
$a = 3
isset($a); // retorna true, pois agora $a possui valor

As for your code, there are two ways to do:

1st: removing the isset():

<?php
    if ($_GET["ref"] == "app")) {
        include ('single/single-app.php');
    } else {
        include ('single/single-site.php');
    }
?>

2º: adding the isset() as a validation parameter:

<?php
    if (isset($_GET["ref"]) && $_GET["ref"] == "app") {
        include ('single/single-app.php');
    } else {
        include ('single/single-site.php');
    }
?>

Both will deliver the same idea: if the parameter ref be equal to app, will include single-app.php, otherwise, being null or not, it shall include single-site.php.

  • I still have this error: Parse error: Unmatched ')' in D: XAMP htdocs baixarmp3 wp-content themes B4-baixar-mp3 single.php on line 61 Print: https://prnt.sc/xdtfig 02: https://prnt.sc/xdtgbg

  • @Matheusvitor the if() had an extra parenthesis, try now

  • It worked, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.