<? php
if ($ i == 0)
{
echo "i is 0";
}
elseif ($ i == 1)
{
echo "i is 1";
}
elseif ($ i == 2)
{
echo "i is 2";
}
?> <? php
switch ($ i)
{
case 0:
echo "i is 0";
break;
case 1:
echo "i is 1";
break;
case 2:
echo "i is 2";
break;
}
?> <? php
class naughty_checks
{
var $ data = array ();
/ * Class constructor, array of input data to check * /
function naughty_checks ($ data)
{
$ this-> data = $ data;
}
/ * Check for field completion * /
function field_empty ($ field)
{
if (! isset ($ this-> data [$ field]) || strlen (trim ($ this-> data [$ field])) == 0)
{
return true;
}
else
{
return false;
}
}
/ * Check if a field is a positive integer * /
function field_numeric ($ field)
{
if (! isset ($ this-> data [$ field]) || intval (trim ($ this-> data [$ field])) <1)
{
return true;
}
else
{
return false;
}
}
}
?> <? php
// $ a - our array
$ error_flag = false;
$ error_text = ''; // Error text
$ _chck = new naughty_checks ($ a); // Initialize the class, pass our array
/ * Magic * /
<b> switch (true)
{
case ($ _ chck-> field_empty ('name')):
$ error_flag = true;
$ error_text = 'You have not filled the Name field.';
break;
case ($ _ chck-> field_numeric ('age')):
$ error_flag = true;
$ error_text = 'You did not fill in, or you incorrectly filled in the Age field.';
break;
} </ b>
if ($ error_flag)
{
die ($ error_text);
}
else
{
echo 'Have you ever seen a UFO?';
}
?> Source: https://habr.com/ru/post/13092/
All Articles