Im trying to make a random number counter game that uses a for loop to print out 6 random numbers 1 - 6. I want to make it so the code can say how many times the number 6 shows in the loop.
At the moment I have the code it prints out for a loop of 6 random numbers but it only counts the numbers printed out.
For example
Welcome to the Dice Game!
How many sixes will you roll?
4 2 4 6 4 6
You rolled 2 six(es)!
<?php
echo"<h1>Welcome to the guess game thing guess how many 6s!</h1>";
$counter = 0;
for ($i=0; $i <=6;$i++) {
$randomNum = rand(1,6);
if ($randomNum <= 6) {
echo "<br> $randomNum";
$counter++;
}
else
{
echo"$randomNum <br>";
}
}
echo"<br>You rolled $counter sixes";
Some minor changes but you were almost there. Being consistent with your line breaks and verifying you check specifically for 6
$numberToMatch = 6;
for ($i = 0; $i <= 6; $i++) {
$randomNum = rand(1,6);
if ($randomNum == $numberToMatch) {
$counter++;
}
echo "$randomNum <br>";
}
You can do it like this:
$num = $_POST["num"];
for ($i=0; $i <=100;$i++) {
$randomNum = rand(1,10);
if ($randomNum == $num) {
echo $randomNum;
break;
}
echo $randomNum;
}
echo"<h2> there are $i</h2>";
Related
I need to see if the numbers 1 - 1000 are divisible and make them the color red. But i've got no idea how to.
here's my code:
<?php
for ($i=1; $i<=1000; $i++) {
echo " $i <br/>";
}
if ($i % 7 != 0) {
$i += 7 - ($i % 7);
<?php
for ($i=1; $i<=1000; $i++) { // iterate through the numbers
if ($i % 7 != 0) // not divisible by 7, just print the number
echo $i." <br/>";
else //divisible by 7, print the number inside a span in order to color the text red
echo '<span style="color:red">'.$i.'</span></br>'
}
?>
I am working with some legacy PHP code so re-writing this isn't an option at this point but I have a dropdown for number of years and months of employment and currently they go from 0 - 11, 0 - 65. Can a PHP loop array numbers starting at NULL, which adds -Select- as the default forcing user to make a selection, but also have 0 as the starting number?
I've tried:
for ($i = NULL; $i <= 11; $i++) {
echo $i;
}
But 0 is no loner an option
This is what I have currently:
for ($i = 0; $i <= 11; $i++) {
echo $i;
}
I need it to display as:
-Select-
0
1
2
3
4
etc
Echo the text before echoing the numbers using a loop.
<?php
echo '-Select-';
for($i = 0; $i <= 11; ++$i) {
echo $i;
}
NULL++ would not increase the value of NULL, which is essentially nothing. Why not start at -1, and if ($i == -1) then echo select?
It would look like this:
for ($i = -1; $i <= 11; $i++) {
if ($i == -1) {
echo '-Select-';
} else {
echo $i;
}
}
I am stuck with this php loop. Then n = 3, k = 5, s = 21.
Can anyone help me please ?
Rows 3. In the first row 5 chairs:
1 row: ⑁⑁⑁⑁⑁ (5 chairs)
2 row: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
3 row: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Chairs in total: 21
<?php
for ($i=0; $i<=6; $i++) {
for ($j=0; $j<$i; $j++) {
if($i == 1) {
echo '';
}else{
echo '⑁';
}
} echo ' ';
}
?>
If I understand correctly what you want,
$n is number of row you needed,
$k number of chair at row 1.
Each row chair is added by two.
<?php
$n = 3;
$k = 5;
for($i = 0; $i < $n; $i++) {
if($i >= 1) {
echo PHP_EOL;
}
echo str_repeat('-', $k + ($i * 2));
}
Example: When n = 3 and k = 8, it must be obtained that order s = 30 chairs.
Create a PHP solution that specifies the N-row queue in the variables, K-how many chairs should be in the first row.
For example: N = 3; K = 5;
After loading the page (after performing the program actions with the variables available) the following should be displayed:
Rows 3. First row 5 chairs:
Queue 1: ⑁⑁⑁⑁⑁ (5 chairs)
Queue 2: ⑁⑁⑁⑁⑁⑁⑁ (7 chairs)
Queue 3: ⑁⑁⑁⑁⑁⑁⑁⑁⑁ (9 chairs)
Total required chairs: 21
I think you want to use 'if' syntax. Is it right?
If it's not correct, You should describe your work more detail
<?php
// ...
for($i = 0; $i< sizeof($rows); $i++ ){
if ($i % 2 == 0) {
print "It's even";
}
}
// ... If a row is an Array, use this one.
for($i = 0; $i< sizeof($rows); $i++ ){
for($j = 0; $j< sizeof($rows[$i]); $j++ ){
if ($j % 2 == 0) {
print "It's even index of a row";
//do Something.
}
}
}
?>
I need a output like this
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And so on (There are more like this)
I used this code
<?php
for ($i = 0; $i <= 10 ; $i++) {
if ($i == 5 || $i == 4 || $i == 9) { //And so on Like this
echo "$i<br>";
}
}
?>
But the main problem is output is showing the number serially.
//It shows
Number 3
Number 4
Number 5
Number 8
Number 9
Number 10
//But I need
Number 5
Number 4
Number 9
Number 3
Number 8
Number 10
And this takes much time to code. And it not looks so good. Sure there is a easy way out!
I am expecting something like this -
//Surely this is not right. It's just an idea.
<?php
$x = 5,4,9,3,8,10;
for ($i = 0; $i = $x; $i++) {
echo "$i<br>";
}
?>
Take this
$x = array(5,4,9,3,8,10);
foreach ($x as $i) {
echo "Number $i<br>";
}
but please, learn the basics of PHP if you really want to code in php.
JustOnUnderMillions's answer is corrent - You can even have access to key & value like this
$x = array(
"num1" => 1,
"num2" => 2,
...
...
);
foreach($x as $key => $value){
echo $key . " : " . $value . "<br>";
}
This code will divide the score until it reaches the number 5.
The $rows[score] is equal to 6600 in the database.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
echo $i = $i /2;
echo "<br>";
}
?>
This is what my browser outputs:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125
3.22265625
I don't understand why the browser output the last 3.22 - how do I stop the loop from echo out the last one that is less than 5??
Nothing wrong here the last value you get is from 6.4453125 / 2 = 3.22265625 since 6.4453125 still greater than 5
because 6 is higher than 5? so it does one more loop making $i 3.2 where the loop stops
If ($i<5) it wont go into the loop but there is no way to know until you check. $i = 6.4453125 the last time it checks, so it goes into the loop and it divides it by 2, which makes it less than 5 so it doesn't go into the loop again and stops.
I found the way to answer my own question so 3,22 will not be viewed on the page.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
$i = $i /2;
if($i >= 5) {
echo $i;
echo "<br>";
}
}
?>
Since you're dividing by two immediately before displaying the result, you want to stop your loop when $i >= (5*2) i.e. $i >= 10, not 5.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 10) {
echo $i = $i /2;
echo "<br>";
}
?>
This gives:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125