That would make some sense, but it seems it should trigger an error or at least a warning, rather than just ignoring the whole statement.
That would make some sense, but it seems it should trigger an error or at least a warning, rather than just ignoring the whole statement.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
By default, PHP is rather lenient. If it comes across a variable not declared before and is being used, it will create it on the spot. To catch those errors, put this at the start of any PHP script
In fact, this is my SOP. Helps to catch a lot of bugs due to mis-spelling and such.PHP Code:error_reporting(E_ALL);
I was not using any variables, and I had turned on E_ALL error-reporting (as you can see from the transcript of the session I posted). I agree that it is usually a good idea to turn on E_ALL in new scripts.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
You have to feed empty() a variable. Constants cause parse errors. I don't know why you weren't getting errors Twey.
My recommendation for the OP is:PHP Code:php > echo empty(0) ? 'yes' : 'no';
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in php shell code on line 1
php > print empty(0) ? 'yes' : 'no';
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '$' in php shell code on line 1
php > $var = 0;
php > print empty($var) ? 'yes' : 'no';
yes
php >
PHP Code:if ( $_POST['description'] == '' || !is_numeric($_POST['id']) ) {
die('You did not complete all of the required fields');
}
toe_head2001 (04-07-2009)
is_numeric() is good, but I stand by my recommendation of === for the former.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
The problem with === NULL is that it doesn't test for an empty string. If the POST variable is set, which if the form was submitted, it will be, NULL won't filter anything.PHP Code:$_POST['description'] = '';
$_POST['id'] = 100;
if ( $_POST['description'] === NULL || !is_numeric($_POST['id']) ) {
die('You did not complete all of the required fields');
}
I think he was in favour of:
Not:PHP Code:if ( $_POST['description'] === '' || !is_numeric($_POST['id']) ) {
die('You did not complete all of the required fields');
}
PHP Code:if ( $_POST['description'] === NULL || !is_numeric($_POST['id']) ) {
die('You did not complete all of the required fields');
}
Well, for example, '0' == false. Strings can be coerced too.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
I'm sorry Twey, please bare with me.
I am not seeing a difference:
What am I missing? Or is it that you are saying since we know the $_POST variable is a string we should explicitly test for the value and type, hence === '' , where '' is an empty string ?PHP Code:<?php
$_POST['description'] = '0';
$_POST['id'] = 100;
// BETWEEN
if ( $_POST['description'] === '' || !is_numeric($_POST['id']) ) {
die('You did not complete all of the required fields');
}
// AND
if ( $_POST['description'] == '' || !is_numeric($_POST['id']) ) {
die('You did not complete all of the required fields');
}
exit;
Bookmarks