开发者

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

开发者 https://www.devze.com 2023-04-12 06:51 出处:网络
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 tes
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号