Rodolfo Lavandino
Rodolfo Lavandino

Reputation: 53

PHP syntax explanation

How can I translate to PHP syntax something like that:

if(isset($_GET['q'])//but q=is not empty or <2 because in this case redirect to) { do this } else { do this one }

I hope it's not too massy.

Thanks.

updated question

Why this code does not redirect ?

    if(isset($_GET['q']))
{ 
    if(!empty($_GET['q']) || $_GET['q']>2)
    {

    $q = $_GET['q'];
    $q = mysql_real_escape_string($q);
    $sql = $db->prepare ('SELECT * FROM t WHERE a = :a');
    $sql->bindParam(':a',$q);
    $sql->execute();

    }

    else

    {

      header("Location:somepage.php");

    }

} else {

$sql = $db->prepare ('SELECT * FROM t ORDER BY b');
$sql->execute();

}

Upvotes: 1

Views: 120

Answers (3)

Thiefbrain
Thiefbrain

Reputation: 26

I assume q is a number here.

if (isset($_GET['q']) {
    if (empty($_GET['q'] || $_GET['q'] < 2)) {
        // do redirect here
    } else {
        // do mysql here
    }
}

Upvotes: 1

inquam
inquam

Reputation: 12932

What? Do you mean the code for the "text" you have?

if(isset($_GET['q']) && (!empty($_GET['q']) || $_GET['q'] < 2)) {
  // redirect here using header("location: foo.php") or some other function if you are using some framework.
}
else {
  /otherwise do something else.
}

Update to your updated question:

Because $_GET['q'] isn't empty? Do a var_dump of $_GET so we, and you, know what's in there. It's impossible to tell otherwise. But I think your code is doing exactly what you have told it to do. You just don't have the complete picture of what you want to accomplish.

Upvotes: 2

Do like this

<?php
if(isset($_GET['q']))
{
    if(!empty($_GET['q']) || $_GET['q']<2)
    {
        header("location:somepage.php");
    }
    else
    {
        echo "Cannot be redirected";
    }
}

Upvotes: 2

Related Questions