Jump to content

Reference Function inside {}


unemployment

Recommended Posts

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

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>

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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.