unemployment Posted July 1, 2011 Share Posted July 1, 2011 my state and country function don't seem to be referenced properly. <span class="dark_grey"><?php echo ($company['country'] == 'United States') ? "{$company['city']}, {state($company['state'])}" : "{$company['city']}, {country($company['country'])}"; ?></span> What is the proper syntax? Link to comment https://forums.phpfreaks.com/topic/240887-reference-function-inside/ Share on other sites More sharing options...
KevinM1 Posted July 1, 2011 Share Posted July 1, 2011 I don't believe you can use the ternary operator in this way. Have you tried simply: if($company['country'] == 'United States') { echo "{$company['city']}, " . state($company['state']); } else { echo "{$company['city']}, " . country($company['country']); } Link to comment https://forums.phpfreaks.com/topic/240887-reference-function-inside/#findComment-1237355 Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2011 Share Posted July 1, 2011 Although I think you're sacrificing readability for compactness, and creating an absolute nightmare for anyone who has to edit the code in the future (including yourself), you can't use a function call in a quoted string. You need to concatenate it. <span class="dark_grey"><?php echo ($company['country'] == 'United States') ? "{$company['city']}, " . state($company['state']) : "{$company['city']}, " . country($company['country']); ?></span> Link to comment https://forums.phpfreaks.com/topic/240887-reference-function-inside/#findComment-1237360 Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2011 Share Posted July 1, 2011 Where there's a will, there's a way (I don't recommend that you actually use this method as you will never be able to read and troubleshoot what your code is doing) - <?php $state = 'state'; // variable function name $country = 'country'; // variable function name ?> <span class="dark_grey"><?php echo ($company['country'] == 'United States') ? "{$company['city']}, {$state($company['state'])}" : "{$company['city']}, {$country($company['country'])}"; ?></span> Link to comment https://forums.phpfreaks.com/topic/240887-reference-function-inside/#findComment-1237383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.