How to use operators so that they exclude zero? - php

In my database I have "maximum files" but I would like to set it at 0 for unlimited.
Therefore I need a way to
if ($count >= $max){
//a value of 0 in $max should not be here
} else {
//but here
}
Is this possible or do I have to create an exclusion for 0?

if($max && ($count >= $max)) ....

if (($max !== 0) && ($count >= $max) ){

if (!empty($max) && $count >= $max ) { ...

Related

check if 3 number are odd with PHP

I'm trying to check if these 3 numbers are odd:
if ( ($pezzi % 1) && ($base % 1) && ($altezza % 1) ) {
}
Why my script don't work? I do not understand why..
if ( ($pezzi % 2 !=0) and ($base % 2 !=0) and ($altezza % 2 !=0) ) {
}
Try this way:
if((number1 % 2) != 0 AND (number2 % 2) != 0 AND (number3 % 2) != 0){
-- do something
}
You should do like this:
if ($pezzi%2!=0 && $base%2!=0 && $altezza%2!=0) {
}
if ( ($pezzi & 1) && ($base & 1) && ($altezza & 1) ) {
}
Your statement is true. You just didn't compare it with 0:
if(($pezzi % 2) != 0 && ($base % 2) != 0 && ($altezza % 2) != 0){
}

PHP - Number of units between two numbers

I'm trying to find the number of units between 2 numbers that are under zero between 0 and a limit and over that limit. Here is my function. It works fine until I have to work with some huge numbers which takes a lot of time to process. I am trying to find a way to execute this code without using a loop.
public function getBetween($num1, $num2) {
$limit = 500000;
$array = array(0,0,0);
if ($num1 >= $num2) {
$low = $num2;
$high = $num1;
} else {
$low = $num1;
$high = $num2;
}
for($i=$low; $i < $high; $i++) {
if ($i < 0) {
$array[0]++;
} elseif ($i >= 0 && $i < $limit) {
$array[1]++;
} else {
$array[2]++;
}
}
return $array;
}
I have started to split my loop into elseif statements but this is getting messy really quick and I will also have to eventually be able to set more than one limit which will become impossible to use.
if ($low < 0 && $high < 0) {
} elseif ($low < 0 && $high >= 0 && $high < $limit) {
} elseif ($low < 0 && $high >= $limit) {
} elseif ($low >= 0 && $low < $limit && $high < 0) {
} elseif ($low >= 0 && $low < $limit && $high >= 0 && $high < $limit) {
} elseif ($low >= 0 && $low < $limit && $high >= $limit) {
} elseif ($low >= $limit && $high < 0) {
} elseif ($low >= $limit && $high >= 0 && $high < $limit) {
} elseif ($low >= $limit && $high >= $limit) {
}
I am trying to find a clean way to do it. Any ideas?
EDIT
Here is an example of the array I'm trying to get.
If my limit was 500, $num1 = -100 and $num2 = 700 i would get the array
$array[0] = 100
$array[1] = 500
$array[2] = 200
I didn't test it (didn't run a PHP script but I tried it "manually" with a few examples).
You still have loops, but only one iteration per limit (instead of one per unit).
// Example datas
$limits = array(0, 500, 800);
$low = -100;
$high = 1000;
$splittedResults = array();
// Get total of units
$totalUnits = abs($high - $low);
$totalCounted = 0;
foreach($limits as $limit) {
if ($low > $limit) {
// Nothing under the limit
$nbUnderLimit = 0;
} elseif($high < $limit) {
// Both values under the limit
$nbUnderLimit = $totalUnits;
} else {
// $low under the limit and $high over it
$nbUnderLimit = abs($limit - $low);
}
// Here we know how much units are under current limit in total.
// We want to know how much are between previous limit and current limit.
// Assuming that limits are sorted ascending, we have to remove already counted units.
$nbBetweenLimits = $nbUnderLimit - $totalCounted;
$splittedResults[] = $nbBetweenLimits;
$totalCounted += $nbBetweenLimits;
}
// Finally, number of units that are over the last limit (the rest)
$splittedResults[] = $totalUnits - $totalCounted;
You could create an array of the numbers with range() and use array_filter
$count = sizeof(array_filter (range(0,800), function($value){ return ($value > 500); }));
And one for < as well etc.
You only need to define range array once, separately.

Optimize if-statement to check values

How can I optimize this if-statement?
if ($min && $max && $value && ($min <= $max) && ($min <= $value) && ($value <= $max)) {
// do anything
}
What it should do:
I got three values (min, max and value). First of all, all values should be != 0. Second, min <= value <= max.
Valid:
min = 1; max = 3; value = 2;
min = 2; max = 2; value = 2;
this:
if ( 0 < $min && $min <= $value && $value <= $max ){
echo 'good';
}
The answer is:
if(isset($min , $max , $value ) && ($min <= $value) && ($value <= $max)){
//Insert your code here
}
I think this prevents any value from being a 0 and makes sure value is inside the min and max range.
if ( ( $min > 0 && $max >= $min ) && ( $value >= $min && $value <= $max ) ) {
echo "Good";
} else {
echo "Bad";
}

PHP ElseIf Statement with Numbers

The user can only enter a number between 1 and 5 - if they enter 0, leave the field blank or enter a number greater than 5 it will be default reset to 5. 1,2,3,4 are accepted otherwise.
$max=mysql_real_escape_string($_POST["max"]);
if ($max=="0" || $max==""){
$max_r="5";
} elseif ($max > "5"){
$max_r="5";
} else {
$max_r=$max;
}
However it always spits out 5.
Well, you're comparing strings and not integers. Try $max = (int) $_POST['max'] and don't wrap the values in quotes. Then, you can always escape $max before writing it to the DB.
$max = (int) $_POST['max'];
if ( ! $max || $max > 5){
$max_r = 5;
} else {
$max_r = $max;
}
Or, you could go one-liner FTW:
$max_r = ( ! $max || $max > 5) ? 5 : $max;
$max = intval($_POST['max']);
if($max < 0 || $max > 5){
$max = 5;
}

Allow only two digits max in GET?

How do I check if there are only two digits maximum in if(++$i > $_GET['number']) break; ?
use :
$number = intval($_GET['number']) ;
if( number >= 0 && <= 99 )
{
}
//OR
if( preg_match('/^[0-9]{2}$/',$_GET['number'] )
{
}
if (strlen($_GET['number']) > 2)
{
...
}

Categories