简体   繁体   中英

Checking if variable is set, not null and not empty

I'm using isset to check if a variable is set and not null, then I process it (eg send it via email or to a Google Spreadsheet). Sometimes I receive empty results, like if the variable is null or empty, not even an empty space. Why is this happening? Am I missing something?

    if(isset($_POST["name"]) && isset($_POST["phone"])){
            $name  = $_POST["name"];
            $phone  = $_POST["phone"];
            $message="Nom: $name \nPhone: $phone \n";

            require 'PHPMailer/PHPMailerAutoload.php';
            $mail = new PHPMailer;
            $mail->setFrom('from@mail.com', 'Name From');
            $mail->addAddress('to@mail.com', 'Name To');
            $mail->Subject  = 'Subject';
            $mail->Body     = $message;
      }

!empty($_POST["name"]) it check the variable have a value. if a the name is not empty then proceed with other code.

if(isset($_POST["name"]) && !empty($_POST["name"]) && isset($_POST["phone"]) !empty($_POST["phone"])){
        $name  = $_POST["name"];
        $phone  = $_POST["phone"];
        $message="Nom: $name \nPhone: $phone \n";

        require 'PHPMailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('from@mail.com', 'Name From');
        $mail->addAddress('to@mail.com', 'Name To');
        $mail->Subject  = 'Subject';
        $mail->Body     = $message;
  }

another option

if($_POST["name"] != "") 

if the variable have a null string the code inside the if, not proceeded.

Additional reference- In where shall I use isset() and !empty()

isset() returns TRUE if var exists and has value other than NULL, FALSE otherwise.

ex: $var="";

isset($var) will return true;

I suggest of using !empty() instead of isset().

Echoing both of the sentiments expressed in the preceding answers here, I'd observe that "the ever-helpful PHP language provides a variety of functions that 'try to be helpful' in various situations." But, those "various situations" can be quite different! Thus, "one size" does not "fit all."

For instance, if you're dealing with an HTTP GET/POST data-stream, your first order-of-business should be to determine whether the web-client provided a particular variable in its response. If you determine that it did, then you should thereafter treat it as what it is: "a character string." Move right along, then (IMHO ...) to Jees Denny's "other option" of testing whether that string is empty.

Tests for "NULL," on the other hand, really should apply only to the results from database queries. In this case, you know that a value has been provided by the SQL server, because you know that the server will provide some value for every column that you request. In this case, you first want to know whether that value is or isn't NULL . (Which value, kindly note, "is not the same as 'an empty string.'")

Therefore, even though the PHP language provides many "apparently-very-similar functions, all of which you of-course can use," you should make it a point only to use the ones that are most-closely related to the actual situation that you know that you are dealing with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM