0

I am reading a book and there is the following code inside :

if ( !isset( $_POST[$requiredField] ) or !$_POST[$requiredField] )

I can understand the first part of the use of isset function but I cannot understand what the second part does.
Any ideas?
Thank you very much.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by skiabox
1
if ( !isset( $_POST[$requiredField] ) or !$_POST[$requiredField] )

When you type something for example:

if(!$myVariable) //This is the same as if you wrote=> 
if($myVariable != true)

First part with isset, checks if the key that holds variable $requiredField exists in the $_POST array.

But considering that is is associative array, you want to check that $_POST[$requiredField] also has some value attached to it.

If you have just $_POST[$requiredField] = "", it will count it as empty, and it means there is no value connected with that key.

And when value of some key in array is "" it returns FALSE.

So by this if statement, you are checking if this array is missing $_POST[$requiredField] key, or even if it has that key, that it must have a value attached to it.

In conclusion,
everything that you put in the curly braces after this if statement will run under two conditions:

1: global variable $_POST doesn't have key $requiredField in it.
OR
2: $_POST[$requiredField] exists but doesn't contain any value.

Hope this solved your problem,

Cheers,

Toni

Edited by tnjiric: n/a

0

Thank you very much for your nice example and explanation!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.