string comparison in php - php

I am trying to compare two strings I get from a password form in HTML.
They are being stored in variables from $_POST. I print them out and they look the same, but the code below will never evaluate to true, only false. Why does that happen?
//Verify the passwords match
if( ($passwd != $pass_confirm) && ($new_email != $email_confirm) ){
echo "Email and/or password do not match";
return FALSE;
}
I appreciate any help.

For your code to show the error message, both the email and password must be wrong.
Try using || instead of &&, so the error is shown when just one of them is wrong.

If you print them out and they look the same, you might have a trailing newline character problem. Perhaps you could try trimming the strings before comparison? Doing a var_dump might help to pinpoint the problem because it shows the length of the string.
Also, I would suggest the following check (note the || and strict comparison operators):
if ($passwd !== $pass_confirm || $new_email !== $email_confirm) {
echo "Email and/or password do not match";
return false;
}

Related

Why does a second if work when an else doesn't? (PHP)

Can anyone explain why this:
if($xml->$ul !== ""){
echo $xml->$ul;
}
if($xml->$ul == ""){
echo "0";
}
does work, while
if($xml->$ul !== ""){
echo $xml->$ul;
}else{
echo "0";
}
does not work?
Am i missing something?
Short explanation: if the xml contains $ul it will echo its value, if it is not contained it will echo 0. Works perfectly with first code, but second code just echos the values, the else is completely ignored.
I appreciate all answers!
You are not doing the same equality check. In the first example you are first checking using !==, then in the second if you are using ==.
See this answer for an explanation of the difference between === equality and == equality. In short, === not only checks the values are equal, but also that the types of the variables being compared are the same as well.

Validating an if statement with two conditions

I'm trying to validate a form. The form has a simple if statement. What is wrong with this please? The browser says: Parse error: syntax error, unexpected '&&' . Both the variables being checked are separate fields. They must both be numbers & not empty.
if (isset($_POST["submit"])) {
if(empty($numberwelds)) && (empty($conwelds)) {
echo " One or both of the numbers are empty ";
} else if(!is_numeric($numberwelds)) && (!is_numeric($conwelds)) {
echo "Data entered was not numeric";
} else {
echo "it passed";
}
}
I'm still getting ' one or both of the numbers are empty', even if they both are numbers. My new code:
if (isset($_POST["submit"])) {
if(empty($numberwelds) || empty($conwelds)) {
echo " One or both of the numbers are empty ";
} else if(!is_numeric($numberwelds) || !is_numeric($conwelds)) {
echo "one or both of them is not a number";
} else {
echo "it passed";
}
}
Thank-you
Your problem is on the lines where you have;
if(empty($numberwelds)) && (empty($conwelds)) {...
and,
} else if(!is_numeric($numberwelds)) && (!is_numeric($conwelds)) {
you are basically trying to create an if statement of the form;
if(stuff in here) && (stuff in here){..
This is a syntax error because if statements need to be followed immediately by a bracketed control flow, and in your example you have an if statement followed by &&.
All you need to do is add some more brackets to make a correct if statement, for example;
use
if((stuff in here) && (stuff in here)){...
instead of
if(stuff in here) && (stuff in here){..
If you want your error output One or both of the numbers are empty to be accurate, you have to use || and not &&
What's happening is even if one of the options is empty, the if statement is ignored. And && means both statements must be true. An || means the either-or OR both must be true.
As far as the syntax error, the other answered solved it for you.

PHP validation function. Check for numeric and positive

Little confused about how to code this. User inputs numbers, then the validation checks to make sure it is numeric and positive. The user can also leave this field blank.
This is what i have so far, it just checks to see that something was inserted.
$error_blue = check_blue($phone);
if($error_blue !=''){
print "<p>Blue: $error_blue";
}
Is where the item is validated at the top of the page.
function check_blue($blue){
if(! is_numeric($blue)){
return'Please Enter a valid number for Blue.';
}}
Is where the function is.
Any help on what to do here would be much appriciated.
Something like this?
function check_blue($blue) {
if (is_numeric($blue) && $blue > -1) {
return 1;
}
else {
return 0;
}
}
if(!check_blue($phone)) {
return'Please Enter a valid number for Blue.';
}
See the demo
Assuming it's an integer you expect,
if ((int)$blue==abs($blue) || empty($blue)) $error = false;
else $error = true;
Then you use $error to decide what to do.
Everyone here was using if else. It is not that this is wrong, but just to show you another idea, which might be useful for other type of validation, take a look at validate filters :
http://www.php.net/manual/en/filter.filters.validate.php
For this purpose you might use FILTER_VALIDATE_INT
You might want to use something simple as
function check_blue($phone) {
return (empty($phone) || (is_numeric($phone) && $phone >= 0));
}
First check if the field is blank, nothing to do if it is.
If it is not blank it will be checked for numericality and to being greater or equal zero.

Understanding XOR PHP

I am writing section in my program to update a user, and there is password and confirm passwords. I dont want to do anything to the password field in the DB unless they have filled the fields in.
So I have error checking for:
if(($sql['password']!="") && ($sql['cpassword']!=""))
{
if($sql['password'] == $sql['cpassword'])
{
//update
}
}
However I want to write a quick line to throw an error message if they only filled out one of the password fields. So I figured:
if($sql['password'] ^ $sql['cpassword'])
{
echo You must fill out <i>both</i> password fields and they <i>must</i> match.";
}
Would do it, but It doesnt seem to. Then I added a NOT operator, !, and that seemed to work well, however if both fields have nothing in them, I still get the error message :(
From the logic I can see from this answer, It would seem that simple XOR would work. But
But it doesnt, Can someone explain to me why?
XOR certainly is not what you want here.
As David states, XOR will return a "true" result if the values are different, however when you XOR two strings the XOR operation is only done up to the point of the shortest string.
Examples
'AAAA' ^ 'AAAA' This returns an empty string (false-equivalent value) as the values are the same.
'AAAA' ^ 'bbbb' This returns an non-empty string (true-equivalent value)..
'AAAA' ^ 'AAAAbbbb' This returns an empty string (false-equivalent value), even though the strings are different. This is because the result of the operation only considers the first 4 characters.
In Sandy Lee's example (bool)$string does not really help.
(bool)'0' = false
(bool)'1' = true
This does not tell you if the string is empty or not. It simply gives you the boolean-equivalent value of the string.
There is no need to use XOR here at all. It's not the right tool for the job. There is no need to try and do anything fancy either, the simple tools work perfectly.
$password = trim($password);
$confirm = trim($confirm);
if (!$password || !$confirm) {
// One of the fields was not completed.
}
elseif ($password !== $confirm) {
// Fields did not match
}
else {
// Update password
}

PHP checking if empty fields

I have a sign up form with about 10 fields and they are all required to be filled in before processing, so trying to avoid all those if checks I came up with this, is this a good method?
foreach($list as $items) {
if(empty($items)) {
$error[] = "All fields are required.";
break;
}
}
or should I make if(empty($field_1) || empty($field_2) etc.. then output the error?
Assuming that your data is coming from $_GET or $_POST, all data fields will be strings. This means that you should be able to do the check in a single function call:
if (in_array('', $list, TRUE)) {
$error[] = "All fields are required.";
}
This looks for strings which are exactly equal to an empty string. If you want to make the comparisons loose (more or less identical to the check that empty() does) just remove the final TRUE.
EDIT Thinking about it, you don't need the strict comparison. I did this to allow for a legitimate field value of '0' (which empty() would not allow) but this will also be permitted with loose comparisons, since '0' != ''.
ANOTHER EDIT If you want to check that the length of the sting is greater than two, you will have to loop:
foreach ($list as $item) {
if (strlen($item) < 2) {
$error[] = "All fields are required.";
break;
}
}
This will also "clear out 0" assuming that by this you mean "not allow a value to be 0". If you also want to disallow '00' (or any other string that results in 0) you can change the if clause to this:
if (strlen($item) < 2 || (!(int) $item)) {
it's ok. If you just want to show message "All fields are required." without showing which field in blank.
Otherwise it will be more user friendly if you check and show which field is left empty.
It's good idea to put it into loop as you did but please note that this will fail even when user inputs 0 and will pass for string containing only spaces, so you may want to make better checks than empty()
I would approach this with an in_array check.
<?php
$fields=array('name','age','yadayada','something_else');
foreach ($_POST as $key=>$value){
if(in_array($key,$fields) && $value!=''){
$$key=$value;
}else{
$error[$key]='This field is required.';
}
}
?>

Categories