Re: Using Logical OR operator in IF statement???
| From: | James, Yz | Date: | Sun, 21 Oct 2001 19:33:39 +0000 |
| Subject: | Re: Using Logical OR operator in IF statement??? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71752@lists.php.net to get a copy of this message | ||
Hi Brad,
This worked for me:
<?
if ((substr($sString, 0, (strlen($sString)-1) == "-")) || (substr($sString,
0, 1) == "-")) {
echo "you can't have a dash at the beginning or end of your string.";
}
?>
...... but I'd tend to go for a regex as a solution to what you're after,
which involves less code:
<?
if (preg_match("/^-|-$/s", $string)) {
echo "You cannot have a \"-\" character at the beginning or end of your
string.";
} else {
echo "Whatever....";
}
?>
Just my thoughts...
James
"Brad Melendy" <brad@techkno.com> wrote in message
news:20011021060945.16311.qmail@pb1.pair.com...
> Hello,
> Ok, this works:
>
> if (substr($sString,(strlen($sString)-1)!="-")) {
> print "You can't have a dash at the end of your string.";
> }
>
> and this works:
> if (substr($sString,0,1)!="-") {
> print "You can't have a dash at the beginning of your string.";
> }
>
> But, this doesn't work for any case:
> if ((substr($sString,(strlen($sString)-1)!="-")) or
> (substr($sString,0,1)!="-")) {
> print "you can't have a dash at the beginning or end of your string.";
> }
>
> What could be wrong? I've used a logical OR operator in the middle of an
IF
> statement like this before, but for some reason, this just isn't working.
> Anyone got any ideas? I suppose I can just evaluate this with two
different
> IF statements, but it seems like I shoud be able to do it in one and
reduce
> duplicate code. Thanks very much in advance.
>
> .....Brad
>
>
>