Determine whether the variable has a Value Problem

Source: Internet
Author: User
{Code...} the front-end submits a $ name variable. How can I write this variable if it determines whether $ name has a value or does not exist? Yes: {code...} Or: {code ...}
$name = $_POST['name'];

The frontend submits$nameVariable.$nameIs there a value or does not exist?

Yes:

if(isset($name)){}

Or:

if(!empty($name)){}

Reply content:
$name = $_POST['name'];

The frontend submits$nameVariable.$nameIs there a value or does not exist?

Yes:

if(isset($name)){}

Or:

if(!empty($name)){}

$ Name = isset ($ _ POST ['name'])? Trim ($ _ POST ['name']): ''; if ($ name) {// your code} else {// return false} // trim, if the value to be followed is string but array is passed, an error is returned. Note.

Isset (): // whether the variable has been declared
Empty (): // whether the variable is null

Determine whether $ name has a value or if (empty ($ name) {} Or if (! $ Name) {}, well, above

if(!isset($_POST['name']) || !$_POST['name']){    //todo}

Reference PHP manual:

Empty () is essentially the same! Isset ($ var) | $ var = false is equivalent.

Generally I useif (!isset($name) || empty($name))Judgment.

Note that null,$arr=['name'=>null];var_dump(isset($arr['name']));//false

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.