Break in while statement in PHP not working

I know the error is prolly obvious, but i cannot figure it out.  I want to create a numbered div for each div that stops on 10, and puts 10 in the last div, like the rest.  This code gives me one div with the number 1 in it.  Could someone explain in detail why this is happening? Thanks :)

<?php

$x = 1;

while ($x <= 10) {
	echo "<div>" . $x . "</div>";
	$x++; 
	if ($x = 10) {
	break;	
	}
		
}



?>

Open in new window

LVL 1
LB1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You need to use double = for comparison:

if ($x == 10) {
0
Chris StanyonWebDevCommented:
You may be better off using a for loop instead:

for ($x=1; $x < 10 ; $x++) {
	 echo "<div>" . $x . "</div>";
}

Open in new window

0
Julian HansenCommented:
A for is probably better but if you want to use a While  then you could do it like this
while ($x <= 10) {
	echo "<div>" . $x++ . "</div>";
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

LB1234Author Commented:
Thanks Julian!  To all other respondents, your solutions resulted in a div that ended in "9".  Julian's was the only one that ended with 10, but thank you!!!
0
Julian HansenCommented:
ChrisStanyon's solution will work with this change
for ($x=1; $x <= 10 ; $x++) {
	 echo "<div>" . $x . "</div>";
}

Open in new window

0
Chris StanyonWebDevCommented:
Your original code ended with a div containing 9!!
0
LB1234Author Commented:
And it was incorrect, Chris. :)  I clearly stated I needed the code to end with DIVs that numbered to 10.
0
Chris StanyonWebDevCommented:
Your question stated that you only got 1 div and you asked why it was happening. I told you, because you didn't have the double ==

Everything else was just a suggestion on how to do it better.

Hey ho!
0
Julian HansenCommented:
@LB1234 - Chris is correct. If you had changed if ($x = 10) to ($x == 10) your original code would have worked as required. In addition Chris pointed out a better way of doing what you wanted to do.

Split points here would have been in order as my post was just riding on the back of Chris'.

Recommend you request attention and ask a mod to re-open the question and then include Chris in the points split.
0
LB1234Author Commented:
Requesting attention.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.