This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
PHP: Shortest way to check if a variable contains positive integer?
How do I know, is a given variable a number without residue (and not a negative number)?
Like we have numbers (each number is a variable): 5; 6.416; -76; 254.
Its better to have some function, which would give true or false, like:
number "5" is true
6.416 is false (has residue "416")
-76 is false
254 is true.
Thanks.
if ( (int)$num == $num && (int)$num > 0 ){
return true;
}
This should do it:
function is_positive_int($n) {
return $n && abs(intval($n)) == $n;
}
Related
This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);
This question already has answers here:
Test if number is odd or even
(20 answers)
Closed 7 years ago.
How can I get if a number is even or odd or neither (have decimal, like 1.5) with PHP? I know that there are operators like *, /, but they did not work.
Here's a try (of course it did not) (work that's just to find if it's a even number):
function even($n) {
return (($n/2)*2 == $n);
}
echo even(1); // true (should be false)
echo even(2); // true
How about
function even($n) {
if (!is_int($n)) {return 'n';}
return !($n % 2);
}
even(1); // false;
even(2); // true;
even(1.5); // 'n'
The danger here is that 'n' will evaluate as false if used as a boolean. It might be better to return some specific constants instead of true or false. The OP didn't specify what the return values should be.
It is pretty simple. modulo (%) is the operator you want, it determines if there would be a remainder if x is divided by y... for example (3 % 2 = 1) and (4 % 2 = 0).
This has been asked before too - pretty common question - you really just need to see if your number, $n % 2 is equal to 0.
php test if number is odd or even
Check if given number is integer first. And bitwise & to check if it is even or odd. Here is an example...
if (is_int($n)) {
if ($n & 1) {
echo 'Odd!';
} else {
echo 'Even!';
}
} else {
echo "Not a Integer!";
}
Hope this is helpful.
Use the modulo operator (%) to determine whether the integer is divisible by 2. You also need abs() to handle negative numbers, and is_int() to handle the fact that the modulo operator doesn't correctly handle floating point numbers. An example implementation follows:
function is_even($num) {
return is_int($num) && abs($num % 2) == 0;
}
function is_odd($num) {
return is_int($num) && abs($num % 2) == 1;
}
// this last one seems self-explanatory, but if you want it, here it is
function is_neither_even_nor_odd($num) {
return !is_even($num) && !is_odd($num);
}
// Tests: The following should all output true:
var_dump(
is_even(0),
is_even(2),
is_even(-6),
is_even(51238238),
is_odd(1),
is_odd(-1),
is_odd(57),
is_neither_even_nor_odd(1.5),
is_neither_even_nor_odd(2.5),
is_neither_even_nor_odd(-0.5),
is_neither_even_nor_odd(0.00000001)
);
Here's a demo.
is_numeric returns true if the given variable is a number
is_int returns true if the given variable is an integer
The modulor operator % can be used to determine if an integer is even or odd:
$num % 2 == 0 // returns true if even, false if odd
This question already has answers here:
Truncate float numbers with PHP
(14 answers)
Closed 9 years ago.
I have a float number in PHP : 0.966666666667
I would like to print it like : 0.96 I used round() and number_format() but they give me both
0.97 is there a function to do that please ?
You can do this:
$num = 0.966666;
$num = floor($num * 100) / 100;
The best way to do this I've found is this:
//$val - the value to truncate
//$dist - the number of digits after to decimal place to keep
function truncate($val, $dist) {
//get position of digit $dist places after decimal point
$pos = strpos($val,'.');
if($pos !== false) {//if $val is actually a float
//get the substring starting at the beginning
//and ending with the point $dist after the
//decimal, inclusive -- convert to float.
$val = floatval(substr($val, 0, $pos + 1 + $dist));
}
return $val;
}
Then just call truncate($YOUR_NUM, 2);.
Source: https://stackoverflow.com/a/12710283/3281590
This question already has answers here:
Php Regular Expression repeated characters
(2 answers)
Closed 9 years ago.
I've got a Call Me Back form which sends me a phone number of a person who wants to be called back. Today I received the form with '88888888' instead of a real phone number.
How can I check if the string contains 1 and the same number, continous?
There must not be more than 4 same numbers in a row.
To check if the string contains only one repeated integer, string -> array, check if the unique count is 1.
<?php
$string = "88888888";
$array = array_unique( str_split( $string ) );
$result = $array;
if( count($result) === 1 ) {
echo "Same number repeated in string";
}else{
echo "More than 1 number found in string";
}
?>
-Edit-
optimized: Removed for loop thanks to comment by #Uberfuzzy
$number_string = (string)$number_string;
return strlen($number_string) > 0 && str_repeat($number_string[0], strlen($number_string)) === $number_string;
If I get an integer variable (anywhere from 0+) There are a few things I can do to make sure the number is not 0(zero):
Option 1:
if($number > 0){
// number is not zero
}
Option 2:
if($number){
// number is not zero
}
Option 3:
if((bool) $number){
// number is not zero
}
Option 4:
if(!!$number){
// number is not zero
}
Etcetera....
Which one of the above is considered really the best to do?
Or is there an even better option?
Use the identical comparison operaton, which does not do any type juggling (and is faster).
if ($number !== 0) {
// ^^^
// Number is not identical to 0
}
Note: This is assuming the variable is actually a "integer variable", and not a string that happens to contain a number.
if (false == ($number === 0)) {
// ^^^
// It is false that Number is identical to 0
}
I've always used this:
if(!empty($number)){
// Number is not 0
}
I think of it as two birds with one stone.
Variable set.
Not 0
if($number != 0){
// Number is not 0
}