MaskedPhantom00 Posted October 24, 2010 Share Posted October 24, 2010 What is the difference between these two lines !isset($_POST[$requiredField]) and !$_POST[$requiredField] Quote Link to comment https://forums.phpfreaks.com/topic/216725-quick-question/ Share on other sites More sharing options...
mentalist Posted October 24, 2010 Share Posted October 24, 2010 The following is explaining exactly what your asking... However generally the ! is used within logic, e.g. an 'if' statement, making it slightly different, but the same... isset checks if the variable exists within the php session variable table, returning true if there (and is set to something other than NULL), false if not. The ! technically inverts what ever follows it next (***) In the second example the ! inverts what ever follows as before, but depending on what is in the specific post variable you may not get expected results. e.g. (can't find reference to ! in manual, so what follows will either be right, or will be the opposite... lol), if it contains a non empty string then it would be classed as true (I think) and then get inverted, however if the variable is say not set, null, or set as false or 0, then it will be computed to true. If you were to use within an 'if' statement, the first statement would read: if the variable is not set then... and the second statement would read: if the boolean representation of the variable is not true then... That was a mind bender... so easy to think it, but to explain...lol Quote Link to comment https://forums.phpfreaks.com/topic/216725-quick-question/#findComment-1125941 Share on other sites More sharing options...
MaskedPhantom00 Posted October 24, 2010 Author Share Posted October 24, 2010 The following is explaining exactly what your asking... However generally the ! is used within logic, e.g. an 'if' statement, making it slightly different, but the same... isset checks if the variable exists within the php session variable table, returning true if there (and is set to something other than NULL), false if not. The ! technically inverts what ever follows it next (***) In the second example the ! inverts what ever follows as before, but depending on what is in the specific post variable you may not get expected results. e.g. (can't find reference to ! in manual, so what follows will either be right, or will be the opposite... lol), if it contains a non empty string then it would be classed as true (I think) and then get inverted, however if the variable is say not set, null, or set as false or 0, then it will be computed to true. If you were to use within an 'if' statement, the first statement would read: if the variable is not set then... and the second statement would read: if the boolean representation of the variable is not true then... That was a mind bender... so easy to think it, but to explain...lol Lol. You kind of explained a concept I already sort of understood. I read a book on Javascript and C++, so I have the idea behind the '!' statement. Maybe if I provide a bigger snippet of the code it would help to visualize my problem: foreach ( $requiredFields as $requiredField ) { if ( !isset( $_POST[$requiredField] ) or !$_POST[$requiredField] ) { $missingFields[] = $requiredField; } } I'm just having a difficult time visualizing the difference between the two variables. Like, when using !isset($_POST($requiredField)), my guess would be that if the variable is not 'set', meaning 'not initialized', it would set $missingFields[] = $requiredField. Now the second portion of the if the statement is confusing me. I understand the 'or' part, seeing as how C++ and Javascript both carry the same idea, but the $_POST[$requiredField] part. Does it mean if it was ever changed? or if anything was sent? Maybe you may be able to assist me with this, seeing as how you may( I know you do) have more experience with interpreted languages than I do. Quote Link to comment https://forums.phpfreaks.com/topic/216725-quick-question/#findComment-1125995 Share on other sites More sharing options...
phpfreak Posted October 25, 2010 Share Posted October 25, 2010 Perhaps check into empty() foreach ( $requiredFields as $requiredField ) { if ( empty( $_POST[$requiredField] )) { $missingFields[] = $requiredField; } } However, if the strict error reporting is on, you may still get an undefined variable message - those you need to suppress or hide on a production environment. Quote Link to comment https://forums.phpfreaks.com/topic/216725-quick-question/#findComment-1126008 Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2010 Share Posted October 25, 2010 Without context I can't be certain, but i'd think it should be more like this: if( !isset($_POST['$requiredvalue']) || empty( $_POST['$requiredvalue']) ) { Quote Link to comment https://forums.phpfreaks.com/topic/216725-quick-question/#findComment-1126010 Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 Either that or in this specific case whoever wrote the code is expecting the field to be a boolean value, e.g. from a checkbox... Quote Link to comment https://forums.phpfreaks.com/topic/216725-quick-question/#findComment-1126113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.