<-- Terug naar Snippets

Vergelijkings operators

PHP versie: all

String Integer

== Gelijk aan (waarde)

=== Identiek aan (waarde en type)

!= Niet gelijk aan (waarde)

<> Niet gelijk aan (waarde)

!== Niet identiek aan (waarde en type)

< Kleiner dan

> Groter dan

<= Kleiner dan of gelijk aan

>= Groter dan of gelijk aan

String Integer

Broncode


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 100

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 106

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 119

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 120

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 121

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 199

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 287

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 288

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 289

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 290

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jorendew/phphulp/inc/class.ubb.php on line 291
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
ini_set
('display_errors'1);
error_reporting(E_ALL);


function 
vergelijk($a$b$operator)
{
    switch(
$operator)
    {
        case 
'equal' : return $a == $b; break;
        case 
'identical' : return $a === $b; break;
        case 
'notequala' : return $a != $b; break;
        case 
'notequalb' : return $a <> $b; break;
        case 
'notidentical' : return $a !== $b; break;
        case 
'less' : return $a $b; break;
        case 
'more' : return $a $b; break;
        case 
'equalless' : return $a <= $b; break;
        case 
'equalmore' : return $a >= $b; break;
    }
    return 
false;
}
function 
create_int($var)
{
    if(
is_numeric($var))
    {
        return (int)
$var;
    }
    return 
false;
}
function 
get_operator($operator)
{
    switch(
$operator)
    {
        case 
'equal' : return '==' ; break;
        case 
'identical' : return '===' ; break;
        case 
'notequala' : return '!=' ; break;
        case 
'notequalb' : return '<>' ; break;
        case 
'notidentical' : return  '!==' ; break;
        case 
'less' : return '<' ; break;
        case 
'more' : return '>' ; break;
        case 
'equalless' : return '<=' ; break;
        case 
'equalmore' : return '>=' ; break;
    }
}

if(
$_SERVER['REQUEST_METHOD'] == 'POST')
{
    if(empty(
$_POST['a']) || empty($_POST['b']))
    {
        
$errors[] = '<p><i>Vul beide velden in!</i></p>';
    }
    else
    {
        if(!
$a = ($_POST['a-type'] == 'int') ? create_int($_POST['a']) : '"'.$_POST['a'].'"')
        {
            
$errors[] = '<p><i>De string bij waarde 1 kan nooit een integer zijn!</i></p>';
            
$error true;
        }
        if(!
$b = ($_POST['b-type'] == 'int') ? create_int($_POST['b']) : '"'.$_POST['b'].'"')
        {
            
$errors[] = '<p><i>De string bij waarde 2 kan nooit een integer zijn!</i></p>';
            
$error true;
        }
        
        if(!isset(
$error))
        {
            
$result vergelijk($a$b$_POST['operator']);
            
            
$content[] = '<p><b>'.$a.'</b> '.get_operator($_POST['operator']).' <b>'.$b.'</b> geeft als uitkomtst: <b>';
            
$content[] = $result 'TRUE' 'FALSE';
            
$content[] = '</b></p>';
        }
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>PHPtuts.nl - PHP Scripts - Vergelijkings operators</title>
    
    <link rel="stylesheet" href="../../styles/default.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="../../styles/ubb.css" type="text/css" media="screen" />
</head>
<body>
    <div id="wrap">
       <p class="none small align-right"><a href="/codeSnippets/">&lt;-- Terug naar Snippets</a></p>
        <h1>Vergelijkings operators</h1>        
        <div id="info">
            <p>PHP versie: all</p>
        </div>
        
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <p>
                <label class="field" for="var1">Waarde 1</label>
                <input type="text" id="var1" name="a" />
                <input type="radio" class="none" id="a-string" name="a-type" value="string"  checked="checked"/> String 
                <input type="radio" class="none" id="a-int" name="a-type" value="int" /> Integer
            </p>
            <p>
                <label class="field">Operators</label>
            </p>
            <p>
                <input type="radio" name="operator" id="equal" value="equal" class="none" checked="checked" />
                == Gelijk aan (waarde)
            </p>
            <p>
                <input type="radio" name="operator" id="identical" value="identical" class="none" />
                === Identiek aan (waarde en type)
            </p>
            <p>
                <input type="radio" name="operator" id="notequala" value="notequala" class="none" />
                != Niet gelijk aan (waarde)
            </p>
            <p>
                <input type="radio" name="operator" id="notequalb" value="notequalb" class="none" />
                &lt;&gt; Niet gelijk aan (waarde)
            </p>
            <p>
                <input type="radio" name="operator" id="notidentical" value="notidentical" class="none" />
                !== Niet identiek aan (waarde en type)
            </p>
            <p>
                <input type="radio" name="operator" id="less" value="less" class="none" />
                &lt; Kleiner dan
            </p>
            <p>
                <input type="radio" name="operator" id="more" value="more" class="none" />
                &gt; Groter dan
            </p>
            <p>
                <input type="radio" name="operator" id="equalless" value="equalless" class="none" />
                &lt;= Kleiner dan of gelijk aan
            </p>
            <p>
                <input type="radio" name="operator" id="equalmore" value="equalmore" class="none" />
                &gt;= Groter dan of gelijk aan
            </p>
            <p>
                <label class="field" for="var2">Waarde 2</label>
                <input type="text" id="var2" name="b" />
                <input type="radio" class="none" id="b-string" name="b-type" value="string" checked="checked" /> String 
                <input type="radio" class="none" id="b-int" name="b-type" value="int" /> Integer
            </p>
            <p>
                <input type="submit" value="Vergelijk"/>
            </p>
        </form>
        
        <?php
        
// Weergeven van meldingen uit het phpscript.
        
if(isset($errors))
        {
            echo 
'<ul>';
            foreach(
$errors as $error);
            {
                echo 
'<li>'.$error.'</li>';
            }
            echo 
'</ul>';
        }
        elseif(isset(
$content))
        {
            foreach(
$content as $line)
            {
                echo 
$line;
            }
        }
        
        
// Weergeven broncode
        
define('FILE'__FILE__);
        require_once(
'../broncode.php');
        
?>        
    </div>    

<!-- Google analytics (geen onderdeel van script) -->
<script type="text/javascript" src="/inc/ga1.js"></script>
<script type="text/javascript" src="/inc/ga2.js"></script>

</body>
</html>