Php floating if statement Confused [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 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

Related

How to get the numbers after the decimal place for an if statement in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.

Comparison with && and || not working [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 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.

Php sum numbers with the same string [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 7 years ago.
Improve this question
i try sum numbers with the same string , for get one only number to the end , for example this :
<?php
$result.="1";
$result.="4";
$result.="8";
$result.="346";
$end +=result;
echo $end;
?>
I try do this but no get never the result ok , only show all numbers but not the result of the sum , by this my question , i don´t know if i writte something bad or similar
Thank´s for the help , regards
You are concatenating instead of adding.
<?php
$result+=1; // Need the += here instead of .=
$result+=4;
$result+=8;
$result+=346;
$end +=result; // Like you already did here.
echo $end;
The problem is that you are trying to add up STRINGs not INTs. You have to convert the values.
$result+=intval("1");
$result+=intval("4");
$result+=intval("8");
$result+=intval("346");
$end =$result;
echo $end;
This is not how you are supposed to do it, if you really want to store every number, just use an array :
<?php
$myNumber = [];
$myNumber[] = 1;
$myNumber[] = 4;
$sum = 0;
foreach($myNumber as $r)
$sum += $r;
echo $sum;
?>
$sum will now contain the sum of every number. You could also use the array_sum function to perform this operation.

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

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