Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 0's problem
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
beno




PostPosted: 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'];
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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?
beno




PostPosted: 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??
=)
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
beno




PostPosted: 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())
Insectoid




PostPosted: Tue Oct 28, 2008 8:49 pm   Post subject: RE:0\'s problem

I have no idea, my example was based on general programming.
btiffin




PostPosted: 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.
bash:

$ php -r '$a = "12"; echo $a + 3 . "\n";'
15

Cheers
jeffgreco13




PostPosted: 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...

php:


<?php
$a=$_REQUEST[num];

if ($a*1!=0) {
echo $a;
} else {
$a=0;
echo $a;
}
?>



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.
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: 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
beno




PostPosted: 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 Razz
anyways, thx ppl =D
btiffin




PostPosted: 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
code:

$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 Razz
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
code:

echo "000"
000

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.
code:

echo (integer) "0";
0
echo "000" + 0;
0
echo "000000000000" + 0;
0
echo (int) "000000000000000000000";
0
echo (float) "000000.00000";
0
echo (float) "00000.00E100";
0

And once again with strings (which echo won't show, so you can't really tell)
code:

$a = "0000000";
$a = (string)(int)$a;
echo $a;
0

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.
code:
[code]....source code....or other text[/code]
and we all get to see pretty (and indented) source code.

I actually changed your quote, the code
code:
$a[i]
causes compsci to enter italics mode because of the bracket i bracket, it will stay in italics until it sees
code:
[/i]
to end italics mode. Had you used b as your loop variable, your post would be bold.

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. Wink

Cheers
Edits; tag typos;
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: