RE: [PHP] Help with Logical 'OR'

2002-01-28 Thread Martin Towell

basically it's to do with "lazy evaluation" or whatever it's called

* logical expressions are read from left to right.
* with OR, the expression reading is halted when the first TRUE is found
* with AND, the expression reading is halted when the first FALSE is found

so with :
 if (true||false)
php will stop processing when it comes across the "true"

and with :
  if (false&&true)
php will stop processing when is comes across the "false"

in both cases, this means that whatever the second expression is, it wont
get "executed"

hope that explains it a bit furthur

Martin

-Original Message-
From: jv [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, January 29, 2002 2:26 PM
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Help with Logical 'OR'


Thanks Matt, Jeff, and Sam
(BTW Sam, for some reason your post didn't come to me on the list through
regular email- I found it when I did a search in the archives at php.net. I
'm pretty sureI didn't delete it. I mention this because I'm sure the list
server hiccups sometimes and I'd hate to offend someone by not thanking or
acknowledging  them :-)  Also,  the truth tables are helpfull in visualizing
the different logical conditions- thanks!)

Anyway,  I've tried all combinations yet still can't get (true||fale) or
(true&&false) to work, even by using (!empty) or (!isset).

I recall reading something about that if a cetrtain envioronmental variable
isn't set to 'on' on the server then... I'm not sure what I'm talking about
so I'll try to look it up again.

I know that I could just nest functions but I'm trying to learn
alternatives...




> At 02:04 PM 1/28/2002 -0600, jv wrote:
> >It seems that only the first condition is being evaluated.
> >
> >I know that if  both of the following conditions are false then the
second
> >set of statements should get read, and that's what happens, but when I
set
> >$name to true and $text to false (true||false) then the first set gets
read.
> >
> >Shouldn't both conditions be true in order for the first set of
statements
> >be read? Why would the first set of statements get read when the
conditions
> >are (true||false)?
> >
> >Thanks in advance for your help.
> >james
> >
> >
> >if ($name || $text)
> >
> > {
> >
> >   $text = stripslashes("$text");
> >   print "Hello $name\n";
> >   print "You said:\n $text\n";
> >
> > }else{
> >
> >   print "Sorry, but you seem to have left one or more entries
blank.\n";
> >   print "Please return to form\n";
> >   print " and complete the input\n";
> >
> >}
> >**
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]



Re: [PHP] Help with Logical 'OR'

2002-01-28 Thread jv

Thanks Matt, Jeff, and Sam
(BTW Sam, for some reason your post didn't come to me on the list through
regular email- I found it when I did a search in the archives at php.net. I
'm pretty sureI didn't delete it. I mention this because I'm sure the list
server hiccups sometimes and I'd hate to offend someone by not thanking or
acknowledging  them :-)  Also,  the truth tables are helpfull in visualizing
the different logical conditions- thanks!)

Anyway,  I've tried all combinations yet still can't get (true||fale) or
(true&&false) to work, even by using (!empty) or (!isset).

I recall reading something about that if a cetrtain envioronmental variable
isn't set to 'on' on the server then... I'm not sure what I'm talking about
so I'll try to look it up again.

I know that I could just nest functions but I'm trying to learn
alternatives...




> At 02:04 PM 1/28/2002 -0600, jv wrote:
> >It seems that only the first condition is being evaluated.
> >
> >I know that if  both of the following conditions are false then the
second
> >set of statements should get read, and that's what happens, but when I
set
> >$name to true and $text to false (true||false) then the first set gets
read.
> >
> >Shouldn't both conditions be true in order for the first set of
statements
> >be read? Why would the first set of statements get read when the
conditions
> >are (true||false)?
> >
> >Thanks in advance for your help.
> >james
> >
> >
> >if ($name || $text)
> >
> > {
> >
> >   $text = stripslashes("$text");
> >   print "Hello $name\n";
> >   print "You said:\n $text\n";
> >
> > }else{
> >
> >   print "Sorry, but you seem to have left one or more entries
blank.\n";
> >   print "Please return to form\n";
> >   print " and complete the input\n";
> >
> >}
> >**
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Help with Logical 'OR'

2002-01-28 Thread Sam Masiello


For sake of efficiency, many languages will do what is called "Short
Circuit" in a situation like this.  Let's look at a simple truth table for
an OR using two conditions (T=true, F=false):

T || T = T
T || F = T
F || T = T
F || F = F

In the first two cases, it is only necessary to evaluate the first condition
because since they are true, the entire statement will be true.  In case
number 3, since the first condition was false, the second condition needs to
be evaluated to determine whether or not the entire statement is true.  In
case 4, both statements are evaluated (for the same reason as case 3).

If you need BOTH statements to be evaluated, chances are that you want an
AND condition, not an OR condition.  A simple truth table for an AND using
two conditions:

T && T = T
T && F = F
F && T = F
F && F = F

In the case of the AND, it is always necessary to evaluate both conditions
as both conditions need to be true in order for the entire statement to be
true.  If any condition in a set of AND conditions is false, the value of
the entire expression is also false.

HTH

Sam Masiello
Software Quality Assurance Engineer
Synacor
(716) 853-1362 X289
[EMAIL PROTECTED]



- Original Message -
From: "jv" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, January 28, 2002 3:04 PM
Subject: [PHP] Help with Logical 'OR'


> It seems that only the first condition is being evaluated.
>
> I know that if  both of the following conditions are false then the second
> set of statements should get read, and that's what happens, but when I set
> $name to true and $text to false (true||false) then the first set gets
read.
>
> Shouldn't both conditions be true in order for the first set of statements
> be read? Why would the first set of statements get read when the
conditions
> are (true||false)?
>
> Thanks in advance for your help.
> james
>
> 
> if ($name || $text)
>
> {
>
>   $text = stripslashes("$text");
>   print "Hello $name\n";
>   print "You said:\n $text\n";
>
> }else{
>
>   print "Sorry, but you seem to have left one or more entries
blank.\n";
>   print "Please return to form\n";
>   print " and complete the input\n";
>
> }
> **
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Help with Logical 'OR'

2002-01-28 Thread Jeff Sheltren

With a logical OR statement, many languages will evaluate the first 
statement, and if the first condition is true, then it will not evaluate 
the second condition, because TRUE OR (anything) is TRUE.  This saves the 
program from executing any more code than it has to.  If the first 
condition is FALSE, then the second condition will be checked.

If you are wanting both $name and $text to be true in order for the first 
block of code to be executed, then you will want to use an AND (&&) statement.

I hope that answers your question.

Jeff

At 02:04 PM 1/28/2002 -0600, jv wrote:
>It seems that only the first condition is being evaluated.
>
>I know that if  both of the following conditions are false then the second
>set of statements should get read, and that's what happens, but when I set
>$name to true and $text to false (true||false) then the first set gets read.
>
>Shouldn't both conditions be true in order for the first set of statements
>be read? Why would the first set of statements get read when the conditions
>are (true||false)?
>
>Thanks in advance for your help.
>james
>
>
>if ($name || $text)
>
> {
>
>   $text = stripslashes("$text");
>   print "Hello $name\n";
>   print "You said:\n $text\n";
>
> }else{
>
>   print "Sorry, but you seem to have left one or more entries blank.\n";
>   print "Please return to form\n";
>   print " and complete the input\n";
>
>}
>**



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Help with Logical 'OR'

2002-01-28 Thread jv

It seems that only the first condition is being evaluated.

I know that if  both of the following conditions are false then the second
set of statements should get read, and that's what happens, but when I set
$name to true and $text to false (true||false) then the first set gets read.

Shouldn't both conditions be true in order for the first set of statements
be read? Why would the first set of statements get read when the conditions
are (true||false)?

Thanks in advance for your help.
james


if ($name || $text)

{

  $text = stripslashes("$text");
  print "Hello $name\n";
  print "You said:\n $text\n";

}else{

  print "Sorry, but you seem to have left one or more entries blank.\n";
  print "Please return to form\n";
  print " and complete the input\n";

}
**



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]