开发者

learning intval

开发者 https://www.devze.com 2023-01-06 01:28 出处:网络
can you tell me what is the function of in开发者_如何学运维tval? and what about this? \'.intval($_POST[\'bla\']).\'

can you tell me what is the function of in开发者_如何学运维tval? and what about this?

'.intval($_POST['bla']).'


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


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']);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号