Reputation: 3749
Is there a shorter way to write the following IF condition in PHP? Thanks
if ( ($ext != 'jpg') OR ($ext != 'png') OR ($ext !='jpeg') OR ($ext != 'gif') )
{
$error = 'Image type not allowed.';
}
Upvotes: 1
Views: 151
Reputation: 5157
All answers above are correct, but for the sake of something different:
$error = (!in_array($ext, array('jpg','png','jpeg','gif'))) ? 'Image type not allowed.' : '';
Upvotes: 0
Reputation: 734
if (!in_array($ext, array('jpg','jpeg','gif)) { $error = 'Image type not allowed.'; }
Upvotes: 1
Reputation: 28089
if (!in_array($ext, array('jpg','png','jpeg','gif')) )
$error = 'Image type not allowed.';
or
if(!preg_match('/^(jpe?g|png|gif)$/',$ext))
$error = 'Image type not allowed.';
Upvotes: 3
Reputation: 10517
e.g.
if (!in_array($ext, array('jpg', 'png', 'jpeg', 'gif')){
$error = 'Image type not allowed.';
}
Upvotes: 4