How can I convert following long if-else condition into switch case in PHP? Or, please suggest any feasible solution besides switch case.
if($total_products >= 1 && $total_products <=10 ){
} elseif ($total_products >= 11 && $total_products <=25 ){
} elseif ($total_products >= 26 && $total_products <=40 ){
} elseif ($total_products >= 181 && $total_products <=200 ){
} elseif ($total_products >= 351 && $total_products <=400 ){
} elseif ($total_products >= 401 && $total_products <=500 ){
} elseif ($total_products > 500 ){
}
You can make a small function to do the job and can use it like this:
pubic function isInRange($value, $min, $max){
return ($value <= $max && $value >= $min);
}
you can use this function like this:
$value = 16;
$ranges = array(
'range1'=> array(
'min'=> 1,
'max'=> 10
),
'range2'=> array(
'min'=> 11,
'max'=> 20
)
);
function isInRange($value, $min, $max){
return ($value <= $max && $value >= $min);
}
foreach($ranges as $key => $range){
echo (isInRange($value, $range['min'], $range['max']))? $key.' returned true': $key.' returned false';
}
An alternative option is to use the filter_var method:
filter_var(
$yourInteger,
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => $min,
'max_range' => $max
)
)
);
Please, note that the filter_var method is type safe.
You will see many good working examples at the bottom of the php.net site i linked above.
Related
I have a list containing a bunch of values from 1-400. I am trying to divide the data into ranges like [1-50], [51-100], .. , [351-400] and get the count for the values that falls within the range given . I basically have the code working. So, my question would be is there a better way to do this or what would be a good practise for this?
$temp = array(); //temp array to store the values from mysql
$final_array = array //final array to store the counts from the range initialized to 0
(
"0" => 0,
"1-50" => 0,
"51-100" => 0,
"101-150" => 0,
"151-200" => 0,
"201-250" => 0,
"251-300" => 0,
"301-350" => 0,
"351-400" => 0
);
$sql = "SELECT count(*) AS total FROM user GROUP BY user_id";
$statement = $DB_->link->prepare ( $sql );
$statement->execute ();
if ($result = $statement->get_result ())
{
while ( $row = $result ->fetch_assoc() )
{
$temp [] = $row;
}
}
else
{
die(mysql_error());
}
foreach ($temp as $child)
{
if( $child['total'] >= 351 && $child['total'] <= 400)
{
$final['351-400']++;
}
elseif( $child['total'] >= 301 && $child['total'] <= 350)
{
$final['301-350']++;
}
...
elseif( $child['total'] >= 1 && $child['total'] <= 50)
{
$final['1-50']++;
}
}
Desired results
Array
(
[0] => 0
[1-50] => 1
[51-100] => 0
[101-150] => 0
[151-200] => 1
[201-250] => 0
[251-300] => 4
[301-350] => 5
[351-400] => 18
)
I would iterate over the keys of final_array, exploding them using - as the delimiter. While this method is slower than what you have, as it's iterating over final_array for each row returned from the database, it is much more maintainable, as it seamlessly handles keys for exact matches (only 1 number), and arbitrary ranges. Adding more buckets simply requires editing the final_array array, rather than changing a bunch of lines of code.
foreach ($temp as $child) {
foreach($final_array as $key => $value) {
if (strpos($key, '-') === false) {
if ($child['total'] == intval($key)) {
$final_array[$key]++;
}
} else {
list($min, $max) = explode('-', $key);
if ($child['total'] >= intval($min) && $child['total'] <= intval($max)) {
$final_array[$key]++;
}
}
}
}
I would also forgo the use of the $temp array, simply processing the results as they are returned:
if ($result = $statement->get_result ())
{
while ( $child = $result->fetch_assoc() ) {
foreach($final_array as $key => $value) {
if (strpos($key, '-') === false) {
if ($child['total'] == intval($key)) {
$final_array[$key]++;
}
} else {
list($min, $max) = explode('-', $key);
if ($child['total'] >= intval($min) && $child['total'] <= intval($max)) {
$final_array[$key]++;
}
}
}
}
}
else
{
die(mysql_error());
}
The most efficient system would load all the results in an array, pass that array through array_count_values, and then aggregate those.
This is the best way which you used & for single key count we use PHP function array_count_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";
}
I have the following function that works properly, except for when you use the input value of 0. I tried searching around to see if the 0 is equated as a NULL or if I'm doing something wrong.
When a zero is input, it outputs advanced which is greater than 20. Can anyone explain? Thanks
I plan on making the switch equate 0-10, 11-20, 21-30, 31-40, 41+ but for this example i am just using two scenarios. Thanks
**EDIT I do want values when its 20 :)
function strengthPlan( $data ) {
$data = ( int ) $data;
switch( $data ) {
case( $data <= 19 ):
$result = 'standard strength';
break;
case( $data >= 20 ):
$result = 'advanced strength';
break;
}
return $result;
}
echo strengthPlan( 0 );
Your logic is incorrect. Switch statements are checking for equality. Your code is checking whether $data is equal to TRUE or FALSE.
case( $data < 20 ):
will evaluate to:
case( TRUE ):
because 0 < 20.
Since 0 is not equal to TRUE but to FALSE (after conversion), the second case is run.
Basically, you cannot use switch case for < or > but only for ==.
That's not how switch statements. They compare the case to the value you provide to switch. Here's what you should do instead:
function strengthPlan($length) {
return $length >= 20 ? 'advanced strength' : 'standard strength';
}
If you're planning on using more conditions, then use an if...elseif statement as follows:
function strengthPlan($length) {
if ($length < 5) return 'micro strength';
elseif ($length < 10) return 'tiny strength';
elseif ($length < 15) return 'small strength';
elseif ($length < 20) return 'standard strength';
elseif ($length < 30) return 'advanced strength';
else return 'super strength!!!!!';
}
It will trickle down each condition until it hits a number that it is within. Alternatively, you can use sort of a lookup table style like this:
function strengthPlan($length) {
$plans = array(
1 => 'super strength!!!!!',
$length < 30 => 'advanced strength',
$length < 20 => 'standard strength',
$length < 15 => 'small strength',
$length < 10 => 'tiny strength',
$length < 5 => 'micro strength',
);
return $plans[1];
}
There was a discussion about this here: http://forums.devnetwork.net/viewtopic.php?f=50&t=113253
Using switch in this case is wrong
Use this one
http://sandbox.phpcode.eu/g/b4053.php
<?php
function strengthPlan( $data ) {
return ($data > 20) ? 'advanced strenght' : 'standard strenght';
}
echo strengthPlan( 0 );
How can I determine using PHP code that, for example, I have a variable that has a value
between 1 and 10, or
between 20 and 40?
if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))
Do you mean like:
$val1 = rand( 1, 10 ); // gives one integer between 1 and 10
$val2 = rand( 20, 40 ) ; // gives one integer between 20 and 40
or perhaps:
$range = range( 1, 10 ); // gives array( 1, 2, ..., 10 );
$range2 = range( 20, 40 ); // gives array( 20, 21, ..., 40 );
or maybe:
$truth1 = $val >= 1 && $val <= 10; // true if 1 <= x <= 10
$truth2 = $val >= 20 && $val <= 40; // true if 20 <= x <= 40
suppose you wanted:
$in_range = ( $val > 1 && $val < 10 ) || ( $val > 20 && $val < 40 ); // true if 1 < x < 10 OR 20 < x < 40
You can do this:
if(in_array($value, range(1, 10)) || in_array($value, range(20, 40))) {
# enter code here
}
if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
// A value between 1 to 10, or 20 to 40.
}
Sorry for the late answer, but this function allow you to do that.
function int_between($value, $start, $end) {
return in_array($value, range($start, $end));
}
// Example
$value1 = 20;
$value2 = 40;
echo int_between(20, $value1, $value2) ? "true" : "false";
Guessing from the tag 'operand' you want to check a value?
$myValue = 5;
$minValue = 1;
$maxValue = 10;
if ($myValue >= $minValue && $myValue <= $maxValue) {
//do something
}
If you just want to check the value is in Range, use this:
MIN_VALUE = 1;
MAX_VALUE = 100;
$customValue = min(MAX_VALUE,max(MIN_VALUE,$customValue)));
Try This
if (($val >= 1 && $val <= 10) || ($val >= 20 && $val <= 40))
This will return the value between 1 to 10 & 20 to 40.
How can I check if a given number is within a range of numbers?
The expression:
($min <= $value) && ($value <= $max)
will be true if $value is between $min and $max, inclusively
See the PHP docs for more on comparison operators
You can use filter_var
filter_var(
$yourInteger,
FILTER_VALIDATE_INT,
array(
'options' => array(
'min_range' => $min,
'max_range' => $max
)
)
);
This will also allow you to specify whether you want to allow octal and hex notation of integers. Note that the function is type-safe. 5.5 is not an integer but a float and will not validate.
Detailed tutorial about filtering data with PHP:
https://phpro.org/tutorials/Filtering-Data-with-PHP.html
Might help:
if ( in_array(2, range(1,7)) ) {
echo 'Number 2 is in range 1-7';
}
http://php.net/manual/en/function.range.php
You could whip up a little helper function to do this:
/**
* Determines if $number is between $min and $max
*
* #param integer $number The number to test
* #param integer $min The minimum value in the range
* #param integer $max The maximum value in the range
* #param boolean $inclusive Whether the range should be inclusive or not
* #return boolean Whether the number was in the range
*/
function in_range($number, $min, $max, $inclusive = FALSE)
{
if (is_int($number) && is_int($min) && is_int($max))
{
return $inclusive
? ($number >= $min && $number <= $max)
: ($number > $min && $number < $max) ;
}
return FALSE;
}
And you would use it like so:
var_dump(in_range(5, 0, 10)); // TRUE
var_dump(in_range(1, 0, 1)); // FALSE
var_dump(in_range(1, 0, 1, TRUE)); // TRUE
var_dump(in_range(11, 0, 10, TRUE)); // FALSE
// etc...
if (($num >= $lower_boundary) && ($num <= $upper_boundary)) {
You may want to adjust the comparison operators if you want the boundary values not to be valid.
You can try the following one-statement:
if (($x-$min)*($x-$max) < 0)
or:
if (max(min($x, $max), $min) == $x)
Some other possibilities:
if (in_array($value, range($min, $max), true)) {
echo "You can be sure that $min <= $value <= $max";
}
Or:
if ($value === min(max($value, $min), $max)) {
echo "You can be sure that $min <= $value <= $max";
}
Actually this is what is use to cast a value which is out of the range to the closest end of it.
$value = min(max($value, $min), $max);
Example
/**
* This is un-sanitized user input.
*/
$posts_per_page = 999;
/**
* Sanitize $posts_per_page.
*/
$posts_per_page = min(max($posts_per_page, 5), 30);
/**
* Use.
*/
var_dump($posts_per_page); // Output: int(30)
using a switch case
switch ($num){
case ($num>= $value1 && $num<= $value2):
echo "within range 1";
break;
case ($num>= $value3 && $num<= $value4):
echo "within range 2";
break;
.
.
.
.
.
default: //default
echo "within no range";
break;
}
I've created a simple helper function.
if ( !function_exists('number_between') )
{
/**
* number_between
*
* #param {integer} $number
* #param {array} $range [min, max]
* #return {boolean}
*/
function number_between(
int $number,
array $range
){
if(
count($range) !== 2 ||
is_numeric($range[0]) === FALSE ||
is_numeric($range[1]) === FALSE
){
throw new \Exception("number_between second parameter must contain two numbers.", E_WARNING);
}
if(
in_array($number, range($range[0], $range[1]))
){
return TRUE;
}else{
return FALSE;
}
}
}
Another way to do this with simple if/else range. For ex:
$watermarkSize = 0;
if (($originalImageWidth >= 0) && ($originalImageWidth <= 640)) {
$watermarkSize = 10;
} else if (($originalImageWidth >= 641) && ($originalImageWidth <= 1024)) {
$watermarkSize = 25;
} else if (($originalImageWidth >= 1025) && ($originalImageWidth <= 2048)) {
$watermarkSize = 50;
} else if (($originalImageWidth >= 2049) && ($originalImageWidth <= 4096)) {
$watermarkSize = 100;
} else {
$watermarkSize = 200;
}
I created a function to check if times in an array overlap somehow:
/**
* Function to check if there are overlapping times in an array of \DateTime objects.
*
* #param $ranges
*
* #return \DateTime[]|bool
*/
public function timesOverlap($ranges) {
foreach ($ranges as $k1 => $t1) {
foreach ($ranges as $k2 => $t2) {
if ($k1 != $k2) {
/* #var \DateTime[] $t1 */
/* #var \DateTime[] $t2 */
$a = $t1[0]->getTimestamp();
$b = $t1[1]->getTimestamp();
$c = $t2[0]->getTimestamp();
$d = $t2[1]->getTimestamp();
if (($c >= $a && $c <= $b) || $d >= $a && $d <= $b) {
return true;
}
}
}
}
return false;
}
Here is my little contribution:
function inRange($number) {
$ranges = [0, 13, 17, 24, 34, 44, 54, 65, 200];
$n = count($ranges);
while($n--){
if( $number > $ranges[$n] )
return $ranges[$n]+1 .'-'. $ranges[$n + 1];
}
I have function for my case
Use:
echo checkRangeNumber(0);
echo checkRangeNumber(1);
echo checkRangeNumber(499);
echo checkRangeNumber(500);
echo checkRangeNumber(501);
echo checkRangeNumber(3001);
echo checkRangeNumber(999);
//return
0
1-500
1-500
1-500
501-1000
3000-3500
501-1000
function checkRangeNumber($number, $per_page = 500)
{
//$per_page = 500; // it's fixed number, but...
if ($number == 0) {
return "0";
}
$num_page = ceil($number / $per_page); // returns 65
$low_limit = ($num_page - 1) * $per_page + 1; // returns 32000
$up_limit = $num_page * $per_page; // returns 40
return "$low_limit-$up_limit";
}
function limit_range($num, $min, $max)
{
// Now limit it
return $num>$max?$max:$num<$min?$min:$num;
}
$min = 0; // Minimum number can be
$max = 4; // Maximum number can be
$num = 10; // Your number
// Number returned is limited to be minimum 0 and maximum 4
echo limit_range($num, $min, $max); // return 4
$num = 2;
echo limit_range($num, $min, $max); // return 2
$num = -1;
echo limit_range($num, $min, $max); // return 0
$ranges = [
1 => [
'min_range' => 0.01,
'max_range' => 199.99
],
2 => [
'min_range' => 200.00,
],
];
foreach($ranges as $value => $range){
if(filter_var($cartTotal, FILTER_VALIDATE_FLOAT, ['options' => $range])){
return $value;
}
}
Thank you so much and I got my answer by adding a break in the foreach loop and now it is working fine.
Here are the updated answer:
foreach ($this->crud->getDataAll('shipping_charges') as $ship) {
if ($weight >= $ship->low && $weight <= $ship->high) {
$val = $ship->amount;
break;
}
else
{
$val = 900;
}
}
echo $val ;