I'd like to add even numbers to an array and then echo it out, here's my code but it just shows array() when I print...what am i doing wrong?
<?php
$x =88;
$numbers = array();
while ($x % 2 == 0 && $x <= 99) {
$numbers[] = "$x";
$x++;
}
print_r($numbers);
?>
You should move the "evenness" test from the while loop, and move it to a conditional within the while loop:
<?php
$x = 88;
$numbers = array();
while ($x <= 99) {
if ($x % 2 == 0) {
$numbers[] = $x;
}
$x++;
}
print_r($numbers);
?>
As you have currently written the while loop, it ends if the number is not even. You should also remove the quotes from $x when you add to the $numbers array.
Related
Using while loop to print 10-20 odd numbers on the screen, with <-> (exapmle 11-13-15-17-19, <-> not after 19).How to take the last number so as not to put a -. Thank you.
<?php
$x = 10;
while($x < 20){
$x++;
if($x % 2){
echo $x. "-";
}
}
You can push the odd values to an array and after the loop you can convert the array to a string with the implode (https://www.php.net/manual/en/function.implode.php) function.
$x = 10;
$arr = [];
while($x<20){
$x++;
if ($x%2){
$arr[] = $x;
}
}
echo implode(",", $arr);
// output will be a comma seperated string
without (helper)array
You can use rtrim() (https://www.php.net/manual/de/function.rtrim.php) funktion.
$str = "1,2,3,";
echo rtrim($str,",");
As mentioned in the comments, have a boolean variable say firstTime. If this is true, don't prepend a hyphen, else if it is false, prepend it with a hyphen.
<?php
$x = 10;
$firstTime = true;
while($x < 20){
$x++;
if($x % 2){
echo ($firstTime ? "" : "-") . $x;
$firstTime = false;
}
}
Turn the limit number into a variable and use a ternary operator to print out a dash only if $x + 1 < $limit
https://paiza.io/projects/EDj6-u-FAcYxYoR7ON_cvg
<?php
$x = 10;
$y = 20;
while($x < $y){
$x++;
if($x % 2){
echo ($x + 1 === $y) ? $x : $x. "-";
}
}
?>
Simple approach
Imagine you are printing a list of numbers from 10 to 20. If it's an even number, print the "-" symbol and if it's odd number, print the number.
<?php
$start = 10;
$end = 20;
while($start<$end)
{
$n=$start;
if($n>10)
echo ($n%2==0)?"-":$n; //the gist is here
$start++;
}
I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo 'three..'; . please enlighten me. thank you
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
if ($x = 3) {
echo 'three..' . "\n";
}
}
$y++;
}
In while loop, you should increment x variable.
If you increment y variable, it will always "true". So it will be endless loop.
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
// code
$x++;
}
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
echo $z;
}
if ($x == 3) {
echo 'three..' . "\n";
}
$x++;
}
i have loops script like this
for($i=0; $i < count($json); $i++) {
}
for example, amount of $json is "12" or anything more than 10, but i want the max of that loops is 10, but, if i use this script
for ($x = 0; $x < 10; $x++) {
}
the result will be 10, but what if the $json amount I got is less than 10? means there will be NULL results, is there any suggestion?
You can use min() (http://php.net/manual/en/function.min.php)
$count = min (count($json), 10);
for($i=0; $i < $count; $i++) {
}
It's best to do the min outside the for so that it's only done once.
$count=count($json);
if($count > 10){
$count = 10;
}
for ($x = 0; $x < $count; $x++) {
}
You can use array_slice to grab the first 10 of the array and foreach them.
This way you will always just loop 10 times at the most.
$json =[1,2,3,4,5,6,7,8,9,10,11,12,13,14];
//$json =[1,2,3,4,5,6]; //uncomment if you want to test with smaller array
$arr = array_slice($json, 0,10);
Foreach($arr as $val){
Echo $val ."\n";
}
This can also be written like this:
Foreach(array_slice($json, 0,10) as $val){
Echo $val ."\n";
}
But I spelled it out just to make it clear.
You can try the code here: https://3v4l.org/7pV3X
This is the code I have. It currently works as is, However I'm experimenting with loops and want to see it can be done with a while loop and how it would be done. With this code I can take 2 input numbers and display them, then point out all odds, add all evens, and add all the squares of the odds.
define ("B","<br/>");
$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];
if ($firstNum < $secondNum)
{
$firstNum = true;
}
elseif ($firstNum >= $secondNum)
{
$firstNum = "You didn't listen, dumb dumb!".'<br/>GO BACK';
}
echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;
echo B;
echo B;
$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;
for ($x = 0; $x < $length; $x++)
{
if (($numbers[$x] % 2) == 1)
{
$odds[$j] = $numbers[$x];
$sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
$j++;
}
else
{
$sumEven = $sumEven + $numbers[$x];
}
}
$x = 0;
$y = 0;
printf("The odd numbers between your integers are: ");
for ($x = 0; $x < $j; $x++)
{
echo $odds[$x];
echo ' ';
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
echo B;
echo B;
printf("The sum of all even numbers between your integers is: ".$sumEven);
echo B;
echo B;
printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
Here is my while loop but it seems to be infinite...
$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++;
$j++;
}
else
{
$sumEvenW = $sumEvenW + $numW[$x];
}
}
$x = 0;
$y = 0;
printf ("The odd numbers between your integers are: ");
while ($x < $j)
{
$x++;
echo $oddsW[$x];
echo "nbsp;";
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
Equivalent loops:
for ($i = 0; $i < 10; $i++) {
echo $i;
}
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
For a loop to ever finish it has to change one of the two evaluating variables. So either $x, or $lengthW would have to change during iteration. You made an if statment, in the first case you define that X increases by 1, but in the else case you do not change any variable that then has an effect on either $x, or $lengthW
Nor is there any check that sees if the else state has been reached and to catch that by either changing $x or $lengthW in a later iteration.
As such there's an infinite loop as soon as you reach the else case.
The if statement uses the same $x value as the last iteration checking the same position of the $numW, as such nothing has changed since the last iteration and you'll hit the else again, and again, and so on.
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++; //$x is increased by one, and as such, the loop will progress.
// remove this $x++ if you place it outside the if else statement.
$j++;
}
else // reached when ($numW[$x] %2) != 1
{
$sumEvenW = $sumEvenW + $numW[$x];
// No changes to $x or $lengthW as such you'll hit the else again
// this could be solved by either adding $x++; here.
}
// or by adding $x++; here
// (if you do add it here, remove it in the if case above,
// or you risk increasing it by 2 every iteration
}
I want to make a script in PHP which displays the numbers and a hyphen between the numbers. It should look like this:
1-2-3-4-5-6-7-8-9-10
I have the following script right now, but the problem is it displays a hyphen at the end of the numbers:
$x = 1;
$h = 1;
while($x <= 10) {
echo "$x";
$x++;
if($h < $x){
echo "-";
$h++;
}
}
Thanks in advance :)
There is simpler way to do this work. Use range() to create array contain numbers and use implode() to join target array with string.
echo implode("-", range(1, 10));
See result in demo
Insert the hypen before the number, except for the first number.
$x = 1;
while($x <= 10) {
if($x > 1)
echo '-';
echo $x;
$x++;
}
You can use temp string
<?php
$x = 1;
$str ='';
while($x <= 10) {
if ($str == '') {
$str = $x;
} else {
$str = $str .'-'.$x;
}
}
echo $str;
?>
This should solve the problem (I don't know why you use the h variable):
<?php
$x = 1;
$result="";
while($x <= 10) {
$result.=$x."-";
}
echo substr($result, 0,-1);
?>
or use the implode function