Roman
Roman

Reputation: 3749

PHP shorter way for IF condition

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

Answers (4)

Oliver Moran
Oliver Moran

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

Vinoth Gopi
Vinoth Gopi

Reputation: 734

if (!in_array($ext, array('jpg','jpeg','gif)) { $error = 'Image type not allowed.'; }

Upvotes: 1

tobyodavies
tobyodavies

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

heximal
heximal

Reputation: 10517

e.g.

if (!in_array($ext, array('jpg', 'png', 'jpeg', 'gif')){
  $error = 'Image type not allowed.';
}

Upvotes: 4

Related Questions