简体   繁体   中英

I am looking for a php function that will prevent this: Undefined index: message in

if(defined($_POST["message"]) && defined($_POST["name"]))
{

In that if block an exception is thrown constantly. I am looking for a function that will prevent this by testing if it the post variable exists..if it doesnt exists return false instead of throwing an exception

if (isset($_POST["message"]) && isset($_POST["name"])) {
    // ....
}

or if you want to check them to be not only set but also non-empty:

if (!empty($_POST["message"]) && !empty($_POST["name"])) {
    // ....
}

Use isset() for determining if a variable is set. Use defined() to check if a constant is defined with the define() function.

See the Manual:

defined function

isset function

Use isset instead of defined .

if(isset($_POST["message"]) && isset($_POST["name"])) {
[...]

defined takes a string as an argument, and is for checking if a constant exists.

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