This question already has answers here:
What's the difference between ++$i and $i++ in PHP?
(15 answers)
Closed 7 years ago.
I am new to php, for some reason the increment/decrement works inversely. Can someone tell me the reason behind this?
<?php
$var1 = 3;
echo "Addition = " . ($var1 += 3) . "<br>" ;
echo "Subtraction = " . ($var1 -= 3) . "<br>" ;
echo "Multiplication = " . ($var1 *= 3) . "<br>" ;
echo "Divison = " . ($var1 /= 3) . "<br>" ;
echo "Increment = " . $var1++ ;
echo "Decrement = " . $var1-- ;
?>
If you know how the increment and decrement operators work, you'll get the answer.
echo "Increment = " . $var1++ ; //Prints $var1 and then increments the value
echo "Decrement = " . $var1-- ; // Prints the incremented value from previous operation and then decrements the value
To achieve what you are trying to do, use --$var1
Related
I want to create an application that shows multiplication, addition, subtraction and division with 2 random numbers. I made a function that shows random numbers:
function Numbers() {
echo(mt_rand() . "<br>");
echo(mt_rand() . "<br>");
echo(mt_rand(1,10));
}
Numbers();
Can someone explain to me how I can make it go +/- and x each other?
I now changed my code to this:
function Numbers() {
$Number1= echo(mt_rand() . "<br>");
$Number2= echo(mt_rand() . "<br>");
$Number1 + $Number2;
$Number1 - $Number2;
$Number1 / $Number2;
}
Numbers();
Here's an example for the addition, you figure out the rest:
$a = mt_rand();
$b = mt_rand();
echo "$a + $b = " . ($a + $b);
function printRnd()
{
$a_ = rand(1,10);
$b_ = rand(1,10);
echo "a={$a_}, b={$b_}<br><br>";
$plus_ = $a_+$b_;
$minus_ = $a_-$b_;
$multi_ = $a_*$b_;
$divid_ = $a_/$b_;
echo "a+b={$plus_}<br>";
echo "a-b={$minus_}<br>";
echo "a*b={$multi_}<br>";
echo "a/b={$divid_}<br>";
}
printRnd();
Here is the code:
<?php
for($i =0, $x = 100 ; $i<1; $i++){
echo $x . 'y' . $i+1 . ' = '. $i*$x . ' <br>';
}
?>
My Expected Output was: 100 y 1 = 0
But the actual result was: 101 = 0;
Where did 'y' go?
https://ideone.com/n9HYGp
. have more operator precedence than +.
echo $x . 'y' . $i+1 = 101
Because it will operate as
echo ($x . "y" . $i)+1 ;
This is what happens.
$x3= ($x . "y" . $i); //100y0
$u = $x3+1 ; //101
You are doing + operation on a string. So the first digits before any characters will be taken as integer value.
Eg:
10y0g8 = 10
t10 =0
By doing an Arithmetic Operation, interpreter will convert string to integer, and it will discard all other characters. so 100y+1 = 101 it won't be 101y
as explained in the above (Subin Thomas) answer the addition of 1 with the varchar value like 100y0 will add 1 with 100(the first occurence of integer). the following code will work as you expected
<?php
for($i =0 ,$x = 100; $i<1; $i++){
echo $x . 'y' . ($i+1) . ' = '. $i*$x . ' <br>';
}
?>
To test :
<?php
for($i =0, $x = 100 ; $i<1; $i++){
echo $x . 'y' . ($i+1) . ' = '. $i*$x . ' <br>';
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can some one tell why my php line break not working ( echoing ) ?
I know i can write the code in a different way to make the line break work, but i want to know the reason behind this ?
<?php
$var1 = 3;
echo "Addition = " . $var1 += 3 . "<br>";
echo "Subtraction = " . $var1 -= 3 . "<br>";
echo "Multiplication = " . $var1 *= 3 . "<br>";
echo "Division = " . $var1 /= 3 . "<br>";
?>
Well seems like I have to clean some things up here.
Let's take a look at the operator precedence, which says:
. has a higher precedence, than +=, -=, *=, /=
. is left associative
=, +=, -=, *=, /= is right associative
We also take a look at the note at the bottom of the manual:
Note:
Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
Means that even tough = has a lower precedence than . it gets evaluated first. You can also see this if you do something like this:
$xy = "HERE";
echo "I am " . $xy = "NOT HERE";
Now you would think that . has a higher precedence than = and will get evaluated first, but as from the note in the manual, the assignment is first and you end up with this:
echo "I am " . ($xy = "NOT HERE");
output:
I am NOT HERE
So if we put all these information's together, we can say, that the assignment gets evaluated first, but it's right assocative. Means this:
$var1 = 3;
echo "Addition = " . ($var1 += 3 . "<br>");
echo "Subtraction = " . ($var1 -= 3 . "<br>");
echo "Addition = " . ($var1 *= 3 . "<br>");
echo "Addition = " . ($var1 /= 3 . "<br>");
So this code will end up in this:
echo "Addition = " . ($var1 += "3<br>");
echo "Subtraction = " . ($var1 -= "3<br>");
echo "Addition = " . ($var1 *= "3<br>");
echo "Addition = " . ($var1 /= "3<br>");
Which then through the arithmetic operator gets convert to an integer we end up with this:
echo "Addition = " . ($var1 += 3);
echo "Subtraction = " . ($var1 -= 3);
echo "Addition = " . ($var1 *= 3);
echo "Addition = " . ($var1 /= 3);
And after the assignment is done the concatenation gets evaluated, which looks like this:
echo "Addition = " . 6;
echo "Subtraction = " . 3;
echo "Addition = " . 9;
echo "Addition = " . 3;
With this you end up in this output:
Addition = 6Subtraction = 3Addition = 9Addition = 3
And now how to solve this? Simply wrap your assignment in parentheses, so that the <br> tag doesn't get into the assignment. E.g.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) . "<br>";
echo "Multiplication = " . ($var1 *= 3) . "<br>";
echo "Division = " . ($var1 /= 3) . "<br>";
//^ ^ So the br tag doesn't get in the assignment of the variable.
This is happening because of the type casting issues. 3 . "<br>" will be converted to number while the operation will be performed. Wrap the inside () so that the operations are performed first then the concatenation.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
You can use commas,
echo "Addition = " . $var1 += 3 , "<br>";
echo "Subtraction = " . $var1 -= 3 ,"<br>";
echo "Addition = " . $var1 *= 3 , "<br>";
echo "Addition = " . $var1 /= 3 ,"<br>";
Or wrap it in brackets:
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Otherwise the 3 number is concatenated with <br>.
Your PHP means:
echo "Addition = " . $var1 += (3 . "<br>");
echo "Subtraction = " . $var1 -= (3 ."<br>");
echo "Addition = " . $var1 *= (3 . "<br>");
echo "Addition = " . $var1 /= (3 ."<br>");
And number + 3 . '<br>' is number + (int)(3 . '<br>') which is number + 3. No <br> exists now due to retyping to number(converting to number).
Use brackets around equations.
echo "Addition = " . ($var1 += 3) . "<br>";
echo "Subtraction = " . ($var1 -= 3) ."<br>";
echo "Addition = " . ($var1 *= 3) . "<br>";
echo "Addition = " . ($var1 /= 3) ."<br>";
Try this..
"." is used for php variable to concate not for numbers
<?php
$var1 = 3;
echo "Addition = ". ($var1 += 3) ."</br>";
echo "Subtraction = ". ($var1 -= 3) ."</br>";
echo "Addition = ". ($var1 *= 3) ."</br>";
echo "Addition = ". ($var1 /= 3) ."</br>";
?>
Try this way.
<?php
$var1 = 3;
echo "Addition =" . ($var1 += 3 ).'<br>';
echo "Subtraction =" . ($var1 -= 3).'<br>';
echo "Addition =" . ($var1 *= 3 ).'<br>';
echo "Addition =" . ($var1 /= 3 ).'<br>';
?>
I'm trying to implement pagination for a search result. The following code works perfectly:
echo "<p>" . $data['meta']['total'] . " properties found. (search " . $data['meta']['searchId'] . ")</p>\n";
$pages = $data['meta']['total'] / $count;
$pages = ceil($pages);
echo "<p>" . $pages . "</p>\n";
However, if I add in the following, I get a timeout:
$page = 1;
echo "<p>";
while ($page <= $pages); {
echo $page++ . " ";
}
echo "</p>\n";
No doubt I'm missing something obvious.
Your error is here:
while ($page <= $pages); {
//^ See this empty statement here!
echo $page++ . " ";
}
Your while loop loops trough a empty statement so no statement is going to increment $page. So your curly brackets are just a normal code block, in order to get your while loop working just remove the semicolon like this:
while ($page <= $pages) {
echo $page++ . " ";
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have these two codes in PHP:
$msgs = 5;
//These two types of string concatenation
echo 'You got ' . $msgs . ' messages';
echo "You got $msgs messages";
Let's make some new test, is really simple
Live example
Test
<?
$str1 = $str2 = "";
for ($i=0; $i < 10000; $i++) {
$start = microtime(true);
$str1 .= 'You got ' . $i . ' messages';
$str1_test[] = microtime(true) - $start;
}
echo "Dotted: " . ($str1_result = array_sum($str1_test) / 10000);
echo PHP_EOL;
for ($i=0; $i < 10000; $i++) {
$start = microtime(true);
$str2 .= "You got {$i} messages";
$str2_test[] = microtime(true) - $start;
}
echo "Interpolated: " . ($str2_result = array_sum($str2_test) / 10000);
echo PHP_EOL . ($str2_result < $str1_result ? "Interpolation" : "Dot") . " is faster!";
Result
Dotted: 1.1234998703003E-6
Interpolated: 1.2600898742676E-6
Dot is faster!
Real fact
The difference is to small to be significative, I personally like the interpolation, is elegant and faster to read, is up to you! ;)