Solved

PHP While statement that increments and echoes from 0 to 10, but skips the number 5

Posted on 2014-04-29
13
177 Views
Last Modified: 2014-04-29
This is what I came up with but 5 is still printed.  Any ideas?  Thanks.

<?php 


$x = 0;

while ($x < 10) {
	echo $x;
	
	if ($x == 5) {
	continue;	
	}

	$x++;
	
	
}

?>

Open in new window

0
Comment
Question by:LB1234
  • 5
  • 5
  • 3
13 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
The test for "5" should be before the echo statement.
0
 
LVL 1

Author Comment

by:LB1234
Comment Utility
I did that, but then the count starts at 1 rather than 0.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
As with most things in computer programming, there are a lot of paths that can lead to the same goals.

Try it this way:
http://iconoun.com/demo/temp_lb1234.php

<?php // demo/temp_lb1234.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28422075.html

$x = 0;

while ($x <= 10)
{
    if ($x != 5) echo $x;
    $x++;
}

Open in new window

If you're new to PHP and want to get a foundation in how this stuff works, this article can help you get started and stay grounded.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
<?php 

$x = 0;

while ($x < 10) {
	if ($x != 5) {
	echo $x;	
	}
	$x++;
}
?>

Open in new window


LE: Ray was faster this time :)

HTH,
Dan
0
 
LVL 1

Author Comment

by:LB1234
Comment Utility
All great solutions, but I'm trying to get this figured out using continue specifically.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Good grief!  Why use code that you've already shown does not work??  I think adding "continue" will only confuse the issue.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 34

Accepted Solution

by:
Dan Craciun earned 400 total points
Comment Utility
<?php 

$x = 0;

while ($x < 10) {
	if ($x == 5) {
		$x++;
                continue;
	}
        echo $x;
	$x++;
}
?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:LB1234
Comment Utility
Thanks Guys :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Note that with the use of "continue" you can wind up with code that duplicates itself.  For example, note the use of two iterations of $x++ in the most recent code example.  While you certainly can do it this way, it just does not make sense to me to complicate a code example that already works.

But that aside, here are some other ways of thinking about the problem:

<?php // demo/temp_lb1234.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28422075.html

$arr = array_merge(range(0,4), range(6,10));
foreach ($arr as $num) echo $num;

echo '<br>' . PHP_EOL;

$arr = range(0,10);
unset($arr[5]);
foreach ($arr as $num) echo $num;

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
Ray, it's probably homework...
0
 
LVL 1

Author Comment

by:LB1234
Comment Utility
It's not homework, just trying to learn and understand.  In general, I understand that efficiency is important, but in programming there are of course many ways to skin a cat, and I'd like to be familiar with many approaches to the same problem.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
If you could choose only one book to learn PHP, I would recommend this one.  A month or two with it will save you a year or two of trial and error, I promise!
http://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166/

Get the latest version and when a new version is released, throw the old one away (or give it to one of your enemies) and replace it with the new copy.
0
 
LVL 1

Author Comment

by:LB1234
Comment Utility
Haha, thanks Ray, I just ordered it. :)  Arrives in two days with Amazon Prime.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Save signature to file 7 44
Need some help setting up PDFLib 4 26
mysql left join sentence 7 19
Help cleaning out CSS 2 22
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now