Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
<?php
$p= "php";
$P= "PHP";
echo ($p > $P);
?>
output: 1
it shows 1 meaning $p is greater than $P , but how are they compared i mean what are their values? and also how to display the values?
If you'd like to know how it works, a simple strcmp and ord functions can help you see them. Rough example:
http://php.net/manual/en/function.ord.php
Returns the ASCII value as an integer.
$p= "php";
$P= "PHP";
echo ord($p);
echo '<br/>';
echo ord($P);
echo '<br/>';
var_dump(strcmp($p, $P));
echo '<br/>';
var_dump($p > $P); // $p greater than $P
Output:
112
80
int(32)
bool(true)
It's based on each characters decimal value
http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
Related
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 2 years ago.
Improve this question
I want to convert int into comma separated string in php.
for example,
100000 into "1,00,000".
you can use number_format
$number=number_format(100000);
$echo $number; //output 100,000
or
$number=number_format(100000);
$newNumber=sprintf('"%s"',$number);
echo $newNumber ; //output "100,000"
you can use number_format() function
<?php
$number = '100000.457888';
echo number_format($number, 0); // returns 100,000
echo number_format($number, 1); // returns 100,000.5
echo number_format($number, 2); // returns 100,000.46
echo number_format($number, 3); // returns 100,000.458
echo number_format($number, 4); // returns 100,000.4579
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I just want to know how to convert inch based on the integer inputted in the field.
For Example:
$int = $_POST['int']; //value 1
//computation here...
$inch = //formula
//echoing the inch
echo $inch;
If you want convert from centimeter can you use this source
function cm2inches($cm)
{
$inches = $cm/2.54;
$inches = $inches%12;
return sprintf('%d ins', $inches);
}
echo cm2inches(162);
You will have result
3 ins
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know how I can randomize first and last two digits in php? Suppose the current code is:
FJA793HIYX
then after randomizing it should be:
HJA793HIGD
How can I do that?
Try
$str = 'HJA793HIGD';
$filter = substr($str,1,7);
echo $newstr= randLetter().$filter.randLetter().randLetter();
function randLetter() {
$int = rand(0,26);
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $alpha[$int];
return $rand_letter;
}
Try this!
$randomDigit = substr( "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,34 ) ,1 ) .substr( md5( time() ), 1)