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.

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

Thank you very much for your nice example and explanation!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.