kentonator Posted April 17, 2019 Share Posted April 17, 2019 Ok, to be fair, I’m brand new to PHP and just trying to piece this together for a friends website. The idea of this code is if a value of 1 is given in the data entry it would return house and so on. But right now, even if it’s a 3 it’s just returning house instead of Mobile Home. I’ve tried everything I know and googled for ever, but if you don’t know the right language to google, it’s impossible. I have a feeling this is a simple fix and I just don’t know it. function property_structure_type ($data){ $data = array("1", "2", "3", "4", "5", "6", "7"); if (in_array("1", $data)) { return "House"; } if (in_array("2", $data)) { return "Cabin"; } if (in_array("3", $data)) { return "Mobile Home"; } if (in_array("4", $data)) { return "Apartment"; } if (in_array("5", $data)) { return "Condominium"; } if (in_array("6", $data)) { return "Townhome"; } if (in_array("7", $data)) { return "None"; } Quote Link to comment Share on other sites More sharing options...
benanamen Posted April 17, 2019 Share Posted April 17, 2019 (edited) It' simple really. You are providing an array, it matches the 1 and returns back to the caller so the rest of the if's are not processed. Try this... <?php declare(strict_types=1); function property_structure_type ($data) { if (in_array("1", $data)) { return "House"; } if (in_array("2", $data)) { return "Cabin"; } if (in_array("3", $data)) { return "Mobile Home"; } if (in_array("4", $data)) { return "Apartment"; } if (in_array("5", $data)) { return "Condominium"; } if (in_array("6", $data)) { return "Townhome"; } if (in_array("7", $data)) { return "None"; } } echo property_structure_type ([2]); Edited April 17, 2019 by benanamen Quote Link to comment Share on other sites More sharing options...
Barand Posted April 17, 2019 Share Posted April 17, 2019 Of course - no matter what value you pass, "1" is in the array so it returns "House". Functions exit as soon as a value is returned. Perhaps function property_structure_type ($data) { $types = [ 1 => 'House', 2 => 'Cabin', 3 => 'Mobile Home', 4 => 'Apartment', 5 => 'Condominium', 6 => 'Townhome' ]; return isset($types[$data]) ? $types[$data] : 'None'; } echo property_structure_type(3); //-> Mobile home Quote Link to comment Share on other sites More sharing options...
kentonator Posted April 17, 2019 Author Share Posted April 17, 2019 3 hours ago, Barand said: Of course - no matter what value you pass, "1" is in the array so it returns "House". Functions exit as soon as a value is returned. Perhaps function property_structure_type ($data) { $types = [ 1 => 'House', 2 => 'Cabin', 3 => 'Mobile Home', 4 => 'Apartment', 5 => 'Condominium', 6 => 'Townhome' ]; return isset($types[$data]) ? $types[$data] : 'None'; } echo property_structure_type(3); //-> Mobile home This worked perfectly, thanks a ton! Quote Link to comment 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.