Is array_key_exists('submit_input_name',$_

2019-08-30 07:43发布

问题:

I've got a handle on MySQL and HTML, but am still learning PHP. I'm on a bit of a schedule, so when I noticed that Dreamweaver would write PHP for me I started using that feature. I immediately noticed that, of course, the code it inserts isn't that great.

When investigating "Notice: Undefined index:" I came across PHP error: Notice: Undefined index:.

DeaconDesperado pointed out that alibenmessaoud's code was trying to process before post values were set. So I looked into my code for the same problem and noticed that Dreamweaver is using

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "name_of_your_submit_input"))

instead of

if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))

Am I misunderstanding Dreamweaver's code? Isn't checking if the post is the submit name the same as checking if it exists? Am I misunderstanding array_key_exists()? Last question, does it matter that my check is above the form itself?

Thanks for putting up with a newbie who hasn't finished the w3schools PHP tutorial yet.

回答1:

The two examples you've given don't do the same thing, therefore neither is better.

Your first example is asking whether key MM_insert exists in $_POST and that it's value is name_of_your_submit_input.

Whereas your second example is asking whether the key name_of_your_submit_input exists, which could also look like this:

if (isset($_POST["name_of_your_submit_input"]))

In any case, both examples wouldn't cause that PHP Notice.