/    Sign up×
Community /Pin to ProfileBookmark

for a textfield that asks for a number amount to be entered and is does not have parameter set to it, when i put an “if statement” requiring information to be entered do i put

if($variable == “0”){……} ?

or

if($variable == false){……} ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@Stephen_PhilbinJun 13.2005 — When dealing with zero as an integer it's often best to use the strict equality operator. Using === rather than == means that the variable or value you are comparing is of the same type as well as value as the other value or variable. So ===0 checks the value being compared is an integer too and === FALSE checks that the value being compared is a boolean value too.

So 0 == FALSE is true, but 0 === FALSE is not true.

Also 0 == "0" is true, but 0 === "0" is not true.

If 0 is recieved as a value from a form it is considered a string value. (that being the "0" version). If 0 is put between " or ' marks it is considered a string (text), but if it is a number on its own, it is considered an integer (a mathematical object that can be used in a mathematical way).

Does that make sense of should I ramble incoherrantly a bit more?
Copy linkTweet thisAlerts:
@xredawg909xauthorJun 14.2005 — my C++ background sorta helps me understand, but just incase i think the incoherent rambling should help. maybe i didn't explain clearly... say i'm asking a current bank balance (text field) , they leave it blank and hit submit and in my php code i didn't set it to $bal(double or float)

and i want them to go back and enter a value for their balance if they left it blank

would my if statement look like if($bal == 0){please go back and enter missing field} or if($bal==false){please go back and enter missing field} ? or would it be single "= " ? been a while since i've taken c++ so sorry if i seem a little slow here =)
Copy linkTweet thisAlerts:
@NogDogJun 14.2005 — [code=php]
# check to see if it is blank:
if($bal === "") { # do something }

# check to see if it is blank or 0:
if(empty($bal)) { # do something }

# check to see if it is boolean false (where 0 is not the same as false):
if($bal === FALSE) { # do something }
[/code]
Copy linkTweet thisAlerts:
@xredawg909xauthorJun 14.2005 — coo thanx that resposnse hit the mark just one more question, if i have multiple values i don't want empty or 0 would it be if(empty($bal) || empty($variable2) || " " ||) ?
Copy linkTweet thisAlerts:
@NogDogJun 14.2005 — The example you gave is a bit weird, with the third clause being just a space character and ending with a "||", but in theory, yes, you could do:
[code=php]
if(empty($var1) || empty($var2) || empty($var3))
{
# error
}
else
{
# all required variables populated with non-zero values
}
[/code]

Here's an algorithm I've used sometimes:
[code=php]
# required fields in format input_id => description:
$required = array("field1" => "Field One",
"field3" => "Field Three",
"field5" => "Field Five");
# initialize error message:
$error = "";
# check each required field:
foreach($required as $key => $val)
{
if(empty($_POST[$key]))
{
$error .= "<p>ERROR: You must enter a value for $val.</p>n";
}
}
if(!empty($error))
{
echo $error;
}
else
{
# proceed with processing of form...
}
[/code]
Copy linkTweet thisAlerts:
@xredawg909xauthorJun 15.2005 — this php code looks similar to c++ would i be able to apply the same basic principles if i wanted to make a custom code?
×

Success!

Help @xredawg909x 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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