/    Sign up×
Community /Pin to ProfileBookmark

if statements and TRUE

I’m studying PHP and web forms and would like some clarity on what’s happening here with IF/TRUE statements.

In the below sample, if someone submits their email address it is stored in $email? which will then evaluate as TRUE. Is their email address (the stored value in $email) now being overwritten by the word TRUE?

[CODE]// Check for an email address.
if (strlen($_POST[’email’]) > 0) {
$email = TRUE;
} else {
$email = FALSE;
echo ‘<p>You forgot to enter your email address!</p>’;
}[/CODE]

At the end of the IF statements, there is the ($name && $email && etc) stuff where there is no else after it. Is it looking to see if TRUE is flagged to all of these?

[CODE]if ($name && $email && $username && $password) { // If everything’s okay.
// Register the user.
echo ‘<p>You are now registered.</p>’;
} else { // Something’s not TRUE.
echo ‘<p>Please go back and try again.</p>’;
}[/CODE]

Also, sometimes I just see “if ($var);” then straight on to something else without an interrogative. What is happening with these? I gather there’s a truth test but what passes as true if there seems to be no specific test accociated with it? Some clarity on these issue would help me understand what underlies these type of IFs

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@pcthugNov 06.2006 — ...if someone submits their email address it is stored in $email? which will then evaluate as TRUE. Is their email address (the stored value in $email) now being overwritten by the word TRUE?[/QUOTE]Here is the first example written in english:

If the number of characters within the [FONT=Courier New]$_POST['email'][/FONT] variable is greater than [FONT=Courier New]0[/FONT],

create a new variable entitled email and assign it a literal value of [FONT=Courier New]TRUE[/FONT].

Otherwise (else),

create a new variable entitled email and assign it a literal value of [FONT=Courier New]FALSE[/FONT].

Then output to the client [FONT=Courier New]<p>You forgot to enter your email address!</p>[/FONT].

As you can see the value of [FONT=Courier New]$email[/FONT] never gets set to anything other than either TRUE or FALSE and therefore is never overwritten (the POSTDATA is maintained within the [FONT=Courier New]$_POST[/FONT] superglobal array).

At the end of the IF statements, there is the ($name && $email && etc) stuff where there is no else after it. Is it looking to see if TRUE is flagged to all of these?

Also, sometimes I just see "if ($var);" then straight on to something else without an interrogative. What is happening with these? I gather there's a truth test but what passes as true if there seems to be no specific test accociated with it? Some clarity on these issue would help me understand what underlies these type of IFs[/QUOTE]
It is to checking if the variable is equal to true, and as all variables that either exist, aren't empty or aren't explicitly set to [FONT=Courier New]FALSE[/FONT] (or [FONT=Courier New]0[/FONT]) evaluate as true (provided there is no comparison), it is basically checking that all required fields have been completed.
Copy linkTweet thisAlerts:
@NogDogNov 06.2006 — Just to elaborate on the variable evaluating as true/false issue, there are several type/value combinations which will evaluate as FALSE in a [b]if($var)[/b] expression. See the "Converting to Boolean" section of [url=http://www.php.net/manual/en/language.types.boolean.php]this manual page[/url] for all the possibilities. If the variable does not match one of those conditions for being FALSE, then it will be evaluated as TRUE.

If you want to check to see if a variable is expressly set as a boolean TRUE or FALSE value, then you need to use a type-sensitive comparison operator ([b]===[/b] or [b]!==[/b] instead of [b]==[/b] or [b]!=[/b]):
[code=php]
if($var === TRUE) {
// or:
if($var !== FALSE) {
[/code]
Copy linkTweet thisAlerts:
@VirtualBobauthorNov 06.2006 — thanks guys - that's cleared it up for me
Copy linkTweet thisAlerts:
@so_is_thisNov 06.2006 — ... sometimes I just see "if ($var);" then straight on to something else without an interrogative. What is happening with these?[/QUOTE]
You can think of these as performing an existence and content test on most any type of variable or object. If the specified variable is set and has acceptable content, then the statement evaluates to TRUE. Unacceptable content include NULL, empty strings, numeric and string 0, and the FALSE value itself.

However, it is considered sloppy coding to use this method for other than true BOOLEAN variables.
Copy linkTweet thisAlerts:
@pcthugNov 07.2006 — Unacceptable content include... [B]numeric[/B] [values][/quote]
That is incorrect, any numeric value (numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part) other than 0 will evaluate as true:
[code=php]
if(0) // will evaluate as false
if(1) // will evaluate as true
if(+0123.45e6) // will evaluate as true
[/code]
Copy linkTweet thisAlerts:
@so_is_thisNov 07.2006 — ... other than 0 will evaluate as true:[/QUOTE]
Not according to the manual. Apparently you misunderstood what I was saying. The manual states that all of the following will evaluate as false:


'0'

0.0

That is why I said "numeric and string 0"
×

Success!

Help @VirtualBob spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...