Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if $_POST variable is populated [duplicate]

Tags:

php

Possible Duplicate:
Check if $_POST exists

I'm trying to run something if and only if a $_POST var is populated.

Can I do if(empty($_POST[...])) { ... }? Or should I go about this another way?

like image 451
AKor Avatar asked May 07 '11 19:05

AKor


3 Answers

I'd do if(isset($_POST['key'])) { ... }

like image 196
Rifat Avatar answered Nov 17 '22 20:11

Rifat


No, empty() is not the right way of doing it. You have to use isset().

Why? Because many things are considered empty which you probably don't want to miss!

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

See the manual!

like image 21
markus Avatar answered Nov 17 '22 19:11

markus


You can check $_SERVER['REQUEST_METHOD'] wether it is POST or something else. See $_SERVER.

Ooops, I totally misread your question. Do you want to test for a specific entry in $_POST? Then use array_key_exists($key, $_POST).

like image 2
GodsBoss Avatar answered Nov 17 '22 18:11

GodsBoss