GoDaddy Posted June 5, 2006 Share Posted June 5, 2006 I'm currently reading php & mysql web development. I was wondering, how do you know which kind of parameter that pass(type .. int, string, array, etc)Let's say you have included some library that contains the following functionfunction foo($prices){ for($i=0;$i<count($prices);$i++) { echo $prices[$i]; }}How do you know that the function must take an array ?A user could easily pass a string or an int .... isn't there a problem there?EDIT:Another question ... how do you know what type of variable is returned from a function call? you might want a function to return an array of int .. but instead ... it returns a string ...?? Quote Link to comment https://forums.phpfreaks.com/topic/11202-php-noob-question/ Share on other sites More sharing options...
trq Posted June 5, 2006 Share Posted June 5, 2006 Yes...there could be a problem there. Unfortuanately (or fortunately) though, php is loosly typed and as so doesn't force these things.The best thing to do is make your functions check inputted values. eg;[code]function foo($prices){ if (!is_array($prices)) { exit(); } for($i=0;$i<count($prices);$i++) { echo $prices[$i]; }}[/code]Of course this doesn't help a great deal because there is no error thrown. If your using php5 you can however throw exceptions. eg;[code]function foo($prices){ if (!is_array($prices)) { throw new Exception("Expected array as first argument"); } for($i=0;$i<count($prices);$i++) { echo $prices[$i]; }}[/code]To answer your other question. You dont. This is what makes php so simple, it can also make it quite painfull. Its not like C, there are no real data types. Thats just how it is. Quote Link to comment https://forums.phpfreaks.com/topic/11202-php-noob-question/#findComment-41914 Share on other sites More sharing options...
GoDaddy Posted June 5, 2006 Author Share Posted June 5, 2006 thanks thorpe for clearing this out ... i'm c++, java, c# developer .. so those questions came to me. But it seems like php5 is pure OOP now, really great for me.thanks again Quote Link to comment https://forums.phpfreaks.com/topic/11202-php-noob-question/#findComment-41923 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.