Andy_Kemp Posted June 21, 2016 Share Posted June 21, 2016 <?php declare(strict_types = 1); function test(string $username, int $age, float $price) : array { return array('Username' => $username, 'Age' => $age, 'Price' => $price); } $_get = array( 'username' => '', 'age' => '', 'price' => '', ); $_get = array_merge($_get, $_GET); print_r(test($_get['username'], $_get['age'], $_get['price'])); I am new with php7 So when i looked those examples i see test('test_username', 20, 20.01) but int and float are absolutely useless with $_POST, $_GET, Its always error. WITH int - must be of the type integer, string given. WITH float - must be of the type float, string given Quote Link to comment https://forums.phpfreaks.com/topic/301372-php7-scalar-type-declarations/ Share on other sites More sharing options...
Jacques1 Posted June 21, 2016 Share Posted June 21, 2016 And your question is what exactly? It's obvious that you cannot pass raw request parameters to a function that expects numeric types. So? Why would you want that? There should be a component in your application which validates the raw input, converts it when necessary and then passes the converted data on to other methods and functions. You shouldn't have $_GET and $_POST all over the place, anyway. Quote Link to comment https://forums.phpfreaks.com/topic/301372-php7-scalar-type-declarations/#findComment-1533861 Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2016 Share Posted June 23, 2016 Yeah. 100% what @Jacques1 said. You seem to be unaware that everything sent through HTTP Request Method is sent as a string. So you have "20" not 20, "102.99" not 102.99 etc. This isn't usually a problem - as long as you are aware of it so that you can handle the data type as and when you need to. Search the php manual for "typecasting" and you should quickly be able to get some use out of your numbers. Quote Link to comment https://forums.phpfreaks.com/topic/301372-php7-scalar-type-declarations/#findComment-1533914 Share on other sites More sharing options...
NotionCommotion Posted June 23, 2016 Share Posted June 23, 2016 Looks like you example was not meant to be implemented, but only to educate yourself, true? While print_r() is helpful to quickly view data, var_dump() gives you more information. Try the following, you will will see what is going on $_get = array_merge($_get, $_GET); var_dump($_get); Quote Link to comment https://forums.phpfreaks.com/topic/301372-php7-scalar-type-declarations/#findComment-1533929 Share on other sites More sharing options...
Andy_Kemp Posted June 27, 2016 Author Share Posted June 27, 2016 Looks like you example was not meant to be implemented, but only to educate yourself, true? While print_r() is helpful to quickly view data, var_dump() gives you more information. Try the following, you will will see what is going on $_get = array_merge($_get, $_GET); var_dump($_get); Yes thats true Quote Link to comment https://forums.phpfreaks.com/topic/301372-php7-scalar-type-declarations/#findComment-1534041 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.