Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How does && Operator Work in this code??
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
vtruong




PostPosted: Fri Jul 23, 2010 12:52 pm   Post subject: How does && Operator Work in this code??

Hi guys! I am current learning PHP on my own and I have encountered one small problem! This is from a textbook accepting a HTML form and validating the users inputs with conditions. I do not have APACHE or PHP installed on my computer but I am still trying my best to learn this. The code is displayed below and I am confused on one of the last parts of the code...

if ($name && $email && $gender && $comments) {
echo "<p>Thank you, $name, for the following comments:<br/>
<tt>$comments</tt></p>
<p>We will reply to you at $email.</p>\n";
echo $message; // From the $gender conditional.
}


If you can please help me explain how if ($name && $email && $gender && $comments) becomes true... when they're suppose to be $name is suppose to be a string, $email is a string, $gender is a string and $comments is a string. From my past experience of programming the && operator is only true in this case if $name, $email, $gender, $comments are exactly the same values.

Please let me know on how you guys think,
Thank you,
vtruong

P.S. here is the code! Thanks in advance!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Form Feedback</title>
</head>
<body>
<?php # handle_form.php
// Validate the name and combat Magic Quotes, if necessary.
if (!empty($_REQUEST['name'])) {
$name = stripslashes($_REQUEST['name']);
} else {
$name = NULL;
echo '<p><font color="red">You forgot to enter your name!</font></p>';
}
// Validate the email and combat Magic Quotes, if necessary.
if (!empty($_REQUEST['email'])) {
$email = $_REQUEST['email'];
} else {
$email = NULL;
echo '<p><font color="red">You forgot to enter your email address!</font></p>';
}
// Validate the comments and combat Magic Quotes, if necessary.
if (!empty($_REQUEST['comments'])) {
$comments = stripslashes($_REQUEST['comments']);
} else {
$comments = NULL;
echo '<p><font color="red">You forgot to enter your comments!</font></p>';
}
// Validate gender.
if (isset($_REQUEST['gender'])) {
$gender = $_REQUEST['gender'];
if ($gender == 'M') {
$message = '<p>Good day, Sir! </p>';
} elseif ($gender == 'F') {
$message = '<p>Good day, Madam! </p>';
} else { // Unacceptable value.
$message=NULL;
echo '<p><font color="red">Gender should be either "M" or "F"!</font></p>';
}
} else { // $_REQUEST['gender'] is not set.
$gender = NULL;
echo '<p><font color="red">You forgot to select your gender!</font></p>';
}
// If everything is okay, print the message.
if ($name && $email && $gender && $comments) {
echo "<p>Thank you, $name, for the following comments:<br/>
<tt>$comments</tt></p>
<p>We will reply to you at $email.</p>\n";
echo $message; // From the $gender conditional.
} else { // One form element was not filled out properly.
echo '<p><font color="red">Please go back and fill out the form again.</font></p>';
}
</html>
Sponsor
Sponsor
Sponsor
sponsor
chrisbrown




PostPosted: Fri Jul 23, 2010 1:24 pm   Post subject: Re: How does && Operator Work in this code??

vtruong @ Fri Jul 23, 2010 12:52 pm wrote:
From my past experience of programming the && operator is only true in this case if $name, $email, $gender, $comments are exactly the same values.

Think about that for a second, because I don't believe any language treats the && operator that way. It only works with boolean (true/false) values.

Keeping that in mind, PHP treats empty strings as false, and non-empty strings as true.

So you could (and should) interpret the line in question as "if $name is not empty && $email is not empty && etc..."
vtruong




PostPosted: Fri Jul 23, 2010 1:39 pm   Post subject: Re: How does && Operator Work in this code??

Alright, gotcha! : )

Thank you very much for explaining that to me, definitely clears things up!
Tony




PostPosted: Fri Jul 23, 2010 1:48 pm   Post subject: RE:How does && Operator Work in this code??

Oh yeah, the comparisons in PHP are fun.

http://www.otton.org/2008/08/06/stupid-php-tricks-true-false-comparison/
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: