Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can anyone explain in depth why this outputs 9
here is my code in PHP:
$x = 4;
$x = $x+++$x++;
echo $x;
Execution goes like this:
$x = $x++ + $x++; ($x = 4)
$x = 4 + $x++; ($x = 5)
$x = 4 + 5; ($x = 6)
$x = 9;
For a more detailed answer of a more complex example in Java, see this answer: Incrementor logic
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When using increment on variables in Javascript, lint says that x += 1 is preferred over x ++.
But what about in PHP?
Is there any difference between the += and ++ or does it just not really matter?
Well whatever you might say about conventions, try running the following...
$i = 1; $s = 's';
$i++; $s++;
echo $i.'<br>'.$s.'<br>';
$i = 1; $s = 's';
$i += 1; $s += 1;
echo $i.'<br>'.$s.'<br>';
the output is somewhat unexpected...
2
t
2
1
so I would say it could matter very much which is chosen!
x += 1 is rather equivalent to ++x.
All those expressions (x += 1, x++ and ++x) increment the value of num by one, but the value of x++ is the value x had before it got incremented.
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
If i have a loop for example
for ($a=1; $a<6; $a++)
Is it possible to say for the first loop if another variable like $b is the same as one of the other numbers that will come like 2, 3, 4 or 5.
And what if i have a word split, so every letter is a variable like this:
$letter1 = str_split($word)[0];
$letter2 = str_split($word)[1];
$letter3 = str_split($word)[2];
$letter4 = str_split($word)[3];
$letter5 = str_split($word)[4];
How can i check if $a is at 1 in the loop,if it is not letter1 but letter2, 3, 4 or 5?
sure. Just check it against the counter:
for ($a=1; $a<6; $a++){
if ($a===$b){
echo ("b is caught!");
break;
}
}
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
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)
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
I have a math function, for example:
string = "3x+6.5y-23z"
I need to extract the function and get x, y, z; give value x = 6, y = 7, z = 8; and solve.
you can do it like this
<?php
$x = 6;
$y = 7;
$z = 8;
echo $string = 3*$x+6.5*$y-23*$z;
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have one question to you, and I hope you will help me. I want to write calculator but i don't have chance tu change operations , I can only use addition operation. I don't know what happened. can anyone help me?
look at it, what happened?
<?php
$x = isset($_POST['field']);
if($x == 1){
echo $x + 5;
}
elseif($x == 2){
echo $x - 5;
}
?>
You code is wrong as this line will evaluate only as 0 (false) or 1 (true) if condition is met:
$x = isset($_POST['field']);
You need to do it that way:
$x = isset($_POST['field']) ? $_POST['field'] : 0;
(where 0 is default value, $x is assigned to in case there's no $_POST['field]` set.
$x = isset($_POST['field']) ? (int)$_POST['field'] : 0;
You want the value, not the boolean if it's set or not.