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>';
}
?>
Related
so i have this loop like this
<?php for($i = 0; $i <$size; $i++) { ?>
<li>
<a pagenumber="<?php $i+1?>" class="pagination" href="#"><?php $i+1?></a></li>
<?php } ?>
so it kinda looks like this:
1 2 3 4 5 6 7 8
but the problem here is when there is like say a 100 page there is alot of numbers how can i do somthing like this
1 2 3 4 5 .... 98 99 100
and not have numbers from 1 to 100 just keep showing up
so for example when i click 5 then it should show
6 7 8 9...98 99 100
or somthing better i am not able to figure how to go about this
You don't need to loop.
You can use array_slice and implode to get the expected output.
I added $n to give you the option to output one page previous to the selected if it's not page 1 that is selected.
$pages = range(1,100); // range(1, $size);
$page = 1; // page selected by user
if($page ==1){
$n = 0;
}else{
$n = 1;
}
echo implode(" ", array_slice($pages, $page-1-$n, 5)) . " . . . . " . Implode(" ", array_slice($pages, -3));
Output:
1 2 3 4 5 . . . . 98 99 100
With page 5 selected:
4 5 6 7 8 . . . . 98 99 100
https://3v4l.org/doZbI
If you want to stay in the loop then you can use two loops and one optional echo.
$size =100;
$page = 5;
// Echo one page prior to selected page
if($page != 1){
Echo '<a pagenumber="' . ($page-1) .'" class="pagination" href="#">' . ($page-1) . '</a></li>';
Echo ' . '; // show dot for current page
}
// Echo +1 -> +5
For($i = $page+1; $i < $page+6; $i++){
Echo '<a pagenumber="' . $i .'" class="pagination" href="#">' . $i . '</a></li> ';
}
Echo '. . . . ';
// Echo last three pages
For($i = $size-3; $i <= $size; $i++){
Echo '<a pagenumber="' . $i .'" class="pagination" href="#">' . $i . '</a></li> ';
}
https://3v4l.org/1P0W3
So I am working on my Final Project for a web application development class I'm taking and I am creating a Powerball lottery generator. For this to work, the White ball numbers cannot be duplicated. Here is how my code is looking so far:
<?php
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
?>
The issue with this code is that there is a chance that variables 'a' through 'e' have a chance of being duplicate numbers. What code could I use to ensure that none of the variables 'a' through 'e' are the same? I thought of doing something like:
if($a != $b || $a != $c || $a || $d...){
//echo numbers
}else{
//generate new numbers
};
But that is just too much work and I always try to find the most efficient ways to write code. I don't want to have to write more code than I need to. Any assistance would be greatly appreciated. Thank you in advance!
You could generate the numbers this way:
$arr = range(1, 69);
shuffle($arr);
$a = $arr[0];
$b = $arr[1];
$c = $arr[2];
$d = $arr[3];
$e = $arr[4];
Also take a look at Generating random numbers without repeats
Add them in an array and check for uniqueness:
<?php
for($x = 1; $x < 6; $x++){
$unique = false;
while(!$unique) {
//set each white ball variable (a through e) to a random number between 1 and 69
$a = floor((lcg_value() * 69 + 1));
$b = floor((lcg_value() * 69 + 1));
$c = floor((lcg_value() * 69 + 1));
$d = floor((lcg_value() * 69 + 1));
$e = floor((lcg_value() * 69 + 1));
$numbers = array($a, $b, $c, $d, $e);
if(count($numbers) == count(array_unique($numbers)) {
$unique = true;
}
}
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
}
While loop the random generation of numbers and check for duplicates on the fly.
Test it here:
https://3v4l.org/odOqb
I have changed the random numbers to a smaller size to see if it does create duplicates.
But I have not seen any.
<?php
$arr =array();
for($x = 1; $x < 6; $x++){
//set each white ball variable (a through e) to a random number between 1 and 69
While (count($arr) != 5){
$arr[] = floor((lcg_value() * 6 + 1));
$arr = array_unique($arr);
}
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
Var_dump($arr);
//echo all white ball numbers and powerball number
//echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
};
To make it as efficient as possible you do not want to have to generate a new set of numbers each time therefore if a duplicate appears you would just want to re-pick for that letter right away.
To do this you can add the elements to an array and search through it after each letter to make sure its a unique number. This is done through the utilization of the for loop, while loop, and check variable.
<?php
for($x = 1; $x < 6; $x++) {
//set each white ball variable (a through e) to a random number between 1 and 69
$uniqueNumbers = array();
$check = true;
$a = floor((lcg_value() * 69 + 1));
array_push($uniqueNumbers, $a);
while ($check) {
$check = false;
$b = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($b == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $b);
$check = true;
while ($check) {
$check = false;
$c = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($c == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $c);
$check = true;
while ($check) {
$check = false;
$d = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($d == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $d);
$check = true;
while ($check) {
$check = false;
$e = floor((lcg_value() * 69 + 1));
foreach ($uniqueNumbers as $element) {
if ($e == $element) {
$check = true;
}
}
}
array_push($uniqueNumbers, $e);
//set powerball number variable to a number between 1 and 26
$f = floor((lcg_value() * 26 + 1));
//echo all white ball numbers and powerball number
echo "<b><u>Set #" . $x . "</u></b> - <b>White ball numbers are: </b>" . $a . " , " . $b . " , " . $c . " , " . $d . " , " . $e . ". <b>Powerball Number is </b>" . $f . ".<br />";
}
Below code is for 6/49 Canada lottery
<body bgcolor="gold"><font size="9"></font>
<pre>
<?php
// Code by bhupinder Deol . modify according to needs
for ($i=0;$i<=10;$i++) {
for ($x=0;$x<=5;$x++) {
$rand[$x]=rand(1,49);
}
asort($rand);
$result = array_unique($rand);
$count = count($result);
if ($count == 6) {
print_r($rand);
$x=0;
}
else
{
echo "same numbers in array";
--$x;
}
}
?>
</pre>
</body>
i want to display 10 instances of an image in a 5x2 square. I coded this in PHP/HTML/CSS:
for ($i = 0; $i <= 9; $i++) {
if ($i >= 5) {
$top_position=780;
echo "1". "<br />";
}else {
$top_position = 430;
echo "2". "<br />";
}
if ($i >= 5) {
$u = 20 + 250 * $i - 250 * 5;
}
else {
$u = 20 + 250 * $i;
}
echo "<style> " . ".background" . $u . "{" . "position: absolute;
left: " . $u . "px;" . "top: " . $top_position . "px;" . "</style>";
echo "<img src=http://localhost/Summoner's%20Index/images/scheme.png class=background" . $u . ">";}
I get an 5x1 in the second row, and as text i get: 1111122222, so it seems as if the programm would work properly. Why do i just get 5images and not 10?
Check out the code below:
for ($i = 1; $i <= 10; $i++) {
echo "<img src=http://localhost/Summoner's%20Index/images/scheme.png class=background" . $u . ">";
if ($i == 5){
echo "<br>";
}
}
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
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>';
?>