?
Solved

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

Posted on 2014-04-29
13
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 111

Expert Comment

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

Author Comment

by:LB1234
ID: 40029241
I did that, but then the count starts at 1 rather than 0.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 40029246
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40029247
<?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
ID: 40029252
All great solutions, but I'm trying to get this figured out using continue specifically.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40029254
Good grief!  Why use code that you've already shown does not work??  I think adding "continue" will only confuse the issue.
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 1600 total points
ID: 40029255
<?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
ID: 40029264
Thanks Guys :)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40029269
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 35

Expert Comment

by:Dan Craciun
ID: 40029275
Ray, it's probably homework...
0
 
LVL 1

Author Comment

by:LB1234
ID: 40029278
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 111

Expert Comment

by:Ray Paseur
ID: 40029284
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
ID: 40029291
Haha, thanks Ray, I just ordered it. :)  Arrives in two days with Amazon Prime.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

718 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