Web Hosting Talk







View Full Version : Implode??


Carp
05-24-2005, 02:03 PM
$number is an array. Lets say in the array, there is a 1, a 2, and a 3. I want to change $number array to just an int so the new value of number would be 123. I tried this:


$number = implode ("", $number);


Will this work?

Thanks.

silhouette
05-24-2005, 02:25 PM
foreach($number as $each)
$gluedNumber.=$each;

will this work??

Nyture
05-24-2005, 02:31 PM
carp: yes thats how you would do it.

to sil: yes, but only as long as you set $gluedNumber to "", otherwise it might create a error because ur trying to concatenate a string with something that doesnt exist.

silhouette
05-24-2005, 02:47 PM
Originally posted by Nyture
to sil: yes, but only as long as you set $gluedNumber to "", otherwise it might create a error because ur trying to concatenate a string with something that doesnt exist. u mean $gluedNumber has to be initialized to "" before the foreach loop??

Thank you for pointing out my mistake :)

hiryuu
05-24-2005, 05:32 PM
PHP will magically create it as an empty string but, if you have all warnings turned on (like you should during development), it will point out the issue. It isn't nearly the problem it was when user-input was assigned to global variables, but it's still poor form.

Burhan
05-25-2005, 03:49 AM
This is what you should use :


<?php

$test = array(1,2,3);
echo intval(implode("",$test));
?>

error404
05-25-2005, 08:34 AM
The documentation seems to be unclear...fyrestrtr, do you know if there's any functional difference between intval() and an explicit cast?

God I hate how PHP pollutes the namespace with duplicate & otherwise useless functionality...it's bad enough there's no namespaces and no objects in the built in library...

Here's to hoping it does something more elaborate than

function intval($val = 0)
{
return (int)($val);
}

Burhan
05-25-2005, 08:48 AM
Not as far as I know. The only different is that with intval(), you can specify the base that you want to have it converted to.

Googled
05-26-2005, 01:33 AM
He only asked if his php line was good.

Which is working just fine ;)