1 digit numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cgroup
Forum Newbie
Posts: 13
Joined: Tue Jun 03, 2003 11:51 pm
Location: PA

1 digit numbers

Post by cgroup »

I'm having trouble getting single digit numbers to be 1 digit. This is what I have:

Code: Select all

<?php
$num_right = 0;
if (expression) {
$num_right .= $num_right + 1;
} elseif (expression) {
$num_right .= $num_right + 1;
}
?>
And I get:

Code: Select all

01
or
02
I'd like the code to generate 1 digit numbers like 1 instead of 01. Any suggetions? :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

.= is a concatenation operator.. it always makes strings.

you want += 1 I think...
Last edited by feyd on Tue Jun 01, 2004 3:02 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

or just $num_right++

(is the if/elseif construct really necessary? You're doing the same thing in both clauses)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
cgroup
Forum Newbie
Posts: 13
Joined: Tue Jun 03, 2003 11:51 pm
Location: PA

Post by cgroup »

Thanks! I have no idea what I was thinking. :?

This is for a lottery type game where you pick four numbers between 1 and 9 so I used the if/else to check each number to see if it's right and if so add one to the total number right, but it probably would be better to use switch. Not sure.
Post Reply