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.
Related
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.
I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.
Why aren't the last two cases of my if statement ever matched?
$atype = strtolower($userData['user_type']); // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" ||
(preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
$atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||
(preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
$atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" ||
(preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
$atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" ||
(preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
$atype = 4;
}
echo $atype;
You need to explain your question in a better way.
But i guess as you say the data assumed is company introducer.
So it already matches condition for the first if block.
For ex:
In company introducer
The preg_match will return true.
if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ){
$atype =1 ;
}
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;
}
My charity does "Poker Runs" motorcycle runs where player go to each stop and pick a card. We are looking to make this an easier way to track the winners without having to manually sort through each card.
I believe I have all other functions done, I just am unsure how to check for the full house with the method I am using. And also how to score just for a high card hand.
<?php
$card_one_suit = $_POST['card_one_suit'];
$card_two_suit = $_POST['card_two_suit'];
$card_three_suit = $_POST['card_three_suit'];
$card_four_suit = $_POST['card_four_suit'];
$card_five_suit = $_POST['card_five_suit'];
$card_one = $_POST['card_one'];
$card_two = $_POST['card_two'];
$card_three = $_POST['card_three'];
$card_four = $_POST['card_four'];
$card_five = $_POST['card_five'];
$player = $_POST['name'];
$total_card_amount = $card_one + $card_two + $card_three + $card_four + $card_five;
$card_list = array();
array_push($card_list, $card_one, $card_two, $card_three, $card_four, $card_five);
$card_suits = array();
array_push($card_suits, $card_one_suit, $card_two_suit, $card_three_suit, $card_four_suit, $card_five_suit);
$rank = 0;
foreach($card_list as $card)
{
$count_values[$card]++;
}
foreach($card_suits as $cards)
{
$count_suit_values[$cards]++;
}
//echo "$count_suit_values[$card_one_suit]";
//print_r($count_suit_values);
// ROYAL FLUSH
if(($total_card_amount == "60") && ($count_suit_values[$card_one_suit] == 5))
{
$rank = 1;
echo "ROYAL FLUSH";
}
// STRAIGHT FLUSH
else if (($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40") &&
($count_suit_values[$card_one_suit] == 5))
{
$rank = 2;
echo "STRAIGHT FLUSH";
}
// FOUR OF A KIND
else if ($count_values[$card_one] == 4 || $count_values[$card_two] == 4 || $count_values[$card_three] == 4)
{
$rank = 3;
echo "FOUR OF A KIND";
}
// FULL HOUSE
// HOW TO FIGURE THIS OUT?
// FLUSH
else if ($count_suit_values[$card_one_suit] == 5 || $count_suit_values[$card_two_suit] == 5 || $count_suit_values[$card_three_suit] == 5)
{
$rank = 5;
echo "FLUSH";
}
// STRAIGHT
else if ($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40")
{
$rank = 6;
echo "STRAIGHT";
}
// THREE OF A KIND
else if ($count_values[$card_one] == 3 || $count_values[$card_two] == 3 || $count_values[$card_three] == 3 || $count_values[$card_four] == 3)
{
$rank = 7;
echo "THREE OF A KIND";
}
// TWO PAIR
else if ($count_values[$card_one] == 2 && $count_values[$card_two] == 2 || $count_values[$card_one] == 2 && $count_values[$card_three] == 2
|| $count_values[$card_one] == 2 && $count_values[$card_five] == 2 || $count_values[$card_two] == 2 && $count_values[$card_three] == 2)
{
$rank = 8;
echo "TWO PAIR";
}
// ONE PAIR
else if ($count_values[$card_one] == 2 || $count_values[$card_two] == 2 || $count_values[$card_three] == 2 || $count_values[$card_four] == 2)
{
$rank = 9;
echo "ONE PAIR";
}
// HIGH CARD
else
{
$rank = 10;
echo "NO MATCHES. DETERMINE HIGH CARD";
}
?>
For the hand to be a full house, the array count_values must contain two entries, one of those entries must have a value of 3.
else if (count($count_values) == 2 && (array_values($count_values)[0] == 3 || array_values($count_values)[1] == 3)) {
echo 'FULL HOUSE';
}
I'm creating a custom search form and when I try and sort the results I get all the objects displayed instead of the matched criteria. The reason I discovered was that some of the inputs from the form don't have a default value and when this is not declared in the conditional statement later on (for sorting) it just shows all the objects, whether the other requirements are met or not. I tried applying an OR statement with the specific variables able to be empty, but it gave the same result. Like so -
<?php if ($bedrooms >= $min_rooms
&& $bedrooms <= $max_rooms
&& $space >= $min_space
&& $space <= $max_space
&& $price >= $min_price
&& $price <= $max_price
&& $sel_type == $type
|| $sel_type == ''
&& $country == $sel_country
|| $sel_country == '' ) { ?>
(See the last two statements)
I was thinking of checking each variable in the conditional statement before including it but it feels like unnecessary code. How would you do it?
The && operator has a higher precedence than the || operator, so your expression is currently grouped like this, probably not what you want:
($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && $sel_type == $type)
||
($sel_type == '' && $country == $sel_country)
||
($sel_country == '' )
Try adding parentheses like this to achieve correct grouping:
($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '') )
Your expression may fail as the && operator has a higher precedence than the || operation. That means an expression like this:
… && $sel_type == $type || $sel_type == ''
is equivalent to this (operator precedence highlighted by using parentheses):
(… && $sel_type == $type) || $sel_type == ''
To fix that put the || expressions in parentheses:
$bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')
Additionally, your expression is probably easier to read and to maintain if you use some helper functions like a between function:
function between($val, $min, $max) {
return $min <= $val && $val <= $max;
}
Then your expression reads:
between($bedrooms, $min_rooms, $max_rooms) && between($space, $min_space, $max_space) && between($price, $min_price, $max_price) && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')