I was just working on a customer project and I am wondering if someone know how to make this code more compacter:
$box_size_unknown =
$cart_item['f_box_L'] <= 0 || !$cart_item['f_box_L']
||
$cart_item['f_box_H'] <= 0 || !$cart_item['f_box_H']
||
$cart_item['f_box_D'] <= 0 || !$cart_item['f_box_D'];
I'm a new member. Sorry if my answer is incomplete or wrong.
<?php
function cars($ic){
$cart_item = array('f_box_L','f_box_H','f_box_D');
$cll = $cart_item[0];
$clh = $cart_item[1];
$cld = $cart_item[2];
$box_size_unknown = $cll <= 0 || !$cll || $clh <= 0 || !$clh ||
$cld <= 0 || !$cld;
$ic = $box_size_unknown;
echo $ic;
}
echo cars(0);
?>
Use the function I want to describe.
Related
I have the following code and it's giving me hell of a time as to why the variables inside the for ($x = 1; $x <= $quantity; $x++) loop are not returning anything but null when called in the database insert query that's following it, or when I try to debug with var_dump($cVnum); for instance.
Here is the code:
if ($cash >= $product['cost'] * $quantity) {
// Substract cash
$receiver = $userData['login'];
if ($receiver != "") {
//all variables safe
$database->setDB("account")->mkquery("UPDATE {{table}} SET ".$cashfield." = (".$cashfield." - ".$product['cost'] * $quantity.") WHERE id = '".$accountid."' LIMIT 1", "account");
// For each quantity
for ($x = 1; $x <= $quantity; $x++) {
// Insert vnums to item_award
for ($i = 1; $i <= 4; $i++) {
$cVnum = $product['vnum'.$i];
if ($cVnum > 0) {
$socket0 = 0;
$socket2 = 0;
if ($product['vnum'.$i.'_time'] > 0) {
if ($cVnum == 72701 || ($cVnum > 71069 && $cVnum < 71075) || ($cVnum > 72722 && $cVnum < 72731)) {
$socket2 = $product['vnum'.$i.'_time'];
} else if ($cVnum == 47001 ||
($cVnum > 41136 && $cVnum < 41145) ||
($cVnum > 45078 && $cVnum < 45084) ||
($cVnum > 71164 && $cVnum < 71168) ||
($cVnum >= 45139 && $cVnum <= 45144) ||
($cVnum >= 41311 && $cVnum <= 41314)) {
$socket0 = time() + ($product['vnum'.$i.'_time']*60*60*24);
} else {
$socket2 = time() + ($product['vnum'.$i.'_time']*60*60*24);
}
}
}
}
}
$database->setDB("player");
$insert = [
"login" => $receiver,
"vnum" => $cVnum,
"count" => $quantity,
"given_time" => array("func", "NOW()"),
"socket0" => 22,
"mall" => 1
];
$logok = $database->insert($insert, "item_award");
}
}
I've been at it for like six hours now and can't figure out what's wrong.
UPDATE: I narrowed down where the problem is with $cVnum = $product['vnum'.$i];. I noticed that when I removed $i and it became $product['vnum'] the variable returned the value I expected. Could there be a fix so that I can use with the $i variable though? Why is it returning null when $i variable is included?
With String Operators you have to use double quotes if you want PHP parse your variable : $cVnum = $product["vnum".$i];
I want to create 8 random integers, that are unique and in the range between 0 and 99.
Is there a neater solution, than the one that I thought of?
<?php
do {
$rnd1 = rand(0,99);
$rnd2 = rand(0,99);
$rnd3 = rand(0,99);
$rnd4 = rand(0,99);
$rnd5 = rand(0,99);
$rnd6 = rand(0,99);
} while (($rnd1 == $rnd2) ||
($rnd1 == $rnd3) ||
($rnd1 == $rnd4) ||
($rnd1 == $rnd5) ||
($rnd1 == $rnd6) ||
($rnd2 == $rnd3) ||
($rnd2 == $rnd4) ||
($rnd2 == $rnd5) ||
($rnd2 == $rnd6) ||
($rnd3 == $rnd4) ||
($rnd3 == $rnd5) ||
($rnd3 == $rnd6) ||
($rnd4 == $rnd5) ||
($rnd4 == $rnd6) ||
($rnd5 == $rnd6) );
?>
Try the code below.
It will create an array containing 8 random values between 0 and 99 while checking if they have been added already to ensure they are unique across.
$numbers = array();
while ( count($numbers) <= 8 ) {
$x = mt_rand(0,99);
if ( !in_array($x,$numbers) ) {
$numbers[] = $x;
}
}
print_r($numbers);
Also see these two Q&A's about using mt_rand vs. rand
Difference between mt_rand() and rand()
What's the disadvantage of mt_rand?
This is an optimized version that relies on the array indexes. The complexity of this solution is linear rather than n2 as in #AlexAndrei's answer. It relies on using array indexes as the guarantee there are no duplicates so there is no need to add the overhead of in_array.
$random_numbers = [];
do {
$rand = rand(0, 99);
$random_numbers[$rand] = $rand;
} while( count($random_numbers) < 9 );
$random_numbers = array_values($random_numbers); // This strips the keys
print_r($random_numbers);
If you are using a foreach you can even drop the array_values call which would also increase the speed of your code, but the numbers in the array will be in the form: [ a => a, b => b, ... ]
$rnds = [];
do {
$newRnd = rand(0, 99);
$rnds[] = $newRnd;
} while(in_array($newRnd, $rnds) && count($rnds) < 9);
print_r($rnds);
I've this situation in code where i think the code is unnecessary complex and i believe i can refactor it to make it more easier to understand and read.
So i googled about it and found decompose conditional refactoring, but i'm still in doubt how to do refactoring
if(count($bagTypes) == 1 && (array_key_exists('type1', $bagTypes)
|| array_key_exists('type2', $bagTypes)
|| array_key_exists('type3', $bagTypes))){
$flag = 1;
}
if(count($bagTypes) == 2 && (
(array_key_exists('type1', $bagTypes) && array_key_exists('type2', $bagTypes)) ||
(array_key_exists('type1', $bagTypes) && array_key_exists('type3', $bagTypes)) ||
(array_key_exists('type2', $bagTypes) && array_key_exists('type3', $bagTypes)))
){
$flag = 1;
}
Is there any better way of doing this?
You could try something like this:
$arrayKeys = array(
'type1',
'type2',
'type3'
);
$bagTypesKeys = array_keys($bagTypes);
if ((count($bagTypes) == 1 && count(array_diff($arrayKeys, $bagTypesKeys)) < 3)
|| (count($bagTypes) == 2 && count(array_diff($arrayKeys, $bagTypesKeys)) < 2))
{
$flag = 1;
}
Okay I have 15 variables $A - $O. I need a different way to compare the variables as I think I am doing this a really long way. It works the way I want it to but I'm not happy with the amount of code.
So what my code is doing it's generating a random number for each of the variables. I have it doing this while they equal each other, so in other words it will stop generating random number when each of the variables is different here is my code... Thanks in advance.
do{
$a = rand($min,$max);
$b = rand($min,$max);
$c = rand($min,$max);
$d = rand($min,$max);
$e = rand($min,$max);
$f = rand($min,$max);
$g = rand($min,$max);
$h = rand($min,$max);
$i = rand($min,$max);
$j = rand($min,$max);
$k = rand($min,$max);
$l = rand($min,$max);
$m = rand($min,$max);
$n = rand($min,$max);
$o = rand($min,$max);
}
while ($a==$b || $a==$c || $a==$d || $a==$e || $a==$f || $a==$g || $a==$h || $a==$i || $a==$j || $a==$k || $a==$l || $a==$m || $a==$n || $a==$o
|| $b==$a || $b==$c || $b==$d || $b==$e || $b==$f || $b==$g || $b==$h || $b==$i || $b==$j || $b==$k || $b==$l || $b==$m || $b==$n || $b==$o
|| $c==$a || $c==$b || $c==$d || $c==$e || $c==$f || $c==$g || $c==$h || $c==$i || $c==$j || $c==$k || $c==$l || $c==$m || $c==$n || $c==$o
|| $d==$a || $d==$b || $d==$c || $d==$e || $d==$f || $d==$g || $d==$h || $d==$i || $d==$j || $d==$k || $d==$l || $d==$m || $d==$n || $d==$o
|| $e==$a || $e==$b || $e==$c || $e==$d || $e==$f || $e==$g || $e==$h || $e==$i || $e==$j || $e==$k || $e==$l || $e==$m || $e==$n || $e==$o
|| $f==$a || $f==$b || $f==$c || $f==$d || $f==$e || $f==$g || $f==$h || $f==$i || $f==$j || $f==$k || $f==$l || $f==$m || $f==$n || $f==$o
|| $g==$a || $g==$b || $g==$c || $g==$d || $g==$e || $g==$f || $g==$h || $g==$i || $g==$j || $g==$k || $g==$l || $g==$m || $g==$n || $g==$o
|| $h==$a || $h==$b || $h==$c || $h==$d || $h==$e || $h==$f || $h==$g || $h==$i || $h==$j || $h==$k || $h==$l || $h==$m || $h==$n || $h==$o
|| $i==$a || $i==$b || $i==$c || $i==$d || $i==$e || $i==$f || $i==$g || $i==$h || $i==$j || $i==$k || $i==$l || $i==$m || $i==$n || $i==$o
|| $j==$a || $j==$b || $j==$c || $j==$d || $j==$e || $j==$f || $j==$g || $j==$h || $j==$i || $j==$k || $j==$l || $j==$m || $j==$n || $j==$o
|| $k==$a || $k==$b || $k==$c || $k==$d || $k==$e || $k==$f || $k==$g || $k==$h || $k==$i || $k==$j || $k==$l || $k==$m || $k==$n || $k==$o
|| $l==$a || $l==$b || $l==$c || $l==$d || $l==$e || $l==$f || $l==$g || $l==$h || $l==$i || $l==$j || $l==$k || $l==$m || $l==$n || $l==$o
|| $m==$a || $m==$b || $m==$c || $m==$d || $m==$e || $m==$f || $m==$g || $m==$h || $m==$i || $m==$j || $m==$k || $m==$l || $m==$n || $m==$o
|| $n==$a || $n==$b || $n==$c || $n==$d || $n==$e || $n==$f || $n==$g || $n==$h || $n==$i || $n==$j || $n==$k || $n==$l || $n==$m || $n==$o
|| $o==$a || $o==$b || $o==$c || $o==$d || $o==$e || $o==$f || $o==$g || $o==$h || $o==$i || $o==$j || $o==$k || $o==$l || $o==$m || $o==$n
);
You could do this with an array:
$randoms = array();
while(count($randoms) < 15)
{
$randoms[] = mt_rand($min, $max);//add random
$randoms = array_unique($randoms);//remove duplicates
}
$randoms = array_combine(range('a', 'o'), $randoms);//set keys a to o
And if you really, Really want those values as separate variables, you can do this using variable variables:
foreach ($randoms as $letter => $val)
{
$$letter = $val;
}
Because I wrote $$letter (note two $ signs), this expression evaluates to $<value of $letter> or $a, $b and so on, to which I assign the corresponding value
I suppose, if you want to get the best performance for the least amount of code/overhead, this would be the way to get it:
$random = array();
$count = 0;
$rand = null;
while($count < 15)
{
$rand = mt_rand($min, $max);
if (!isset($random[$rand]))
{//assign random value, and use keys for faster lookup
++$count;
$random[$rand] = $rand;
}
}
var_dump($random);//guaranteed to hold 15 random values, both the keys and values
//so:
$vals = array_values($random);//or array_keys($random)
And again, to turn this array into a distinct variables:
$arr = array_combine(range('a', 'o'), array_values($random));
//or even array_combine(range('a', 'o'), $random);
foreach ($arr as $name => $val)
$$name = $val;
try this
$min = 0;
$min = 999;
while (count($array) < 16){
$val = rand($min,$max);
if (!in_array($val, $array)) {
$array[] = $val;
}
}
The easiest way is to add the variables to an array and then perform your do-while as such:
$arr = array(&$a, &$b, &$c, &$d);
do {
foreach ($arr as $i => $k)
$arr[$i] = rand($min, $max);
}
while (count($arr) == array_unique($arr));
But in your example you are re-doing a lot of work that is not needed, e.g do not re-random unique values. Just a tip.
In PHP you can use variables to access other variables by names in strings.
For instance you can do something like that in your case:
$names = array('a','b','c','d'); // And so on - it can be done easier with casting to char
function check_names_equal($names) {
foreach($names as $name) {
global $$name;
}
foreach($names as $name) {
foreach($names as $name2) {
if ($name != $name2 && $$name == $$name2) {
return true;
}
}
}
return false;
}
while(check_names_equal($names)); // <- this is your shortened while
My comment to that - I know it's not the perfect way to that and I would suggest that you use arrays for holding variables, don't store some random data in single variables.
Your start of the code should like like this prefferably: (it would better to use numbers as indexes too, so you can use loops)
$arr = array();
$arr['a'] = rand($min,$max);
$arr['b'] = rand($min,$max);
$arr['c'] = rand($min,$max);
$arr['d'] = rand($min,$max);
$arr['e'] = rand($min,$max);
$arr['f'] = rand($min,$max);
$arr['g'] = rand($min,$max);
$arr['h'] = rand($min,$max);
$arr['i'] = rand($min,$max);
$arr['j'] = rand($min,$max);
$arr['k'] = rand($min,$max);
$arr['l'] = rand($min,$max);
$arr['m'] = rand($min,$max);
$arr['n'] = rand($min,$max);
$arr['o'] = rand($min,$max);
And with numeric indexing:
$arr=array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min,$max);
}
Ahother option is to create an array and go growing it with random values comparing every new vaue with the existing ones:
$myvars=array();
while (sizeof($myvars)<15)
{
$success=false;
do{
$candidate=rand($min,$max);
//compare the candidate with every element in array
if (!in_array($candidate, $myvars))
$success=true; //if found no success
} while (!$success);
$myvars[]=$candidate;
}
Now in $myvars you have 15 different values.
I need help on a if statement here is what is going on.
I have a value being pulled $paint['product_id']
I need to say if that value is between
81501 - 81599 or
81701 - 81799
say blah
else if that value is between
81001 - 81099 or
81301 - 81399
say blah2
else if
86501 - 86599 or
86001 - 86099 or
85001 - 85099
say blah3
and say nothing if it does not apply.
What id did try
<? if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799):?>
blah
<? elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399):?>
blah2
<? elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099):?>
blah3
<? endif;?>
The problem I am having is "blah" is showing up on items in the blah3 category.
Hope that makes sense and thanks in advance for any help!
Replace $x with $paint['product_id'].
You should group them with brackets:
if( ($x > 10 && $x < 20) || ($x > 40 && $x < 50) ) { ...
Consider creating a user defined function, e.g.
function between($x, $lim1, $lim2) {
if ($lim1 < $lim2) {
$lower = $lim1; $upper = $lim2;
}
else {
$lower = $lim2; $upper = $lim1;
}
return (($x >= $lower) && ($x <= $upper));
}
Then the rest of your code becomes much more legible:
if between($paint['product_id'], 81501, 81599) blah;
As given, the "between" function will work even if you don't know ahead of time whether the first or the second argument is larger.
You need more parenthesis
<?php
<? if (($paint['product_id'] >= 81501 && $x <= 81599) || ($paint['product_id'] >= 81701 && $x <= 81799)):?>
blah
<? elseif (($paint['product_id'] >= 81001 && $x <= 81099) || ($paint['product_id'] >= 81301 && $x <= 81399)):?>
blah2
<? elseif (($paint['product_id'] >= 86501 && $x <= 86599) || ($paint['product_id'] >= 86001 && $x <= 86099) || ($paint['product_id'] >= 85001 && $x <= 85099)):?>
blah3
<? endif;?>
there are at least 3 ways to solve it.
First solution has been already posted by users
Second solution is to create between function and use it.
function between($number, $from, $to)
{
return $number>$from && $number<$to;
}
if(between($paint['product_id'], 81501, 81599) || $paint['product_id'], 81701, 81799))
echo 'blah';
else if(between($paint['product_id'], 81001, 81099) || $paint['product_id'], 81301, 81399))
echo 'blah2';
else if(between($paint['product_id'], 86501, 86599) || $paint['product_id'], 86001, 86099) || $paint['product_id'], 85001, 85099))
echo 'blah3';
else echo 'it does not apply';
Third solution is to use range() function and in_array() function
example if(in_array($paint['product_id'], range(81501, 81599)))
rest goes the same
if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799){
echo "blah";
}
elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399){
echo "blah2";
}
elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099){
echo "blah2";
}
Hellow i'm going to desribe how to use if condition between two numbers in PHP in easy steps.
<?php
$a= 65;
$b= 9;
if($a<$b)
{
echo "$a this is correct";
}
else{
echo "$b this is greater than $a";
}
?>