RE: [PHP] substr?
| From: | Ford, Mike [LSS] | Date: | Tue, 18 Jun 2002 15:55:01 +0000 |
| Subject: | RE: [PHP] substr? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-102813@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Lazor, Ed [mailto:ELazor@providence.org]
> Sent: 17 June 2002 21:24
> To: 'Chris Knipe'; php-general@lists.php.net
> Subject: RE: [PHP] substr?
>
>
> Here's another way of writing that code that may be easier to
> work with:
>
> $TestValues = array("072", "073", "082", "083",
> "084");
> $NumC = $substr($_POST['NumC'], 0, 3);
> if (in_array($NumC, $TestValues))
> $FormError = "True";
And here's another:
switch substr($_POST'NumC'], 0, 3):
case '072':
case '073':
case '082':
case '083':
case '084':
// acceptable -- do anything appropriate here
// (even nothing, if that's what you want!)
break;
default:
// not acceptable
$FormError = "True";
endswitch;
Incidentally, why are you setting the variable $FormError to the string "True", rather
than the Boolean TRUE? You may have a perfectly ggod reason, but it looks odd to my eyes. If a
Boolean would be acceptable, you could also write it like this:
$NumC = substr($_POST'NumC'], 0, 3);
$FormError = $NumC!='072' && $NumC!='073'
&& $NumC!='082' && $NumC!='083' &&
$NumC!='084';
or:
$FormError = !in_array(substr($_POST'NumC'], 0, 3),
array('072', '073', '082', '083',
'084'));
> > -----Original Message-----
> > From: Chris Knipe [mailto:savage@savage.za.org]
> > Sent: Monday, June 17, 2002 12:19 PM
> >
> > So what's the difference between
> > if (!(($blah == blah) OR ($ab == ab))) {
> > and
> > if ((!$blah == blah) OR (!$ab == ab)) {
Well, as you've written it, a hell of a lot!! I assume you meant the second version to be:
if (!($blah == blah) OR !($ab == ab)) {
> > Shouldn't they both do the same? And if so, why didn't they
> > in this case??
No, they absolutely shouldn't. Think about it: the first ORs the two comparisons, and then
takes the NOT of that; the second NOTs both comparisoons, then does the OR. It's the same sort
of reason why
$x = -1 * -2;
is not the same as
$x = -(1 * 2);
When working with Boolean expressions in this way, deMorgan's laws often come in handy; these
state that:
!a AND !b is the same as !(a OR b)
!a OR !b is the same as !(a AND b)
Hope this helps!!
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211