Comparison with && and || not working [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This first comparison will check if the value is 14 in length and the User ID is 1
if ($checkValueLenght === 14 && $UserID[0] === "1") { code... }
but I want it to check if it is 1 or 2, and I cant get this to work ...
if ($checkValueLenght === 14 && $UserID[0] === "1" || "2") { code... }
it goes trough the code whatever letter or number the UserID starts with, but not if it is not exactly 14 in length. What am I doing wrong?

You need this ...
if ($checkValueLenght === 14 && ($UserID[0] === "1" || $UserID[0] === "2")) { code... }
or better, this ...
if ($checkValueLenght === 14 && in_array($UserID[0],["1","2"]) { code... }
assuming that the value of $UserID[0] is indeed a string.

Related

Php floating if statement Confused [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
function is_decimal( $it )
{
return is_numeric( $it ) && floor( $it ) != $it;
}
if (is_decimal == true){
echo "Decimal";
}
This gives me an error even though the int is not a decimal (float). Can someone help me. Thanks
It works fine for a decimal but not for an int.
Simple, when you call a function with a parameter you have to pass a parameter!
function is_decimal( $spaces )
{
return is_numeric( $spaces ) && floor( $spaces ) != $spaces;
}
$tst_var = 1.99
if (is_decimal($tst_var) == true){
echo "Error 3: Decimal causing Error as carpark can't have decimal spaces available.";
}
just use if(is_numeric( $spaces )){ ... } work for int, float even if they are represented by string

how to make password >7 characters (validation)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a form, but I am having trouble getting the strlen function to work.
Below is an example of the code - there is validation further down.
I've commented out the code that isn't working. Basically, all I want to do with this section of code is determine that the passwords match, and are more than 7 characters long.
Can anyone help?
if (isset($_POST['formName']) && $_POST['formName'] == "addUser") {
if ( ( $_POST['frmName'] != '') &&
($_POST['frmSurname'] != '') &&
($_POST['frmEmail'] != '') &&
($_POST['frmPassword1'] != '') ) {
if ($_POST['frmPassword1'] != $_POST['frmPassword2'] ) {
echo "Passwords do not match!";
}
/* if (strlen( ($_POST['frmPassword1']) < 7 ) {
echo "Passwords much be a minimum of 7 characters";
} */
Look at your ():
strlen( ($_POST['frmPassword1']) < 7 )
a b b a
^-----strlen-------------------^
You're not testing the length of the $_POST value, you're doing strlen on the boolean result of foo < 7, which will always be 0/1:
php > var_dump(strlen(true), strlen(false));
int(1)
int(0)
YOu need:
if (strlen($_POST['frmPassword1']) < 7) {
a b b a
Note the labels on the ().
This is where its messed up:
if (strlen( ($_POST['frmPassword1']) < 7 ) {
Let's start that statement over.
First you want the string represented by form field frmPassword1:
$_POST['frmPassword1']
Then you want the string length:
strlen($_POST['frmPassword1'])
Then you want to compare it to less than 8 because you specifically asked for more than 7 characters. Therefore, your expression would be:
strlen($_POST['frmPassword1']) < 8
Now make that a complete condition like so:
if( strlen($_POST['frmPassword1']) < 8 ){
//insert relevant code here telling users password is too short
}
Now you have a working block of code.
You are missing end )
if (strlen( ($_POST['frmPassword1']) < 7 ) {
1 2 3 3 2 # 1 is missing
So it would be
if (strlen( ($_POST['frmPassword1']) < 7 ) ){
1 2 3 3 2 1
NOTE : In your question you have mentioned that passwords match, and are more than 7 characters. So use <= (less than or equal).

Why does DateTime format return as a non-object on my second date format? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting an error that shows only if this DateTime format function is called a second time. It works fine on the first one, which is something I am assuming because the error only occurs on the line of the second call.
if( ! function_exists('month_dropdown')){
function month_dropdown($field_name = 'month', $selected = '01', $value_format = 'm', $atts = ''){
for($i=1;$i<=12;$i++){
$numObj = DateTime::createFromFormat('!'.$value_format, $i);
$val = $numObj->format($value_format);
$nameObj = DateTime::createFromFormat('!F', $i);
$text = $nameObj->format('F');
$months[$val] = $text;
}
return form_dropdown($field_name, $months, $selected, $atts);
}
}
This is the error I am getting:
Fatal error: Call to a member function format() on a non-object in ...application\frontend\helpers\MY_html_helper.php on line 73
Line 73 is where the variable $text is defined.
You're using the wrong formatting character. With month integers, use n
m and n
Numeric representation of a month, with or without leading zeros
Example: 01 through 12 or 1 through 12
For example
$nameObj = DateTime::createFromFormat('!n', $i);
$text = $nameObj->format('F');
Demo here ~ https://eval.in/188555

Check Positive Float value in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a variable as $x=some_float_value. So how can I validate or check that $x is a Positive Float.
For Ex.
$x=-4.99;
function checkFloat($x){
/////return true if it is Positive
}
Please Tell me how can i do this.
Use filter_var() with FILTER_VALIDATE_FLOAT:
if (filter_var($x, FILTER_VALIDATE_FLOAT) && $x > 0) {
// ok
}
See it in action
Check if it is a float with is_float and > 0:
function checkFloat($x) {
return (is_float($x) && $x > 0);
}
I do not see the issue...
return $x > 0;
To conclude:
function checkPositive($x) {
return $x > 0;
}

php string echoes as 0 when it shouldn't [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm writing code to show the prices of some products. If the price is 0 it doesn't show anything.
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
<? if ($book->price_gbp != 0) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if ($book->price_usd != 0) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>
This echoes "€0.00 £33.00 $66.00 ". The € price is set to 99. I can see no reason whatsoever that this should echo as 0! Am I doing something wrong? Bad syntax?
$europrice = number_format($book->price_eur, 2
shouldn't it be $book->price_euro ?
Typo in Euro
number_format($book->price_eur, 2); // This is what you have.
number_format($book->price_euro, 2); // This is what you need.
I don't see anything wrong with this code without seeing more. But maybe your variable?
<? if ($book->price_euro != 0) {$europrice = number_format($book->price_eur, 2); echo "€$europrice";}?>
price_eur should be price_euro?
number_format($book->price_eur, 2);
should be
number_format($book->price_euro, 2);
The null value is causing your problem.
You might still be getting null values or something else try using empty instead
<? if (empty($book->price_euro)) {$europrice = number_format($book->price_euro, 2); echo "€$europrice";}?>
<? if (empty($book->price_gbp)) {$gbpprice = number_format($book->price_gbp, 2); echo "£$gbpprice";}?>
<? if (empty($book->price_usd)) {$usdprice = number_format($book->price_usd, 2); echo "$$usdprice";}?>

Categories