tags:

views:

36

answers:

2

hi all.. can you tell me what is the function of intval? and what about this?

'.intval($_POST['bla']).'
+6  A: 

Why not just read the manual article?

intval — Get the integer value of a variable

This table from that page gives a great example:

echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
Graphain
lol - you're acting like that's the first result if you google 'intval' </sarcasm>
Dan Heberden
I think answers from people are more "user-friendly" then manuals
ggfan
@ggfan have you read the php manuals? They are answers from people and they have major discussion threads about every possible nuance.
Graphain
@Dan Haha I loled
Graphain
+3  A: 

It's an easy way to prevent SQL injections. If your query is like this:

$sql = "DELETE FROM salads WHERE id = " . $_POST['salad_id'];

Then if the user passes the string "id" in instead of an actual id, then all your salads will be deleted!!

But if you wrap intval() around it, it'll convert any bad dressings to the integer zero, and no salads will be affected.

$sql = "DELETE FROM salads WHERE id = " . intval($_POST['salad_id']);
yjerem
I never liked salads anyways
ggfan