acctman Posted May 8, 2009 Share Posted May 8, 2009 can someone assist me with the code below? Unexpected T_as error the countries.php file $countries['US'] = array(name => 'United States'); $countries['AF'] = array(name => 'Afghanistan'); code.. if ($en['mm_country'] != US || CA) { global $countries; include 'modules/members/countries.php'; $countries as $key => $value; $key = $en['mm_country']; $en['mm_coname'] = htmlentities($value['name'],ENT_QUOTES); echo $en['mm_coname']; } Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/ Share on other sites More sharing options...
AmandaF Posted May 8, 2009 Share Posted May 8, 2009 Are you trying to do a foreach loop? I don't think "as $key => $value" works outside of the context of a foreach loop. Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829662 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Put quotes around name. $countries['US'] = array('name' => 'United States'); $countries['AF'] = array('name' => 'Afghanistan'); Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829666 Share on other sites More sharing options...
acctman Posted May 8, 2009 Author Share Posted May 8, 2009 Are you trying to do a foreach loop? I don't think "as $key => $value" works outside of the context of a foreach loop. no not doing a foreach just need to grab the country name for $key Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829684 Share on other sites More sharing options...
acctman Posted May 8, 2009 Author Share Posted May 8, 2009 Put quotes around name. $countries['US'] = array('name' => 'United States'); $countries['AF'] = array('name' => 'Afghanistan'); this is already formated in a file, and works fine without the quotes when used elsewhere on the site. Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829686 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Then use normal array access $key = "US"; $country = $countries[$key]; Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829688 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 if ($en['mm_country'] != US || CA) { Are US and CA strings or constants? Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829692 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Doesn't matter. If there's no constant defined, PHP will assume string (and throw a Notice) Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829694 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Doesn't matter. If there's no constant defined, PHP will assume string (and throw a Notice) I didn't know that. Thanks! One more thing, what does this line do? $countries as $key => $value; Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829697 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 Throws an error obviously Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829698 Share on other sites More sharing options...
thebadbad Posted May 8, 2009 Share Posted May 8, 2009 if ($en['mm_country'] != US || CA) { Means: if ($en['mm_country'] != US) is true or if (CA) is true. I.e. it will always be true. I guess you're looking for if (in_array($en['mm_country'], array(US, CA))) { Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829711 Share on other sites More sharing options...
acctman Posted May 8, 2009 Author Share Posted May 8, 2009 Then use normal array access $key = "US"; $country = $countries[$key]; when i do that the result is just Array. the code needs to be something like this, but i'm not sure how to code it correctly. if ($en['mm_country'] != US) || ($en['mm_country'] != CA) { global $countries; include 'modules/members/countries.php'; $key = $en['mm_country']; $country = $countries(htmlentities($key['name'],ENT_QUOTES)); echo $country; } Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829718 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 What does "code it correctly" mean? You haven't told us what you want. Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829722 Share on other sites More sharing options...
acctman Posted May 8, 2009 Author Share Posted May 8, 2009 What does "code it correctly" mean? You haven't told us what you want. oh sorry i thought i did, i want to connect to countries.php and grab the country name. $en['mm_country'] holds the $key abbervation of the country. so if AF is supplied then when I echo $country the return value should be Afghanistan. also when grabbing the country name htmlentities needs to be used to strip the quotes $countries['US'] = array(name => 'United States'); $countries['AF'] = array(name => 'Afghanistan'); Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829726 Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 $countries(htmlentities($key['name'],ENT_QUOTES)); ^ $countries is an array. Use brackets, not parentheses. $country = htmlentities($countries[$key['name']], ENT_QUOTES); Like that? Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829734 Share on other sites More sharing options...
Mchl Posted May 8, 2009 Share Posted May 8, 2009 More like $country = htmlentities($countries[$key]['name'], ENT_QUOTES); Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829737 Share on other sites More sharing options...
acctman Posted May 8, 2009 Author Share Posted May 8, 2009 $countries(htmlentities($key['name'],ENT_QUOTES)); ^ $countries is an array. Use brackets, not parentheses. $country = htmlentities($countries[$key['name']], ENT_QUOTES); Like that? nothing was returned, no error. Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829748 Share on other sites More sharing options...
acctman Posted May 8, 2009 Author Share Posted May 8, 2009 More like $country = htmlentities($countries[$key]['name'], ENT_QUOTES); that worked thanks. Link to comment https://forums.phpfreaks.com/topic/157387-unexpected-t_as-error/#findComment-829751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.