lotrfan Posted October 4, 2007 Share Posted October 4, 2007 Hi all, I know that PHP automatically sets the data types so you don't worry about it normally. In my case, when using trigonometric functions -- it automatically sets the data type for the variables as "radians". I know about rad2deg and deg2rad functions but that won't work. My users input a degree and so I want the type to be immediatly set as "degrees" NOT "radians". How do it set the variable type to "degrees" at the exact time the user inputs the value in the form? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/ Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 Technically they're being treated as integers or floats and being processed as radians, but I know what you mean ;p. You could do something like this: <?php if(isset($_GET['number']) && is_numeric($_GET['number'])) { $number = $_GET['number']; $dr = (isset($_GET['dr']) && strtolower($_GET['dr']) == 'rad') ? 'rad' : 'deg'; //if dr is set and it's rad, the type is rad, else the type is deg. $nr = ($dr == 'rad') ? $number : deg2rad($number); //number in radians $nd = ($dr == 'deg') ? $number : rad2deg($number); //number in degrees //Note: you wouldn't technically need to keep both numbers stored since you can get from one to the other as needed, but it's just an example ;p echo 'You entered '.$nr.' radians or '.$nd.' degrees.<br /><br />'; echo 'Here is some basic information about your number and the trig ratios:<br />'; echo 'Sin(number) = ' . sin($nr) . '<br />'; //feed the number in radians since that's what the sin, cos and tan functions expect echo 'Cos(number) = ' . cos($nr) . '<br />'; echo 'Tan(number) = ' . tan($nr) . '<br />'; } ?> <form action="" method="GET"> Enter a number: <input type="text" name="number" value=""><br /> <input type="radio" name="dr" value="deg" CHECKED /> Degrees<br /> <input type="radio" name="dr" value="rad" /> Radians<br /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361395 Share on other sites More sharing options...
Crew-Portal Posted October 4, 2007 Share Posted October 4, 2007 ^^ Above should work. In PHP you dont have to declare variable types like in Visual Basic: Dim intage As Integer intage = "16" ^^ Above was in VB6 but in PHP you can just: $intage = "16"; Ya! Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361397 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 ^^ Above was in VB6 but in PHP you can just: $intage = "16"; Um, that would be.... $intage = 16; Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361401 Share on other sites More sharing options...
lotrfan Posted October 4, 2007 Author Share Posted October 4, 2007 Sorry, could you clarify a bit -- I'm REALLY a beginner. To:thorpe and N350CA; $intage = "16"; Isn't that just a variable? And to corbin; You are saying that the trig functions are hard-wired to use radians? and could you explain your code a bit? >_< ....sorry... I'm just confused about this line: $nr = ($dr == 'rad') ? $number : deg2rad($number); Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361410 Share on other sites More sharing options...
roopurt18 Posted October 4, 2007 Share Posted October 4, 2007 If you note the documentation for the trig functions, such as sin(), the documentation states the expected argument is in radians. It is hardwired. If you're users are entering a value as degrees, then you must convert using deg2rad(). I want the type to be immediatly set as "degrees" NOT "radians" "degree" and "radian" are not data types in any language that I know of as they're already perfectly encapsulated as floats or ints. Degrees, radians, miles between two points, your age, they're all just numbers. It is the context in which you use them that gives them meaning. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361416 Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 Ahhh sorry: That line is short for: if($dr == 'rad') { $dr = $number; } else { $dr = deg2rad($number); } (condition) ? true action or return : false action or return; As for the just a variable part.... Yes, that's just a variable, but all variables have a type. In stricter languages (PHP is like insanely loose when it comes to data types), variables must be defined as a certain datatype. For example, in C++: int x = 5; int y = 2; int z = x/2; //z would be 2 because it would round down to integer 2 string str; str = 4; //This would most likely error at compile string otherstr = "4"; int mult = otherstr*2; // would most likely error at compile But in PHP, most of those would be legal (well... They might flag some warnings or come out realy weird, but that's it... No fatal errors). Actually, in PHP, the forced int on the multiplication one would probably be set to 2 (or it should be anyway). Oh, and in PHP, datatypes are set like this: $somevar = (int) 1; //1 $somevar = (int) 2.222452; //2 $somevar = (string) "Hi!"; //hi $somevar = (int) "Hi!"; //0 $somevar = (boolean) true; //true... can't remember if it's (bool) or (boolean) $somevar = (boolean) 1; //true $somevar = (float) 3.14; //3.14 $somevar = (int) $somevar; //3 Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361419 Share on other sites More sharing options...
lotrfan Posted October 4, 2007 Author Share Posted October 4, 2007 THANK YOU! This is starting to make some sense. You guys really are great about helping out beginners. Out of any forum on the nets, you guys are head and shoulders above the rest -- you really know what you are talking about. Thanks again for taking the time to help me out. ***one vary last thing, though...what exactly does this mean: $intage = "16"; I really understand the variable settings now, though. Many Thanks to corbin and ruupert. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361427 Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 Once compiled, PHP will interpret that as the script saying (this is the simple version before someone corrects me) take the string value "16 and store it in the variable $intage. $intage would actually be stored as a string when declared like that just as $intage = "Corbin" would be. It would go something like this in a real example: $var = "15"; echo $var; //echos the string "15" echo $var*2; /* Ok... PHP does a few things here.... It will convert $val to an integer (or float... not sure how PHP handles that.... I guess it would see if it needed to be a float.). Then, it will take the float/int value 15 and multiply it by two. Depending on how strict the PHP engine is, it might then convert this result to a string before echo'ing it out. */ //note: if var was a non numeric string such as "hi", then it would be treated as 0 in math //Here's the opposite: $var = 15; //ok this is stored as an integer. echo $var; //echos 15 once 15 is converted to a string (I'm not sure how strictly the actual PHP engine its self is, but I would imagine this happens, anyway ;p) $val = $var*5; //This would be integral 75 $val2 = $var*2.5; //This would be a float echo $val2; //The float would be output (maybe as a string once again... not sure) An integer is any number that is integral (called 'counting numbers' by some... 1 2 3 4 5 so on). A float number (floating point) is a number that can contain a decimal to put it plainly. 1.0 2.5 1.00001 2315325345342534512351235.7 Integers, floats, so on actually have maximum sizes, but I don't feel like going there ;p. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361434 Share on other sites More sharing options...
lotrfan Posted October 4, 2007 Author Share Posted October 4, 2007 Thanks again corbin. I actually understand when you explain things...that doesn't happen very often, LOL. I'll mark this topic as SOLVED, and I hope any other beginners with variable type questions can look this up. Kudos to all, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361439 Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 No problem ;p. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361443 Share on other sites More sharing options...
roopurt18 Posted October 4, 2007 Share Posted October 4, 2007 What corbin is hinting at is called automatic type conversion, which is to say that PHP will automatically convert from one data type to another where it is able to. An example <?php // Here we declare an integer, it is an integer because it is a numeric value, not // contained within quotes, and has no decimal $int = 5; // Here we declare a float (or double), it is a float (double) because it is numeric, not // contained within quotes, but it does have a decimal $float = 5.0; // Here we declare a string, it is a string because it is enclosed in double quotes $str = "5.0"; ?> I will use an analogy to gloss over this next part. Let's consider three types of engines: a lawn mower motor, a car engine, and a jet engine. All three of those are the same in that they're each an engine. But I shouldn't have to prove to you that underneath the hood they are all vastly different in organization and complexity. The same thing occurs with those three variables above. They are the same in that in some manner they all represent the value 5, but how the computer stores and recognizes them internally is as varied as the three engines. However, it is quite often the case in programming that we have a value stored in one format and want the equivalent value in a different format. Converting from one format to another is called type conversion. There are some conversions that can be handled automatically. For instance, if we continue with the variables I defined above: echo $int * $str; Here we are using the multiplication operator. Multiplication is well understood in math and is applied to numbers; thus the multiplication operator expects two numeric values. However we are giving it a numeric value and a string value. The PHP interpreter will automatically attempt to convert the string value to a number for us. Since the string is the value "5.0", the conversion is easy and PHP will calculate 5 * 5 and print out 25. There are many values that we can convert automatically: int => floating point int => bool There are some conversions that aren't straight forward: "Hello World" => integer ??? In these cases PHP picks a default value, such as FALSE, null, zero, an empty array, etc. The default picked depends on the originating data type and the desired converted data type. Anyways, to make a long story short, you don't really care what the actual data type of a variable in PHP is for 99.99% of the time. You just care that it is a valid value for how you intend to use it. For instance, values contained in $_POST and $_GET are actually strings. Yet you can take a value from $_POST and perform math on it without performing any manual conversion. Pretend you had written a function that expected a numeric input. a_func(5); a_func("5"); function a_func($val){ if(!is_int($val)){ return FALSE; } // else do something } Notice that the function is called twice, once with an int argument and another time with a string argument. Because the function is checking if the parameter is of type integer, the second call (with the string) will return false, even though the string could have been used numerically. The better version of the function would change the call from is_int() to is_numeric(). Hope that helped some. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-361463 Share on other sites More sharing options...
lotrfan Posted October 4, 2007 Author Share Posted October 4, 2007 Quite the explanation, ruupert! That actually does make sense -- and I really hadn't grasped that concept perfectly until now. I saw scripts with " " around numbers and I didn't have any idea what they were... My thanks go out again for explaining things fully. It is only when we understand things completely that we can find new ways to use them. You guys are awesome. Quote Link to comment https://forums.phpfreaks.com/topic/71762-solved-how-to-set-variable-data-types/#findComment-362069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.