php: why 14 + ' ' + 12 returns 26? [duplicate] - php

This question already has answers here:
Adding string with number in php
(6 answers)
Closed 2 years ago.
I'm a beginner in PHP. with my experience something like "number + String + number" should be a string.
But apparently, it's a number in php.
in my case:
echo 14 + ' ' + 12;
// returns 26
// expected: "14 12"
why did this happen?
and how to fix it?

You are adding a '+' for adding two numbers. These are not a string so it will do a addition.
Use this if you want to add these couple of number as a couple of string :-
echo '14'.' '.'12';
// result "14 12";
You can also use with a html code for a blank space :
echo '14&nbsp12';
// result "14 12";

Because echo accepts parameters only inside quotes. If you want text being returne use:
echo"14 12";
In you case PHP does 2 things
Takes 14 adds to 12
Concatenates space.

Related

How to change all front decimal to 0 in php [duplicate]

This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod

PHP - PRINTF Not Printing array $variable[$i] [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 3 years ago.
I'm trying to printf a variable with some 000 on the left side but only print the 000 and no print the variable.
$activePlayers1 = array(3)
numero = $activePlayers1[$i];
printf('000',$numero);
The result of this is 000 and no print the 3.
If you are trying to pad with zeros, you should use a padding specifier:
<?php
$numero = 5;
printf("%'.09d", $numero);
?>
yields
000000005
Try it yourself
The d is a type specifier, used to treat the variable value as an integer and display it as a decimal, and you can find the full list of type specifiers under the sprintf documentation.
d - the argument is treated as an integer and presented as a (signed)
decimal number.
It should be like this
$activePlayers1 = array(3)
numero = $activePlayers1[$i];
printf('%d',$numero);

reverse chunk split of integer in PHP [duplicate]

This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);

Print number with spaces as thousands place separator [duplicate]

This question already has answers here:
PHP - Correct way to add space between numbers?
(3 answers)
Closed 1 year ago.
I'm trying to reverse a string and add spaces from 3 in 3 characters the code I have right now is this:
$rowPreco = "925000"
$rowPreco = strrev($rowPreco);
$rowPreco = wordwrap($rowPreco , 3 , ' ' , true );
$rowPreco = strrev($rowPreco);
if I take the strrev out it prints how I want ("925 000") but if I have the strrev it will print ("92 500 0").
But I need to use the strrev because if the value is ("1200000") it will print ("120 000 0") instead of ("1 200 000").
Any help would be appreciated.
Thanks!
UPDATE!
When I use the number_format it will ignore a jQuery code that I have.
echo "<script>"
."jQuery(document).ready(function() {"
."var parts = jQuery('.casaPreco').text().split('|');"
."jQuery('.casaPreco2').text(parts[4]);"
."});"
."</script>";
Basically the initial string is something like this: "3880562|1|1|925000|||0|0|0"
I need to grab the 4th number so I'm splitting the string on the "|" and use the array parts[4] but when I use the number_format it will grab the number "3880562"
Please do not invent the wheel:
echo number_format('925000', 0, '.', ' '); // 925 000
echo number_format('1200000', 0, '.', ' '); // 1 200 000
number_format manual.

Minus value using php [duplicate]

This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have the value as $title =10 and I want to minus it with 01.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09 but this code not like this it still have the result $minus=9. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round() returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);

Categories