Re: regex operators

From: Date: Tue, 14 Oct 2003 16:02:46 +0000
Subject: Re: regex operators
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-4832@lists.php.net to get a copy of this message
David Sklar wrote:
One thing that I suppose you get by using "case preg_match()" like George does below (as opposed to my preg_case operator or Hartmut's callback) is you can deal with arbitrary arguments to the preg_match() (or other) function -- storing captured subpatterns from the regex, for example.
Aesthetics aside, how is that different in effect from: switch(true) { case preg_match('/foo.*?bar/i', $data):
     /** stuff **/
     break;
case preg_match('/baz/', $data);
     /** other stuff **/
     break;
default:
      break;
}
the "switch(true)" trick is completely ugly IMHO and is just an obfuscated way to write an if-elseif-...-else chain my point of view is that you use switch whenever you have a series of tests against a single value whereas in the code snippet above you could also test against e.g. $date instead of $data in some of the cases whereas this is meant to be so or a typo does not become clear by extending the switch statement itself in the way i propoesd you gain extra functionality from switch while keeping the "tests against a single value" contract i was originaly just thinking about an optional flag that would switch to "===" behaviour instead of just "==" but the callback mechanism was just too obvious :) and if you need extra parameters you can always wrap up whatever test function you use in an extra wrapper function ;) -- Hartmut Holzgraefe <hartmut@php.net>

« previous php.internals (#4832) next »