I have three condition/variable combination e.g. below called amounts:
$a = 15000; $b = 10000; $c = 5000;
or
$a = 10000; $b = 15000; $c = 0;
or
$a = 12000; $b = 0; $c = 15000;
etc.
At least each $a or $b or $c above is not 0 (zero).
If the amount of each not 0 (zero) it have its own associated array e.g. if $b == 0 then $b_array will not set/created, assume all is not 0(zero) then below arrays are created:
$a_array = array('id'=>1);
$b_array = array('id'=>2);
$c_array = array('id'=>3);
If $a / $b / $c is not 0 (zero) then if $b or $c not zero it needs to be linked to $a or $b (if not zero) as below:
if($a != 0 && $b != 0 && $c != 0){
$b_array['id_link'] = $a_array['id'];
$c_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b != 0 && $c == 0){
$b_array['id_link'] = $a_array['id'];
} elseif ($a != 0 && $b == 0 && $c != 0){
$c_array['id_link'] = $a_array['id'];
} elseif ($a == 0 && $b != 0 && $c != 0){
$c_array['id_link'] = $b_array['id'];
}
The result for conditional statement above seems correct as you can check at the php sandbox
Is there any better idea for the conditional code and is there a missing condition (error handling). Any idea or solutions is greatly appreciated. Thanks!!
If I understand your question correctly, instead of checking for every combination possible, just get the first non zero number and assign it's id as id_link to all of them like below:
<?php
$id_link = -1;
if($a !== 0){
$id_link = $a_array['id'];
}else if($b !== 0){
$id_link = $b_array['id'];
}else if($c !== 0){
$id_link = $c_array['id'];
}
$a_array['id_link'] = ($b_array['id_link'] = ($c_array['id_link'] = $id_link));
Online Demo
(The brackets in the expression are added just for readability since they are redundant in case of PHP)
Update:
If you wish to leave the array with it's corresponding variable without the id_link key, then you can just use another 3 if conditions to take care of it.
<?php
$id_link = -1;
if($a !== 0){
$id_link = $a_array['id'];
}else if($b !== 0){
$id_link = $b_array['id'];
}else if($c !== 0){
$id_link = $c_array['id'];
}
if($a !== 0){
$a_array['id_link'] = $id_link;
}
if($b !== 0){
$b_array['id_link'] = $id_link;
}
if($c !== 0){
$c_array['id_link'] = $id_link;
}
Online Demo
Related
I have a PHP fragment that have to be something like this:
$a = false;
if($something) $a = true;
elseif($xyz < 45) $a = true;
elseif($response % 5 == 0) $a = true;
elseif($etc >14) $a = true;
Then I was wondering if there is a way to write better this, like in RoR, that could be something like:
$a = true if($something or ($xyz < 45) or ($response % 5 == 0) or ($etc >14));
The goal is not to repeat the "$a = true" so many times...
Since you're using elseif, you can write it in one line like this:
$a = $something || $xyz < 45 || $response % 5 == 0 || $etc >14;
Any ideas how to shorten if statment in an elegant way.
My if statement:
if(getfoo1() == getfoo2() && getfoo2() == 1)
{
}
EDIT:
I'm looking for something like:
if(getfoo1() == getfoo2() ==1)
{
}
But I suppose we can't do this.
$a = getfoo1();
$b = getfoo2(); // less operations, while it not produces duplicate calls
if($a == $b && $b == 1){
// do something
}
$variable = ((getfoo1() == getfoo2() && getfoo2() == 1) ? $value1 : $value2);
More elegant, combined:
$a = getfoo1();
$b = getfoo2();
$variable = (($a == $b && $b == 1) ? $value1 : $value2);
Since we don't know the possible return values from the functions, if you assume they are integers then you can say:
$a = getfoo1();
$b = getfoo2();
if (($a * $b) === 1) { // strict equality for the win
echo 'hi';
}
The result would only be true iff both $a AND $b are 1.
Another way:
$both = array(getfoo1(), getfoo2());
// use array_diff_assoc so it checks multiple occurrences of the same value
$diffCount = count(array_diff_assoc($both, array(1, 1)));
if ($diffCount === 0) {
echo 'hi';
}
Since anyway getfoo2() == 1 must be true, a better approach is to first check whether getfoo2() is equal to 1. If it false no matter about 2nd condition. But If you first check getfoo1() == getfoo2() and and then check getfoo2() == 1 you have to check 2 conditions all the times.
Therefore go for
$a = getfoo1();
$b = getfoo2();
if($b == 1 && $a == $b)
{
// logiv
}
else
{
}
Try this.
$a = getfoo1();
$b = getfoo2();
if( intval($a && $b) === 1) {
echo 'hi';
}
I'm wanting to set the condition of a do-while loop with a variable. Here's my code...
$ans_type = mt_rand(1, 2);
if ($ans_type == 1){
$condition = '$work_b != $c';
$symbol = '=';
$final_note = '1';
} else {
$condition = '$work_b == $c';
$symbol = '≠';
$final_note = '2';
}
do{
$a = mt_rand(-25, 25);
$b = mt_rand(-25, 25);
$c = mt_rand(-25, 25);
$d = mt_rand(-25, 25);
if($op_1 == '–'){
$work_b = $b * -1;
} else {
$work_b = $b;
}
if($op_2 == '–'){
$work_d = $d * -1;
} else {
$work_d = $d;
}
} while ($a == 0 || $b == 0 || $c == 0 || $d == 0 || $condition);
Note the $condition variable that I want to put in the while() part of the loop. This produces an infinite loop though.
So, is there a way to use variables as conditions in loops?
You can use variables as conditions, however the reason your code produces an infinite loop is because you are not changing $condition within your while loop. Therefore, if $condition evaluates to true once, it will keep evaluating to true (as it never changes in your code).
What you're trying to do can be better achieved by using normal variables:
if( blah ) {
$conditionstate = false;
} else {
$conditionstate = true;
}
...
} while( ... || ($work_b == $c) == $conditionstate );
If you have more varied conditions, maybe a restructure is in order. If there really is no way to restructure it, I'm hesitant to suggest it, because so many people misuse it to terrible consequences, but eval does what you're looking for and can be safe (if not fast) if used carefully. Needing to use it is usually a sign that your program has a bad structure though.
p.s. These types of random number generation problems are much better solved with code like this:
$a = mt_rand(-25, 24);
if( $a >= 0 ) {
++ $a;
}
// $a is -25 to 25, but never 0
$b = mt_rand(-25, 23);
if( $b >= min( $a, 0 ) ) {
++ $b;
}
if( $b >= max( $a, 0 ) ) {
++ $b;
}
// $b is -25 to 25, but never 0 or a
That can be made more elegant, but you get the idea. No need to loop at all, and guaranteed to halt.
$ans_type = mt_rand(1, 2);
if ($ans_type == 1){
$condition = ($work_b != $c);
$symbol = '=';
$final_note = '1';
} else {
$condition = ($work_b == $c);
$symbol = '≠';
$final_note = '2';
}
You are passing $condition as a string. Just save the $condition variable as a boolean.
I have searched the internet in great detail and found many samples of PHP scripts that are addressing permutations. All these scripts are ok for permuting a few numbers and a few words here and there but when we get to perform some heavy stuff I can't execute the code.
Here is the essence of the thing we need and things I did.
I have to find all unique combinations of a range of numbers from 1 to 8.
I do this easily,, no problems there...
Then I have to permute each combination which should result with a crazy number results,, and then I have to perform string replacements on each result...
I know this is intense,,, and I do manage to get it done with numbers up to 6.
6 Executes pretty slow,, and 7 and 8 simply mess with the memory allocation on the machine.
My primary question is
Is there a way I can do permutations super fast? Preferably without affecting memory too badly... AND how fast can in PHP realistically a few million records be displayed (doing it in command line,,, browser doesn't matter here)
I don't know if this will be helpful, but i already made a script that generates the permutation of 10 numbers (0 -> 9) in lexicographic order, and it runs in ~60sec [Quadcore 2.7Ghz] and uses 512MB due to the array sizes ...
By mathematical calculations: 10! = 3628800 permutations in 60 sec, 8! = 40320 permutations, so it should run in (60 * 40320) / 3628800 = 0.67sec say 1 second !
You may need to change the code for a 8-number permutations!
PS: I don't know if the browser would handel the output of +3M numbers !
<?php
/* 0 - > 9 PERMUTATION SCRIPT */
ini_set('memory_limit', '512M');
ini_set('max_execution_time', '0');
$st = timer();
$permutations = array();
for($a=0;$a<=9;$a++){
for($b=0;$b<=9;$b++){
if($b != $a){
for($c=0;$c<=9;$c++){
if($c != $a && $c != $b){
for($d=0;$d<=9;$d++){
if($d != $a && $d != $b && $d != $c){
for($e=0;$e<=9;$e++){
if($e != $a && $e != $b && $e != $c && $e != $d){
for($f=0;$f<=9;$f++){
if($f != $a && $f != $b && $f != $c && $f != $d && $f != $e){
for($g=0;$g<=9;$g++){
if($g != $a && $g != $b && $g != $c && $g != $d && $g != $e && $g != $f){
for($h=0;$h<=9;$h++){
if($h != $a && $h != $b && $h != $c && $h != $d && $h != $e && $h != $f && $h != $g){
for($i=0;$i<=9;$i++){
if($i != $a && $i != $b && $i != $c && $i != $d && $i != $e && $i != $f && $i != $g && $i != $h){
for($j=0;$j<=9;$j++){
if($j != $a && $j != $b && $j != $c && $j != $d && $j != $e && $j != $f && $j != $g && $j != $h && $j != $i){
$permutations[] = $a.$b.$c.$d.$e.$f.$g.$h.$i.$j;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
echo count($permutations);
$et = timer();
$time = $et - $st;
echo "<br/>in : ".$time." seconds.";
function timer(){
list($usec, $sec) = explode(" ", microtime());
return((float)$usec + (float)$sec);
}
?>
EDIT: 1->8 permutation script in: ~0.43 seconds.
<?php
$permutations = array();
for($a=1;$a<=8;$a++){
for($b=1;$b<=8;$b++){
if($b != $a){
for($c=1;$c<=8;$c++){
if($c != $a && $c != $b){
for($d=1;$d<=8;$d++){
if($d != $a && $d != $b && $d != $c){
for($e=1;$e<=8;$e++){
if($e != $a && $e != $b && $e != $c && $e != $d){
for($f=1;$f<=8;$f++){
if($f != $a && $f != $b && $f != $c && $f != $d && $f != $e){
for($g=1;$g<=8;$g++){
if($g != $a && $g != $b && $g != $c && $g != $d && $g != $e && $g != $f){
for($h=1;$h<=8;$h++){
if($h != $a && $h != $b && $h != $c && $h != $d && $h != $e && $h != $f && $h != $g){
$permutations[] = $a.$b.$c.$d.$e.$f.$g.$h;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
echo count($permutations);
?>
if i have statement:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
echo 'what variable's weren't matched';
}
Is there any way of knowing what didn't watch instead of writing everything separately?
Cheers!
No. Your expression was turned into a boolean, so apart from checking the equality(s) again you cannot find out which triggered the "false".
You need to test each individually, but you could do something like this:
$a = 1;
$b = 2;
$c = 3;
$a_matched = $a == 1;
$b_matched = $b == 1;
$c_matched = $c == 1;
if($a_matched && $b_matched && $c_matched)
{
echo 'correct';
}
else
{
if (!$a_matched) echo 'a did not match!';
if (!$b_matched) echo 'b did not match!';
if (!$c_matched) echo 'c did not match!';
}
but that's less clear than just:
$a = 1;
$b = 2;
$c = 3;
if($a == 1 && $b == 2 && $c == 3)
{
echo 'correct';
}
else
{
if (!$a == 1) echo 'a did not match!';
if (!$b == 2) echo 'c did not match!';
if (!$c == 3) echo 'b did not match!';
}
Actually, heh, I take back my comment. You can rely on the boolean short-circuiting to set a variable indicating the last part of the conditional which was true:
if (($x = 'a') && $a == 1 && ($x = 'b') && $b == 2 && ($x = 'c') && $c == 3) {
echo "correct\n";
} else {
echo "$x is wrong\n";
}
Note, I would never write this in production code because it's goofy and very hard to understand what's supposed to be going on. But fun to fiddle with, at least.
Nope! That's not possible. You can make life a lot simpler by using arrays, though:
$results = array(1, 2, 4);
$expected = array(1, 2, 3);
$count = count($results);
$wrong = array();
for($i = 0; $i < $count; $i++) {
if($results[$i] !== $expected[$i]) {
$wrong[] = $i;
}
}
if(count($wrong) > 0) {
echo "There were wrong ones. They were at positions: " . implode(', ', $wrong);
} else {
echo "All good!";
}
For example.