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!
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.
The second parameter of the settype() function is a string, so you'd better wrap it in single or double quotes
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!
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