Why does $m = 40 on this? I am new in php and I get why $k = 40 , but I cant figure out why $m = 40 as well.
<?php
$i = 29;
$j = 11;
$m = 10;
$k = $i++ + $j--;
echo "k = ".$k."<br>";
$j = ($j - 4) / 2;
$m += $j * 10;
echo "m = ".$m."<br>";
?>
<?php
$i = 29;
$j = 11;
$m = 10;
$k = $i++ + $j--;
// $j == 10
echo "k = ".$k."<br>";
$j = ($j - 4) / 2;
// $j = (10 - 4 ) / 2 == 3
// $m == 10
$m += $j * 10;
// $m + $j * 10 = 10 + 3 * 10 == 40
echo "m = ".$m."<br>";
?>
fast explaination of what's happening:
$k = 29 + 11 = 40;
$i = 30 (++)
$j = 10 (--)
echo K = 40
$j = (10-4) / 2 = 3
$m = 3 * 10 + 10 = 40
the '+=' operator add the results of the operation on the right ($j * 10) to the value on the left ($m that contains 10)
Here it is:
$i = 29;
$j = 11;
$m = 10;
$k = $i++ + $j--;
// $k = 40
// $i = 30
// $j = 10
echo "k = ".$k."<br>";
//echo k = 30
$j = ($j - 4) / 2;
// $j = 3
$m += $j * 10;
// $m = 10 + ( 3 * 10 ) = 40
echo "m = ".$m."<br>";
//echo m = 40
It's probably the $++ and ++$ the puzzles you.
// First try:
$a = 0;
$b = 1;
var_dump($c = $a + $b++); // means: $c = $a + $b; $b = $b + 1;
// Second try:
$a = 0;
$b = 1;
var_dump($c = $a + ++$b); // means: $b = $b + 1; $c = $a + $b;
^ make sense?
$j = ($j - 4) / 2; // $j is equal with 10 because of decrement $j-- before
$m += $j * 10; // after that line, $j becomes (10-4)/2 which is 3
echo "m = ".$m."<br>"; // and finally $m = $m + ($j * 10) => 10 + (3 * 10)
Related
Expectation
My Code
<?php
$value = 100;
$years = 5;
for ($i = 1; $i <= $years; $i++) {
$income = round($value * (pow(1+6/100, $i)), 2);
echo $i, " ", $income, "<br>";
}
?>
Output
1 106
2 112.36
3 119.1
4 126.25
5 133.82
How can I get result like my expectation above ?
Just add one more loop
$value = 100;
$years = 5;
for ($i = 1; $i <= $years; $i++) {
$income = array();
for ($j = 6; $j <= 10; $j++) {
$income[] = round($value * (pow(1+$j/100, $i)), 2);
}
echo $i, " ", implode(' ', $income), "<br>";
}
hi i am using php to learn algorithms, i wanted to convert this psuedocode into php,
for i = 1 to n − 1
minval = A[i]
minindex = i
for j = i to n
if (A[j] < minval)
minval = A[j]
minindex = j
exchange A[i] and A[minindex]
this the corresponding code in php
$A = array(1, 4, 2, 3, 70, 10, 7 );
$n = sizeof($A);
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
if ($A[$i] > $A[$j]){
$temp = $A[$j];
$A[$j] = $A[$i];
$A[$i] = $temp;
}
}
}
print_r($A);
print_r is outputting the array as its original order, why my algorithms doents reorder the array ?
You should check your forloops :
for ($i = 0; $i == $n - 1; $i++){
for ($j = $i + 1; $j == $n; $j++){
should be
for ($i = 0; $i < $n - 1; $i++){
for ($j = $i + 1; $j < $n; $j++){
As the second argument in for is a requirement to continue the loop.
$string = 3;
for ($i=0; $i<$string; $i++) {
for($j=0; $j<$string; $j++) {
print $arr[$i][$j] = rand(1,5);
}
print "<br>";
}
Basically this code will output something like
5 5 4
2 5 2
4 5 3
I want to print on the screen something like 5 + 5 + 4 + 2 + 5 + 2 + 4 + 5 + 3 = 35
I have tried multiple methods but am struggling when getting the + to print in the right places.
If I have understood you correctly then you can use the following code:
$arr = array();
$string = 3;
$temp = array();
$sum = 0;
for ($i=0; $i < $string; $i++) {
for($j=0; $j<$string; $j++) {
$arr[$i][$j] = rand(1,5);
$sum += $arr[$i][$j];
$temp[] = $arr[$i][$j];
}
}
echo implode(' + ', $temp).' = '.$sum;
Result:
2 + 5 + 3 + 3 + 4 + 2 + 5 + 3 + 1 = 28
EDIT (without the implode function [OP request in comments]):
$arr = array();
$string = 3;
$temp = '';
$sum = 0;
for ($i = 0; $i < $string; $i++) {
for($j = 0; $j < $string; $j++) {
$arr[$i][$j] = rand(1,5);
$sum += $arr[$i][$j];
if ($i == ($string - 1) && $j == ($string - 1)) {
$temp .= $arr[$i][$j];
} else {
$temp .= $arr[$i][$j].' + ';
}
}
}
echo $temp.' = '.$sum;
Result:
1 + 2 + 1 + 3 + 2 + 3 + 5 + 4 + 5 = 26
Logfiles are full of this error:
Undefined offset: 255 in /var/www/html/site2/functions.inc.php on line 764"
The line 764 is the following:
$counter[$i] = $counter[$x];
The whole function see below.
Can somebody help me to fix it? Thanks.
function Encode($data,$pwd) {
$pwd_length = strlen($pwd);
for ($i = 0; $i < 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
$counter[$i] = $i;
}
$x = '';
for ($i = 0; $i < 255; $i++) {
$x = ($x + $counter[$i] + $key[$i]) % 256;
$temp_swap = $counter[$i];
$counter[$i] = $counter[$x];
$counter[$x] = $temp_swap;
}
$a = '';
$j = '';
$Zcrypt = '';
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $counter[$a]) % 256;
$temp = $counter[$a];
$counter[$a] = $counter[$j];
$counter[$j] = $temp;
$k = $counter[(($counter[$a] + $counter[$j]) % 256)];
$Zcipher = ord(substr($data, $i, 1)) ^ $k;
$Zcrypt .= chr($Zcipher);
}
return $Zcrypt;
}
Your for-loop stops when $i >= 255. You don't want that. I think you want the for-loop to stop at $i>255.
So change
for ($i = 0; $i < 255; $i++) {
into
for ($i = 0; $i <= 255; $i++) {
And you're good to go!
I have a for loop, where $i is 0, and it will run until $i reaches 4. I am trying to make a code that would output numbers in an order like this: 01, 11, 02, 12, 03, 13... etc... Now, the thing is next: when $i is 1, the script should make an order of those number in the boundaries of 1 and 20. When $i is 2, it would be 21 to 40, etc.
I've tried many things (mostly deleted), could not come up with anything that would work the right way.
The inner loop:
for ($j = 0; $j != 10; ++$j)
{
echo $j + 1 + 10 * ($i - 1);
echo $j + 1 + 10 * $i;
}
Try this piece of code;
<?php
$num = 4;
for($i=1;$i<($num + 1);$i++){
$string = "0" . $i . ", 1" . $i;
if($i<$num){
$string .= ", ";
}
echo $string;
}
?>
printf will format your numbers with a leading zero, as specified:
<?php
$format = "%02d ";
for ($i = 1; $i <= 4; $i++) {
$k = 2 * $i - 1;
for ($j = 1; $j <= 10; $j++) {
printf($format, ($k - 1) * 10 + $j);
printf($format, $k * 10 + $j);
}
echo "<br />";
}
?>
You can try:
<?php
$ten = 10;
for ($i = 0; $i<=4; ++$i)
{
echo "0".$i." , ";
echo $ten + $i."<br/>";
}
?>
Only change the range of $i
Thanks