You are here: RE: [PHP] Seemingly weird regex problem « PHP « IT news, forums, messages
RE: [PHP] Seemingly weird regex problem

Posted by Mike Ford on 01/21/05 14:48

To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



On 20 January 2005 20:36, Tim Boring wrote:

> On Thu, 2005-01-20 at 14:40, Bret Hughes wrote:
> > On Thu, 2005-01-20 at 12:43, Jason Wong wrote:
> > > On Friday 21 January 2005 02:16, Tim Boring wrote:
> > >
> > > > It's perfectly legit to use expressions. Now perhaps there is
> > > > something wrong with the regex I'm trying to use, but using a
> > > > regex in and of itself is legal.
> > > > http://www.php.net/manual/en/control-structures.switch.php
> > >
> > > Yes, but comparing those expressions to:
> > >
> > > switch ($line)
> > >
> > > where $line is a string, doesn't make sense. See my other post.
> > >
> >
> > Chaching ( sound of light bulb turning on) I see that in the
> > example in the manual it needs to be compared to true ( a boolean
> > value)
> >
> >
> > switch (true)
> >
> > rather than switch ($line)
> >
> > What is not apparent to me is why the first case matches if the preg
> > fails. Wouldn't line evaluate to true in a boolean context?

Coming a little late to the discussion here, but I don't think anyone's
really cottoned on to the fact that:

switch ($x):
{
case $e1:
...
break;

case $e2:
...
break;

...

}

is functionally equivalent to:

if ($x==$e1):
...
elseif($x==$e2):
...
...
endif;

(except that the $x expression is evaluated only once, at the start, instead
of multiple times).

So it's important to be able to predict how an == comparison will perform,
but because of the way PHP does automatic type conversion, this isn't always
obvious (and, unfortunately, there's no switch() modifier to force ===
comparisons). I can't remember all the minute details, but basically it
goes something like this:

0, 0.0, FALSE, '' and '0' all compare equal to each other.

if one expression is Boolean, the other is converted to Boolean before
comparison.

if one expression is a number, the other is converted to a number (using
usual PHP rules, so a string which does not start with a number is converted
to 0).

if both expressions are string, and both look like a number, both are
converted to numbers and a numeric comparison is performed (so "3"=="3.0",
for instance).

In addition, the tables at http://php.net/types.comparisons may be of help,
particularly table O-2.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация