php string echoes as 0 when it shouldn't [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 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";}?>

Related

Add two decimal values [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 4 years ago.
Improve this question
so I want to add two values (money values) but can't quite work it out.
I keep getting a notice, I've tried number_format to format it but even using that I get the 'Notice: A non well formed numeric value encountered'
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
$price = $price + $dbprice;
//echo number_format($price + $dbprice,2);
}
however I get this problem: Notice: A non well formed numeric value encountered
If I add two values '5' + '1.5' I get '6' as a result.
Edit:
The values I were pulling from the database were formatted with commas.
PHP numbers uses dot as separator and seems that you have coma. Please replace the coma with the dot as first. After that you can cast the prices into floats and finally sum it up :)
You can try with this (based on you example):
<?php
$price = 0.00;
while($row = $result->fetch_assoc()) {
$dbprice = $row["ProductSellPrice"];
// Replace comma. Check also for possible thousand separator.
$dbprice = str_replace(',', '.', $dbprice);
$price = $price + (float)$dbprice;
//echo number_format($price + $dbprice, 2);
}
?>

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

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.

Whitespace between numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I would like to set a whitespace between numbers bigger than 999.
So for example:
Not "1000" but "1 000",
Not "10500" but "10 500";
How to do it while formatting a string?
Try with number_format, something like this:
<?php
$number = 10500;
echo number_format($number, 0, '.', ' ');
// Output 10 500
?>
This should do the trick.
Try using number_format() function.
Example:
$a = 1000;
echo number_format($a, 0, '.', ' ');
You maybe looking for the php function number_format()
$number= 1000;
if($number > 999){
$number = number_format($number, 0, '', ' ');
echo $number;
}
DEMO
You could use the substr() function inside of php

Use of undefined constant ​ - assumed '​' in php [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
<?php
$f=$_POST['rdate'];
echo $f."</br>";
$from=$f.'-01 00:00:00';
$dayyy=explode("-",$f);
//print_r($dayyy);
$year=$dayyy[0];
echo $year."</br>";
$mm=$dayyy[1];
echo $mm."</br>";
$mons = array("01" => "Janauary", "02" => "February", "03"=>"March", "04"=>"April", "05"=>"May", "06"=>"June", "07"=>"July", "08"=>"August", "09"=>"September", "10"=>"October", "11"=>"November", "12"=>"December");
print_r($mons);
/*foreach($mons as $mm)
{
echo $mm;
}*/
$month_name = $mons[$mm];
echo "</br>".$month_name;
$days=cal_days_in_month(CAL_GREGORIAN, $mm, $year);
$dayss= $days-1;
echo $dayss."</br>";
$dayys=' + '.$dayss.' days' ;
$var=$from.$dayys;
//echo $var."</br>";
echo "Last".date('Y-m-d 23:59:59',strtotime($var));​
?>
i'm getting Use of undefined constant ​ - assumed error in the above code.
I guess i got this issue due to non breaking spce but don't know where i left that?
Kindly help!!
You have some kind of weird character on your
echo "Last".date('Y-m-d 23:59:59',strtotime($var));
line
notepad++ couldn't see it but when I tried to arrow throw it, it went through twice.
Anyways, remove the line and copy and paste this
echo "Last".date('Y-m-d 23:59:59',strtotime($var));
check the constant you are using is defined or not ..
if not define it
the code will be
<?php
if (!defined('CAL_GREGORIAN'))
define('CAL_GREGORIAN', 0);
$f=$_POST['rdate'];
echo $f."</br>";
$from=$f.'-01 00:00:00';
$dayyy=explode("-",$f);
//print_r($dayyy);
$year=$dayyy[0];
echo $year."</br>";
$mm=$dayyy[1];
echo $mm."</br>";
$mons = array("01" => "Janauary", "02" => "February", "03"=>"March", "04"=>"April", "05"=>"May", "06"=>"June", "07"=>"July", "08"=>"August", "09"=>"September", "10"=>"October", "11"=>"November", "12"=>"December");
print_r($mons);
/*foreach($mons as $mm)
{
echo $mm;
}*/
$month_name = $mons[$mm];
echo "</br>".$month_name;
$days=cal_days_in_month(CAL_GREGORIAN, $mm, $year);
$dayss= $days-1;
echo $dayss."</br>";
$dayys=' + '.$dayss.' days' ;
$var=$from.$dayys;
//echo $var."</br>";
echo "Last".date('Y-m-d 23:59:59',strtotime($var));

Categories