if($controle->soort == ("1" || "2") ) { ... }

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Tom aan t Goor

Tom aan t Goor

18/01/2013 20:18:42
Quote Anchor link
Ik heb een 'groot' script:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if($controle->soort == "1"){
 // error A \\
}elseif($controle->soort == "2"){
 // error A \\
}elseif($controle->soort == "3"){
 // error A \\
}

// Of het volgende:

if( ($controle->soort == "1") || ($controle->soort == "2") || ($controle->soort == "3") ){
 // error A \\
}
?>


Nu zou ik dit willen oplossen op de volgende manier (o.i.d.):

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php
if($controle->soort == ("1" || "2" || "3") ){
 // error A \\
}
?>



Maar voorzover ik weet is dit niet mogelijk. Weet iemand anders een manier hoe ik dit script 'flink' kan inkorten?
 
PHP hulp

PHP hulp

28/03/2024 19:04:32
 
Erwin H

Erwin H

18/01/2013 20:41:16
Quote Anchor link
Ik zou het met een switch oplossen:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
<?php
switch($controle->soort){
  case
1:
  case
2:
  case
3:
    // error A
    break;
  case
4:
    // etc
    break;
}

?>
 
Gerard M

Gerard M

18/01/2013 20:42:51
Quote Anchor link
Het mooiste, maak een method of functie. Maar dit kan ook:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if($controle->soort >= 1 && $controle->soort <= 3) {
    print "Yay";
}
else {
    print "Nay";
}

?>


Of als je wat meer hipster wilt zijn:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if(in_array($controle->soort, range(1, 3))) {
    print "Yay";
}
else {
    print "Nay";
}

?>


En nog meer, omdat het kan:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
<?php
for($found = false, $i = 1; $i <= 3 && !$found; ++$i) {
    if($i == $controle->soort) {
        print "Yay";
        $found = true;
    }
else if($i == 3) {
        print "Nay";
    }
}

?>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if(preg_match("#^([123]{1})$#", $controle->soort) == 1) {
    print "Yay";
}
else {
    print "Nay";
}

?>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if(filter_var($controle->soort, FILTER_VALIDATE_INT,
    array(
        'options' => array(
            'min_range' => 1,
            'max_range' => 3
        ),
        'flags' => FILTER_NULL_ON_FAILURE
    )
) !==
null) {
    print "Yay";
}
else {
    print "Nay";
}

?>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if(max(3, $controle->soort) == 3 && min($controle->soort, 1) == 1) {
    print "Yay";
}
else {
    print "Nay";
}

?>

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if(count(array_diff((array)$controle->soort, array(1,2,3))) == 0) {
    print "Yay";
}
else {
    print "Nay";
}

?>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if(array_key_exists($controle->soort, array_flip(range(1, 3)))) {
    print "Yay";
}
else {
    print "Nay";
}

?>


Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
<?php
if(array_search($controle->soort, array(1, 2, 3)) {
    print "Yay";
}
else {
    print "Nay";
}

?>
Gewijzigd op 18/01/2013 21:34:18 door Gerard M
 
Ozzie PHP

Ozzie PHP

18/01/2013 23:49:28
Quote Anchor link
Of zo:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
<?php
$error_a
= array(1, 2, 3);
if(in_array($controle->soort, $error_a){
  // error A!
}
?>
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.