There are two things I am trying to accompolish with PHP types & arrays. Hope somebody here can help me: 1) You can use $a = integer; PHP: in PHP. That helps you to recapture the purpose of the variable when you or others visit the code later. I am trying to initialize the above variable, i.e. something like $a = integer =5 ; PHP: or $a = integer(5); PHP: All these give parse errors. Is it possible to accompolish what I am trying??? 2) You can return an array for indexing with both associative & otherwise, with mysql functions. I need to provide a similar functionality in my library, where users can enter elements in an array using keys, but retrieve it later using numeric indexes in the order in which they were entered. So, is this feasible???
php isn't like c or other languages when it comes to data types. it just simply doesn't care what data type it is. for your example $a = 5; would be what you want. and to answer question #2, look into the array_flip() function. www.php.net/array-flip
In one word: No PHP is very loose (in my opinion too loose) with data types. PHP doesn't really care whether you create a string, integer, float or double, as long as you do math operations with them, php will consider them as integers or doubles. If you want to make sure a certain value is of the type int, there are 2 options: 1) you use the === operator instead of the == in an if. The === operator will check the value AND the type: if ( "123" === 123 ) // returns false if ( 123 === 123 ) // returns true if ( "123" == 123 ) // return true PHP: 2) you can use the is_int function to check if the variable is actually of the integer type: $var = 123; if ( is_int ( $var ) ) // returns true $var = "123"; if ( is_int ( $var ) ) // returns false PHP: It is possible if you use array_values: $array = array ( "abc" => 123, "def" => 456); var_dump ( $array ); $vals = array_values ( $arary ); var_dump ( $vals ) ; PHP: will output something like this: array(2) { ["abc"]=> int(123) ["def"]=> int(456) } array(2) { [0]=> int(123) [1]=> int(456) } Code (markup):
That's fine... But that will not let me use $array[$index]; where $index can be a numeric index or the original key...
try this: $array = array ( "abc" => 123, "def" => 456); $new_array = array_merge ( $array, array_values ( $array ) ); PHP: now you can either use $array["abc"] or $array[0] to get 123