php error, help!

Discussion in 'PHP' started by IanT, Feb 26, 2010.

  1. #1
    Im learning php, and trying to understand why I am getting this error when I use the following code for changing the type of a variable with settype() in php5.

    The code is:

    <?php
    $undecided=3.14;
    print gettype($undecided);//double
    print"--$undecided<br>"; //3.14
    settype($undecided, string);
    print gettype($undecided); //string
    print"--$undecided<br>"; // 3.14
    settype($undecided, integer); 
    print gettype($undecided);// integer
    print"--$undecided<br>"; // 3
    settype($undecided, double);
    print gettype($undecided); //double
    print"--$undecided<br>"; // 3.0
    settype($undecided, boolean);
    print"--$undecided<br>"; // 1
    ?>
    
    Code (markup):

    The output is:



    What am I doing wrong? Or is this what its supposed to be spitting out?

    Thanks for the help!
     
    IanT, Feb 26, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    set type doesn't get used much in practice.

    The easiest way to convert to a string is just wrap quotes around it.
    $undecided = "{$undecided}";
    PHP:
    As for integers and doubles, well I usually let the database do the conversion when it saves them.

    FWIW I always send non-integer numbers down to the database as a string anyway ie
    $sql = "insert into `test` (`id`,`qty`) values (null,'{$qty}')";
    PHP:
    Putting the quotes around ensures it is read properly, it seems.

    For boolean you just test if it's zero or not. zero = false, 1 or more is true.
     
    sarahk, Feb 26, 2010 IP
  3. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #3
    The second parameter of the settype() function is a string, so you'd better wrap it in single or double quotes
     
    nabil_kadimi, Feb 27, 2010 IP
  4. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or you can use the keywords themselves

    
    $sky_is_blue = true;
    
    PHP:
     
    Rory M, Feb 27, 2010 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #5
    Sure, but in his example the variable he's testing has a value of 3.14
     
    sarahk, Feb 27, 2010 IP
    Rory M likes this.
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    To turn a string into an integer.

    Use the (int) type cast.
     
    danx10, Feb 27, 2010 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #7
    except that he has a float he's trying to turn into other things...
     
    sarahk, Feb 27, 2010 IP
  8. IanT

    IanT Well-Known Member

    Messages:
    503
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #8
    I think one of the errors mught be related to the "double" conversion... you know how on a html editor when certain types are inserted into the document they are different colors etc... well double never changed a color, but the rest of the types that I had in those brackets were... any thoughts on that??

    thanks for all the help so far! :)

    ...feel like I am learning a lot!
     
    IanT, Feb 28, 2010 IP
  9. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Just a thought, you're using print rather than echo. I'm not quite sure that it follows the same convention, you may wish to try it with brackets (though I'm really not sure that it makes a difference and MAMP isn't online for me to check) Give it a go:

    
    <?php
    $undecided=3.14;
    print gettype($undecided);//double
    print("--$undecided<br>"); //3.14
    settype($undecided, string);
    print gettype($undecided); //string
    print("--$undecided<br>"); // 3.14
    settype($undecided, integer); 
    print gettype($undecided);// integer
    print("--$undecided<br>"); // 3
    settype($undecided, double);
    print gettype($undecided); //double
    print("--$undecided<br>"); // 3.0
    settype($undecided, boolean);
    print("--$undecided<br>"); // 1
    ?>
    
    PHP:
    It may do absolutely nothing, but in practise I'd always tend to use Echo over print ;)
     
    Rory M, Feb 28, 2010 IP
  10. IanT

    IanT Well-Known Member

    Messages:
    503
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #10
    Okay Sweet, I will try that!! Thank you!!
     
    IanT, Feb 28, 2010 IP