Kingkong
Kingkong

Reputation: 73

PHP - check variable is neither empty nor a string

I'm writing the php code which linked with database.

If I put the value on the php code for example

$pid = somevalue.

this some value will pass it to the next php file.

and I want to check this somevalue is not empty or not a string value

if( some condition ){

printf("Invalid input: %s\n", mysqli_connect_error());
echo "<br/><a href='index.php'>Back to previous page</a>";
exit();
}

Upvotes: 2

Views: 142

Answers (4)

Miguel Borges
Miguel Borges

Reputation: 7659

if(!empty($pid) and !is_string($foo)){
   printf("Invalid input: %s\n", mysqli_connect_error());
   echo "<br/><a href='index.php'>Back to previous page</a>";
   exit();
}

Upvotes: 0

ishenkoyv
ishenkoyv

Reputation: 673

$pid  = (int) $pid;
if ($pid) {
} else {
}

Upvotes: 1

nihylum
nihylum

Reputation: 528

Just define it as spoken

if ( ! empty($foo) && ! is_string($foo) ) {

}

Upvotes: 0

N0lf
N0lf

Reputation: 138

use

if (!empty($pid) && is_numeric($pid)) {
    // proceed
} else {
    // error
}

Upvotes: 1

Related Questions