php search within string for word? - php

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 ;
}

Related

Php validation on multiple numeric fields for multiple checks

I am trying to validate multiple numeric fields for multiple checks ie. There are 6 numeric field and I want to run 6 checks.
Firstly, total less than 0 and more then 60 give expected results, but
Failing checks and not behaving as expected for the checks below:
a. Integer
b. Numeric
c. Negative
d. Not Float
$seatsSTA = $_POST['seats']['STA'];
$seatsSTP = $_POST['seats']['STP'];
$seatsSTC = $_POST['seats']['STC'];
$seatsFCA = $_POST['seats']['FCA'];
$seatsFCP = $_POST['seats']['FCP'];
$seatsFCC = $_POST['seats']['FCC'];
$minSeats = 1; $maxSeats = 60;
$totalSeats= ((int)$seatsSTA + (int)$seatsSTP + (int)$seatsSTC + (int)$seatsSTP + (int)$seatsFCA + (int)$seatsFCP + (int)$seatsFCC );
if ( $totalSeats < $minSeats ) {
$errorsBook['user']['seats'] = "Atleast one session must be selected. ";
// header("Location: index.php");
} elseif ( $totalSeats > $maxSeats ) {
$errorsBook['user']['seats'] = "Too many seats selected. ";
// header("Location: index.php");
} elseif ( !is_int($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "Please correct seat selection. ";
header("Location: index.php");
}
elseif ( !is_numeric($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "NOT numeric character. ";
header("Location: index.php");
}
elseif (($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) < 0 ){
// $errorsBook['user']['seats'] = "Negative number not allowed. ";
header("Location: index.php");
}
elseif ( is_float($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "Please correct seat selection. ";
header("Location: index.php");
}
return $errorsBook;
}

How to do decompose conditional refactoring?

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;
}

Calculate Poker Hand

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 need a shorter way to compare variables?

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.

Set one variable to true based on conditions [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I set one variable to true based on other conditions. Here, instead of doing
if ($vara && $varb && $varc)
I'm something like below. Problem is, I'm just not getting something right. Can you please help me?
<?php
$onward = false;
$vara = 11;
$varb = 21;
$varc = 3;
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2)
{$onward = true;}else{$onward = false;}
if ($varc == 3)
{$onward = true;}else{$onward = false;}
if ($onward)
{
echo "Ok";
}else {echo "Not ok";}
?>
Each of your conditions ignores the result of the previous condition. You need to include the previous state of $onward in each subsequent test:
if ($vara == 1)
{$onward = true;}else{$onward = false;}
if ($varb == 2 && $onward)
{$onward = true;}else{$onward = false;}
if ($varc == 3 && $onward)
{$onward = true;}else{$onward = false;}
This way, varb and varc are only tested if $onward is still true after the previous test.
This is a particularly ugly way of writing code. If you have three large conditions and you don't simply want to join them on one line as in your $vara && $varb && $varc, you should be writing it this way:
$onward = ($vara == 1)
$onward = $onward && ($varb == 2);
$onward = $onward && ($varb == 3);
Any time you're simply returning/setting something to true/false in the branches if your if statement, you should just be returning/setting the condition itself.
That is, this:
if (condition) {
return true;
} else {
return false;
}
should always be written:
return condition;
The problem is, you are overwriting the value of $onward in every if-statement. Just use
$onward = $vara == 1 && $varb == 2 && $varc == 3;
I think you are looking for:
$onward = $vara == 1 || $varb == 2 || $varc ==3;
or
$onward = $vara == 1 && $varb == 2 && $varc == 3;
depending on your goal.
With this:
if ($varc == 3)
{$onward = true;}else{$onward = false;}
$onward will always be false if $varc is not 3.
I'm sure you mean
if (($vara == 1) && ($varb == 2) && ($varc == 3))
{$onward = true;}else{$onward = false;}
or even
$onward = (($vara == 1) && ($varb == 2) && ($varc == 3));
A better way of doing this:
if ($vara == 1) {$onward = true;} else {$onward = false;}
is this:
$onward = ($vara == 1);
You're overwriting the same variable each time, so the first two conditions are actually pointless.
I don't know why you'd want to do this over the first, more succinct approach, which will have the same logical effect:
if ($varc ==== 1 || $varc === 2 || $varc === 3)
$onward = true;
else
$onward = false;
Or even just:
$onward = $varc === 1 || $varc === 2 || $varc === 3;
Beware also of ever doing == 1. In comparisons, data are coerced to their truthy/falsy equivalents, so any truthy value will resolve to true in the comparison == 1. To test that something is literally 1, use ===. (This is a good rule generally unless you know you explicitly want to test for value and not type.)

Categories