Solved

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

Posted on 2014-04-29
13
187 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
Industry Leaders: 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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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 …

740 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