This question already has answers here:
Decrementing alphabetical values
(3 answers)
Closed 7 years ago.
I have the following code.
$letter = 'D';
$letter++;
echo $letter; //outputs E
$letter--;
echo $letter; //outputs E
Why isn't the subtract function working?
You should make use of ord() and chr() functions:
$letter = 'D';
$val = ord($letter);
$val++;
echo chr($val);
$val--;
echo chr($val);
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
I have a $total with a value of 1400 which I'm trying to echo as 1,400.00 however sprintf and number_format refuse to cooperate with each other. How do I properly format the $total to echo as 1,400.00?
<?php
$total = 1400;
echo sprintf('%0.2f',$total);
//'1400.00'
echo number_format($total);
//'1,400'
echo sprintf('%0.2f',number_format($total_grand));
//'1.00'
echo number_format(sprintf('%0.2f',$total));
//'1,400'
?>
This should suffice:
$total = 1400;
echo number_format($total,2);
This will output:
1,400.00
See: number_format()
This question already has answers here:
Number to words in php using NumberFormatter class
(2 answers)
Closed 2 years ago.
hi im using php NumberFormatter in laravel
now if i want to echo number i did this code
#php
$digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
echo $digit->format(round(10.557,3));
#endphp
the result will be
but i need it to be
ten and five handred and seventy five
how can i change the format ..
thanks ..
you can try to build a function manually
function numberToText($number) {
$digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
$numberParts = explode('.', (string) round($number,3));
$formatedNumber = $digit->format($numberParts[0]);
if (isset($numberParts[1] ) {
$formatedNumber .= ' and ' . $digit->format($numberParts[1]);
}
return $formatedNumber;
}
#php
echo numberToText(10.557);
#endphp
This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy
This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same
This question already has answers here:
make string of N characters
(5 answers)
Closed 10 years ago.
We have
$symb="_";
$num=10;
We want $ten_symbs to be exactly "__________"; // ten symbols "_".
Whats the fastest and/or the best way to assign ten "_" to $ten_symbs?
str_repeat():
$symbol = '_';
$num = 10;
echo str_repeat($symbol, $num);
You can use str_repeat as:
$ten_symbs = str_repeat($symb, $num);
You can also do:
$ten_symbs = str_pad('',$num,$symb);
But the fist option is cleaner.
look for str_repeat() here: http://www.php.net/manual/de/function.str-repeat.php
$ten_symbs = '';
for($i=0;$i<$num;$i++) {
$ten_symbs .= $symb;
}