User Contributed Notes: settype
sidi@angband.org
06-Oct-1999 10:46
I'm new to php (version 3.0.12), but seems to me having settype causes the first element added to array to be assigned to the [1] index instead of the [0] index if you're not specifying the index.
That might account for some of the count() confusion, unless I'm confused myself.
<pre>
<?php
#try commenting the following line
settype($foo, "array");
$foo[] = "amy";
$foo[] = "bobby";
$foo[] = "cathy";
echo 'Count: ' . count($foo) . "\n";
echo '$foo[0]: ' . $foo[0] . "\n";
echo '$foo[1]: ' . $foo[1] . "\n";
echo '$foo[2]: ' . $foo[2] . "\n";
echo '$foo[count($foo)]: ' . $foo[count($foo)] . "\n";
?>
</pre>
jeffg@NOcounterintuitive.orgSPAM
27-Oct-1999 02:46
It's worth noting that one can neither <I>settype()</i> nor type-cast a variable as a long. The workaround for this seems to be to use <I>pack()</i>.
tboothby@felfel.com
10-Apr-2000 07:33
settype() for some reason increases the initial internal counter for the index of an array if you use
settype($foo, "array");$foo[]='bar';
'bar' will be stored in $foo[1]. furthermore, if you use
reset($foo);$foo[]='barr';
'barr' will be stored in $foo[1] again!
i'm using version 3.0.12 on linux 2.2.5-22
ns@canada.com
05-May-2000 04:38
This settype() behaviour seems consistent to me. Quoting two sections from the manual:
"When casting from a scalar or a string variable to an array, the variable will become the first element of the array: "
<pre>
2 $var = 'ciao';
3 $arr = (array) $var;
4 echo $arr[0]; // outputs 'ciao'
</pre>
And if (like your code above) you do a settype on an empty variable, you'll end up with a one element array with an empty (not unset!) first element. So appeanding to it will start appending at index 1. As for why reset() doesn't do anything:
"When you assign a value to an array variable using empty brackets, the value will be added onto the end of the array."
It doesn't matter where the array counter is; values are added at the end, not at the counter.
support@mastebyte.de
11-Jul-2000 12:39
Note that settype($string, "integer") will set $string to 0 if $string contains any lettery, but the function call will be TRUE
roy@tuginternet.com
14-Jul-2000 07:01
If you settype a double (aka a "real") to an inetger it will truncate the number. I.e.
$some_num=235.68;
settype ($some_num, "integer")
will yield $some_num==235;
This is handy as I haven't been able to find a basic trunc() or truncate() function.
slushpupie@hotmail.com
22-Jul-2000 09:07
in PHP3 converting a string to any number results in the value becoming 0. To check if a string represents a number try this:
<PRE>
$test = "0001";
$testcp = $test;
settype($testcp,"double");
if (strval($testcp) == $test) {
echo("\$test is a number");
} else {
echo ("\$test is not a number");
}
</PRE>
white@network-lynx.net
07-Aug-2000 12:30
Guys,
Are there any way to work with
"unsigned int" ? I wasn't able to
find this.
justin@lacomputersolutions.com
14-Aug-2000 11:11
Building a computer quote generator for a client. Each of their customers gets a slightly different markup. hence the markup variable that everything is multiplied by before displaying to the user. Well I need 2 decimal places, all I can get is 1 or none. Any suggestions?