Split 4 digit number into 2 variables in PHP [duplicate] - php

This question already has answers here:
split string after x characters
(3 answers)
Closed 7 months ago.
Using PHP...
If I have a number, for example, 0901, how can I then get 2 separate variables?
The first containing "09" and the second "01".
Thanks!

Using str_split. You can divide in half.
$number = "0901";
$arr = str_split($number, strlen($number)/2);
print $arr[0];
print $arr[1];

Related

How to count the number of times a "number" appears in string using php? [duplicate]

This question already has answers here:
How to check how many times something occurs in a string in PHP
(5 answers)
Extract a single (unsigned) integer from a string
(23 answers)
Closed 4 years ago.
I am trying to count how many times number appears in a string. For example, in the string "rt_876_io_542_po_367" there are three numbers and in the string "tr_766_ 756" there are two numbers. How do I do this with PHP?? I tried the following code:
$str="RT_657_YT_89";
$key=preg_match_all('!/_[0-9]_/!',$str);
echo $key;
but it echos "0"!! please help
This can easily be accomplished with a regular expression.
function countDigits( $str )
{
return preg_match_all( "/[0-9]/", $str );
}
The function will return the amount of times the pattern was found, which in this case is any digit.

PHP return 1 number after dot in float [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 6 years ago.
I have got this php code
$Likes=1112;
$Likes=$Likes/1000;
echo $Likes."k";
This code returns me 1.112k,but my goal is to get 1.1k
Use the number_format function:
echo number_format($Likes,1)."k";
And a coding style advice: don't start your variables with an upper case letter!

Generate random number with php must be 5 charachter... Even 1 = 00001 [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 7 years ago.
My question subject can be seen easy, but i need something different.
$string = rand(1,99999) // echo 1
But my need it should
$string = rand(---?----) //echo 00001 - Always 5 characters..
For example when $string echo "23" it should write 00023 , if the $string writes 2 character left side will be 3times zero - 3 charachter, should write left side 2 times zero - 4 character , should write 1 time zero...
Logic is always 5 character...
I found the solution...
str_pad($string, 5, "0", STR_PAD_LEFT);

Computing limits in PHP [duplicate]

This question already has answers here:
Is there something that's larger than any numbers in PHP?
(9 answers)
Closed 8 years ago.
I want to compute n^(2/3) as n approaches infinity in PHP.
What I tried:
$n = "INF";
echo pow($n, (2/3));
Desired result:
INF
Actual result:
0
Any suggestions? How to compute limits in PHP?
UPDATE:
OK, first problem solved - I just needed to remove the quotation mark.
But is this actually the way to calculate a limit? Is it interpreted as a limit?
You're passing in the string "INF" into the exponential expression, which isn't valid. Try passing in just POW:
$n = INF;
echo pow($n, (2/3));
// INF

shorten a long number with php [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 9 years ago.
how can i shorten a long number with php ? So a number like 0.3483748937847832 would become 0.34
Please search stackoverflow first - from PHP: show a number to 2 decimal places
number_format()
return number_format((float)$number, 2, '.', '');
https://stackoverflow.com/a/4483561/689579
or
$padded = sprintf('%0.2f', $unpadded); // 520 -> 520.00
https://stackoverflow.com/a/4483715/689579
Use round
echo round($number,2);

Categories