Type Juggling in PHP

Discussion in 'PHP' started by r_honey, Jun 24, 2007.

  1. #1
    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???
     
    r_honey, Jun 24, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    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
     
    ansi, Jun 24, 2007 IP
  3. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Checked it out... That's not what I am looking at...
    Anyways, thanx for the reply.
     
    r_honey, Jun 25, 2007 IP
  4. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $a = 0;
    var_dump($a);
    
    PHP:
    Gives you: int(0)

    or
    
    $a = (int) $a;
    var_dump($a);
    
    PHP:
    Gives you: int(0) :p :)
     
    MMJ, Jun 25, 2007 IP
  5. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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):
     
    UnrealEd, Jun 25, 2007 IP
  6. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That's fine...
    But that will not let me use $array[$index];
    where $index can be a numeric index or the original key...
     
    r_honey, Jun 25, 2007 IP
  7. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    UnrealEd, Jun 25, 2007 IP
  8. r_honey

    r_honey Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanx...
    :D
     
    r_honey, Jun 25, 2007 IP