Solved

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

Posted on 2014-04-29
13
191 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

636 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