¿Qué hay de malo en esta simple línea de PHP?

is_int($_GET['pid']) ? define(PRODUCT, $_GET['pid']) : die('Invalid Product Id');

even if the value of pid is an integer I still get the Invalid product id message. Why?

preguntado el 08 de enero de 11 a las 19:01

You can see in the example: var_dump(is_int("23")); huellas dactilares bool(false). And to actually answer your question: Nothing is wrong with your code. Only thing (imo) is that you should not use the ternary operator this way. You should only use it if you actually assign a value (again: imo). -

Not an answer, just advice: don't use the ternary operator ?: for control flow (executing one statement or the other). It is meant to be used as an expression, for example x = (y > 5 ? 'high' : 'low'). In this case, use if (...) {...} else {...}. -

4 Respuestas

Al igual que la manual says: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric ().

Edit: Though it's implied by my answer here, I'll be more specific: $_GET's contents is strings, so an is_int call will always return false. If what you really want to verify here is that the contents of a particular string represents a entero number, you can use a call to is_numeric.

Respondido el 09 de enero de 11 a las 02:01

"...to verify...that the contents of a particular string represents an integer, you can use a call to is_numeric." No. You can verify with is_numeric () only that it's a number, not that it's an integer. See chris's answer. - GZipp

values in the $_GET array are usually type string, but sometimes type array as well.

use ctype_digit() to check if a string is entirely digit characters. optionally, trim() the string first. only use is_numeric() if you want to accept strings in a wide range of formats that are sometimes interpreted numerically, such as decimals, scientific notation, hex strings etc...

Respondido el 08 de enero de 11 a las 23:01

$_GET[] always returns strings, so is_int() siempre evalúa a false. Intentar is_numeric() en vez.

Respondido el 08 de enero de 11 a las 23:01

You were 8 seconds faster, I'll delete my (identical) answer ;-) - Cristóbal

Cualquier cosa en $_GET is string, and is_int("2") devoluciones false.

Respondido el 08 de enero de 11 a las 23:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.