note 89828 added to reserved.variables.post

From: Date: Wed, 25 Mar 2009 03:34:38 +0000
Subject: note 89828 added to reserved.variables.post
Groups: php.notes 
Request: Send a blank email to php-notes+get-152412@lists.php.net to get a copy of this message
Take note that all $_POST values are (string). If your form field should accept both numeric and alphabets but exclude a value of 0, you will have a problem. <?php ## Using Equal operator if($_POST['myfield'] == 0){ echo 'You cannot set it to 0'; } else { echo 'Correct'; } # You will get the following results // $_POST['myfield'] = 0; // Output: You cannot set it to 0; // $_POST['myfield'] = 1; // Output: Correct; // $_POST['myfield'] = 'test'; // Output: You cannot set it to 0; ## Using Identical operator if($_POST['myfield'] === 0){ echo 'You cannot set it to 0'; } else { echo 'Correct'; } # You will get the following results // $_POST['myfield'] = 0; // Output: Correct; // $_POST['myfield'] = 1; // Output: Correct; // $_POST['myfield'] = 'test'; // Output: Correct; // You need to convert numeric values to integer and use identical operator $_POST['myfield'] = is_numeric($_POST['myfield']) ? (int)$_POST['myfield'] : $_POST['alias']; if($_POST['myfield'] === 0){ echo 'You cannot set it to 0'; } else { echo 'Correct'; } # Now you will get the following results // $_POST['myfield'] = 0; // Output: You cannot set it to 0; // $_POST['myfield'] = 1; // Output: Correct; // $_POST['myfield'] = 'test'; // Output: Correct; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 220.255.7.242 (proxied: 116.14.194.152) ---- Manual Page -- http://www.php.net/manual/en/reserved.variables.post.php Edit -- https://master.php.net/note/edit/89828 Del: integrated -- https://master.php.net/note/delete/89828/integrated Del: useless -- https://master.php.net/note/delete/89828/useless Del: bad code -- https://master.php.net/note/delete/89828/bad+code Del: spam -- https://master.php.net/note/delete/89828/spam Del: non-english -- https://master.php.net/note/delete/89828/non-english Del: in docs -- https://master.php.net/note/delete/89828/in+docs Del: other reasons-- https://master.php.net/note/delete/89828 Reject -- https://master.php.net/note/reject/89828 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152412) next »