Computer Science Canada 0's problem |
Author: | beno [ Tue Oct 28, 2008 8:00 pm ] |
Post subject: | 0's problem |
when the user input "00" or "000" or "0000", ect.. how can you ask the computer to treat them as only one "0"?? do it in php... $a = $_REQUEST['a']; |
Author: | Insectoid [ Tue Oct 28, 2008 8:03 pm ] |
Post subject: | RE:0\'s problem |
convert to a string, check if every element in the string equals zero, then initialize to 0 and convert back to integer? |
Author: | beno [ Tue Oct 28, 2008 8:23 pm ] |
Post subject: | RE:0\'s problem |
i have just learned php for a few days.. how to convert a int to string, then convert it back?? =) |
Author: | Tony [ Tue Oct 28, 2008 8:25 pm ] |
Post subject: | RE:0\'s problem |
"00" _is_ a string. Simply typecast it into the integer. Assuming you are trying to get rid of the leading zeros from a string. |
Author: | beno [ Tue Oct 28, 2008 8:31 pm ] |
Post subject: | RE:0\'s problem |
<?php $str = "000"; $num = (int)$str; ?> is that how you convert it? but, is there any method for php that can check characters? (like java, uses charAt()) |
Author: | Insectoid [ Tue Oct 28, 2008 8:49 pm ] |
Post subject: | RE:0\'s problem |
I have no idea, my example was based on general programming. |
Author: | btiffin [ Tue Oct 28, 2008 8:52 pm ] | ||
Post subject: | Re: 0's problem | ||
Yes; (integer) cast will also work, the intval() function may be worth looking into. And sometimes there is is no need to cast at all as PHP is rife with type coercion.
Cheers |
Author: | jeffgreco13 [ Wed Oct 29, 2008 2:56 am ] | ||
Post subject: | Re: 0's problem | ||
theres many ways to do it really, its pretty much as simple as this though...
The only number that multiplies with 1 to get 0 is 0. No matter how many 0000000000000000 you put. Notice the $_REQUEST treats num as a number (int or real). Try it with decimals too it works. |
Author: | btiffin [ Wed Oct 29, 2008 7:39 am ] |
Post subject: | Re: RE:0\'s problem |
beno @ Tue Oct 28, 2008 8:31 pm wrote: .. but, is there any method for php that can check characters? (like java, uses charAt()) In case you still want to know about that feature; Check into substr(str, start, len) for one of the many many PHP string functions to will facilitate that. Cheers |
Author: | beno [ Thu Oct 30, 2008 9:31 pm ] |
Post subject: | RE:0\'s problem |
i did a for loop $check= strlen ($a); $num=0; for ($i=0;$i<$check;$i++) { if ($a[i]==0) $num++; } if($num==$check) { $a=0; echo $a; } if($num!=$check) echo $a; i kinda understand a little bit what you guys talked about and thank god, my teacher didn't require that much anyways, thx ppl =D |
Author: | btiffin [ Fri Oct 31, 2008 12:53 am ] | ||||||||||||||
Post subject: | Re: RE:0\'s problem | ||||||||||||||
beno @ Thu Oct 30, 2008 9:31 pm wrote: i did a for loop
i kinda understand a little bit what you guys talked about and thank god, my teacher didn't require that much anyways, thx ppl =D I'm sorry Beno; that is way too much code for the task at hand. PHP can be far more concise. There is no need to count the zeros in a string, unless the job is to count the zeros. Mathematically, PHP sees 0, "0", "00", "000", "0000" ... as the same value, zero. Even "0000.0000" is 0, as is "0000.00E100" (zero applied to 10 to the 100th power) is zero to PHP. Strings and numbers
Treated as a string, PHP sees a string. "000" is not zero to PHP when treated as a string. Only when treated mathematically will PHP autoconvert the string of "00" to the value 0. So let autoconversion do that work for you.
And once again with strings (which echo won't show, so you can't really tell)
And the 0 there is actually "0", not the number 0 And just for future reference, (there is no real way of knowing this until someone shows you); when posting code to compsci.ca use the bbcode code tags. Using the square bracket and the word code and a close square, then /code inside square brackets, to return to normal posting.
I actually changed your quote, the code
And have fun with PHP. Built for Personal Home Pages, then "elevated" to a new name and status with recursive PHP: Hypertext Processor, a lot of nifty systems have been built with PHP ... and yet many programmers love to hate it. Which to me, makes it great fun. Cheers Edits; tag typos; |