0

I have a piece of PHP code (in a Magento installation) which produces a list of URLs, I need to put a variable with each of these, for example the first URL could be assigned to $curGBP, the second to $curEUR,and so on... I've managed to produce what the variable name needs to be so it's just putting the final part of the jigsaw together.

I currently have this code:

<?php foreach ($this->getCurrencies() as $_code => $_name): ?>
<?php echo $this->getSwitchCurrencyUrl($_code) ?>
<?php echo "\$cur"?><?php echo $_code ?><br />
<?php endforeach; ?>

Which produces this output:

../GBP/uenc/SESSIONID/ $curGBP  
../EUR/uenc/SESSIONID/ $curEUR
../USD/uenc/SESSIONID/ $curUSD
../AED/uenc/SESSIONID/ $curAED

What do I need to add to the foreach section to assign the URL to a variable?

Note: SESSIONID is a long string which I've replaced in this example for neatness.

1 Answer 1

1
<?php foreach ($this->getCurrencies() as $_code => $_name): ?>
<?php ${"cur$_code"} = $this->getSwitchCurrencyUrl($_code) ?>
<?php endforeach; ?>

That should assign the currency url to the variable names you want to.

For more information if you don´t know how it works, have a look to the variable variables: http://php.net/manual/en/language.variables.variable.php

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to add the "cur" prefix but already modified it on the answer. Anyway the hard part is to find out that variable variables exists xD I learned about them a while after I started programming php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.