Detect field type sent in PHP POST

Asked

Viewed 45 times

0

There is how to identify the type of field that is coming in a post?

Whether it’s Text or Array?

Example:

I have listbox (select Multiple) and text fields in html, and in it the user may or may not select values, I have a function where I check the values of the fields before proceeding, but when I have this type of field in the middle of the problem, I needed to treat it differently when it comes in the post.

  • 1

    You can use is_array($_POST["nome_do_campo"]);. Returning true, is a array.

1 answer

2

There is the function gettype() that returns a string that identifies the type of data that is the variable passed to the parameter, even if it is an array. You can use this to handle your code.

For example, a gettype([1,2]); would return 'array', gettype(1); would return 'integer', gettype('teste'); would return 'string' and so on. It is also possible to do this for values coming via post, such as gettype($_POST['campo']);.

More in the official documentation >>http://php.net/manual/en/function.gettype.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.