Solved

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

Posted on 2014-04-29
13
188 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 110

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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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 110

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 400 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 110

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 110

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

729 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