An easy question for you guys.
I assume there must be an easier (= less code) way to do the following snippet:
if (link_validate_url($items[0]['url_value'])) {
} else {
form_set_error('', 'Not a valid URL.');
}
Simply negate the return value using the !
operator:
if (!link_validate_url($items[0]['url_value'])) {
form_set_error('', 'Not a valid URL.');
}
Of course you could shorten it even more, but IMO that reduces the readability:
if (!link_validate_url($items[0]['url_value']))
form_set_error('', 'Not a valid URL.');
or even
if (!link_validate_url($items[0]['url_value'])) form_set_error('', 'Not a valid URL.');
if (!link_validate_url($items[0]['url_value'])) {
form_set_error('', 'Not a valid URL.');
}
or you can (ab)use common compiler optimization that the second part of an or is never executed if the first part is true
link_validate_url($items[0]['url_value']) or form_set_error('', 'Not a valid URL.');
Try this:
if (! link_validate_url($items[0]['url_value'])) {
form_set_error('', 'Not a valid URL.');
}
Also, you might want to read this.
You could (ab)use the ternary operator.
Typically it is condition ? action if true : action if false;
But you can leave actions blank.
This leaves you with:
link_validate_url($items[0]['url_value'] ?: form_set_error('', 'Not a valid URL.');
Edit:
This is PHP 5.3+