When do we MUST use "condition" function in php? - php

I knew that we use continue to jump to the next iteration in loop
For example
$x = 0;
for( $i = 0; $i < 10; $i++ ) {
if( $i%2 == 0 ) {
continue;
}
$x = $x + $i;
}
In this case we use continue to skip the even number, but we can do this instead
$x = 0;
for( $i = 0; $i < 10; $i++ ) {
if( $i%2 != 0 ) {
$x = $x + $i;
}
}
Can anyone show me some cases that we MUST use continue instead of only using opposite condition.

Related

Display a complete number without scientific writing php [duplicate]

This question already has answers here:
Working with large numbers in PHP
(8 answers)
Closed 2 years ago.
Hello I would like to display in my code a full number to 20 decimal without scientific notation.
I think it's a memory problem in php.
can you help me ? thanks
function get($l, $c)
{
$value = 0;
if (0 <= $c && $c <= $l && $l < 5000) {
$tab = [];
for ($i = 0; $i <= $l; $i++) {
for ($j = 0; $j <= $c; $j++) {
if ($i == $j || $i - 1 <= 0 || $j <= 0) {
$tab[$i][$j] = 1;
$value = $tab[$i][$j];
} elseif ($i != $j) {
$tab[$i][$j] = ($tab[$i - 1][$j - 1]) + ($tab[$i - 1][$j]);
$value = $tab[$i][$j];
}
}
}
}
return $value;
}
echo get(67, 34); // found :1.422652073762E+19 , excpected:14226520737620288370
Have a look at BCMath functions:
function get($l, $c)
{
$value = 0;
if (0 <= $c && $c <= $l && $l < 5000) {
$tab = [];
for ($i = 0; $i <= $l; $i++) {
for ($j = 0; $j <= $c; $j++) {
if ($i == $j || $i - 1 <= 0 || $j <= 0) {
$tab[$i][$j] = 1;
$value = $tab[$i][$j];
} elseif ($i != $j) {
// magic happens here
$tab[$i][$j] = bcadd($tab[$i - 1][$j - 1], $tab[$i - 1][$j]);
$value = $tab[$i][$j];
}
}
}
}
return $value;
}
$result = get(67, 34);
var_dump($result == '14226520737620288370');
echo $result;
Output
bool(true)
14226520737620288370
Working example.
There is that : https://www.php.net/manual/en/function.number-format.php
You can print any number and choose number of decimals you want

Multiple comparisons inside for loops don't break php code. Why?

Why this piece of code works when it is clearly wrong in the second for loop (for ($i==0; $i<$parts; $i++) {)?
Does php allows for multiple comparisons inside for loops?
function split_integer ($num,$parts) {
$value = 0;
$i = 0;
$result = [];
$modulus = $num%$parts;
if ($modulus == 0) {
for($i = 0; $i < $parts; $i++)
{
$value = $num/$parts;
$result[] = $value;
}
} else {
$valueMod = $parts - ($num % $parts);
$value = $num/$parts;
for ($i==0; $i<$parts; $i++) {
if ($i >= $valueMod) {
$result[] = floor($value+1);
} else {
$result[] = floor($value);
}
}
}
return $result;
}
Code for ($i==0; $i < $parts; $i++) runs because $i==0 has no impact on loop.
In normal for loop first statement just sets $i or any other counter's initial value. As you already set $i to 0 earlier, your loop runs from $i = 0 until second statement $i < $parts is not true.
Going further, you can even omit first statement:
$i = 0;
for (; $i < 3; $i++) {
echo $i;
}
And loop will still run 3 times from 0 to 2.

Two conditions in a for loop

Why does this for loop work with each condition on their own, but together the first condition doesn't matter?
for ($j = 0; $j < 5 or $j < $synCount; $j++)
I only want the loop run five times
or
if synCount is less than this.
You probably mean "$j under 5 and $j under $sysCount", or:
$j < min(5, $sysCount)
very simple
$j < min(5, $sysCount)
You can youse the break statement to leave the for loop :
for ($j = 0; $j < 5 ; $j++)
{
if( $j >= $synCount )
break;
//treatment
}
Or calculate your limit before the loop :
$ max = $synCount < 5 ? $synCount : 5;
for ($j = 0; $j < $synCount ; $j++)
{
//treatment
}
Another solution,quickest : use min() :
for ($j = 0; $j < min(5, $synCount) ; $j++)
{
//treatment
}
Try it like this:
$loopcount = ($syncount < 5) ? $syncount : 5;
for ($j = 0; $j < $loopcount; ++$j) {
}
The first line determines whether $syncount is less than 5 or not and then assigns a value to $loopcount based on that. Then the loop runs the required number of times.
for ($j = 0; $j < ($syncCount <= 5 ? $syncCount : 5); $j++)
or slightly optimized (but I guess with 5 or less iterations this absolutely doesn't matter)
for ($j = 0, $limit = min($syncCount, 5); $j < $limit; $j++)
Anothe nice solutions
foreach (range(0, min($syncCount, 5)) as $j)
Sidenote
$syncCount <= 5 ? $syncCount : 5 == min($syncCount, 5)
$productsprice=ProductPrice::model()->findAllByAttributes(array ('product_id'=>$products_data->product_id));
foreach($productsprice AS $productsprice):
for($quantity = 0; $quantity <= 10; $quantity++)
{
echo '<li >'.array(value=>'ProductPrice::model()->getquantity($data->quantity)').'</li>' ;
}
endforeach;
Two conditions may indeed be applied inside for loop using logical operator. For example check status until it's ok
for($i = 0; $i < 100 && $status != 'ok'; $i++){
sleep(1);
$status = checkStatus();
}
Otherwise you need to use additional if expression with break
for($i = 0; $i < 100; $i++){
sleep(1);
$status = checkStatus();
if($status == 'ok') break;
}

decrese value of loop

I have this code, but i want in second loop a decrease of $p value. The first internal loop must be repeated three times, the second, two times and the last, one time. I am trying $p-- but without success.
Any idea ? thanks
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
$p--;
}
}
Move your $p-- to outside the inner for loop:
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
}
$p--;
}
Or better, just depend on the value of $i:
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < 3 - $i; $o++) {
echo "something";
}
}
Or if you don't actually use $i:
for ($i = 2; $i >= 0; $i--) {
for ($o = 0; $o < $i; $o++) {
echo "something";
}
}
It's quite simple.
for ($i = 2; $i >= 0; $i--)
{
}
Set the starting number at the upper limit number, and then go down until equal to 0, $i minus 1;
You need to decrement $p outside the first loop

Why does this code cause an Expected ")" error?

<?php
$i == array(1, 2);
$j == array(a, b);
$m == count($j);
$n == count($i);
for ( $i = 0; $i < $m; i++ )
{
for ( $j = 0; j < $n; j++)
{ echo $i."x"$j; }
}
?>
The error is referencing line 6: for ( $i = 0; $i < $m; i++ )
for ( $i = 0; $i < $m; $i++ )
Note the dollar sign I added before the i++
Same goes for your other for statement:
for ( $j = 0; $j < $n; $j++ )
Wierd error indeed, but it i is not a variable (although PHP might flag a E_NOTICE and convert it to 'i'. You want to reference your variable, so you must add a $ before.
Most likely what you want is:
<?php
$iArray = array(1, 2);
$jArray = array('a', 'b');
$n = count($iArray);
$m = count($jArray);
for ( $i = 0; $i < $n; $i++) {
for ( $j = 0; $j < $m; $j++) {
echo $iArray[$i] . "x" . $jArray[$j];
}
}
?>
The things I changed:
== is used for comparison, = is used for assignment
The second array I assumed you wanted the string literals 'a' and 'b', but you could have also wanted $a and $b if you declared those variables somewhere else
you assign $i to an array, but then in your for loop you overwrite it with $i = 0. You most likely want two variables
missing $s, like I mentioned above
$m was being used for the number of variables in $jArray, but you used it to iterate over $iArray
So just a few pointers, brush up on you PHP and try to make sure your code works with every little change. Make 1 modification, then run it. It is very easy to get lost in syntax for PHP since it is such a dynamic scripting language
You have a bunch of equality checks there. I'm assuming you were actually assigning variables rather than checking for equality.
Change all == equality checks to assignments (=)
You also have improper concatenation on line 9 and I added a comment pointing out another possible error.
$i == array(1, 2);
$j == array($a, $b); // <--Put in $ signs if these are variables in the array
$m == count($j);
$n == count($i);
for ( $i = 0; $i < $m; $i++ )
{
for ( $j = 0; $j < $n; $j++)
{ echo $i."x".$j; }
}

Categories