Forums

custom php switch case

mdma 19 Sep, 2015
Hello maybe simple PHP question but not for me:
I have a case switch code how to leave the complete html in echo statement intact even there are /, ", '. <?php, statement inside without escaping..
i have read about the solutions in php manual
1.)single quotes
2.)escaping
3.)Nowdocs

but what is recomended in chronoforms?
<?php
$lang = \JFactory::getLanguage();
$tag = $lang->getTag();
switch ( $tag ) {
  case 'de-DE':
  default:
   echo '<a id="g_link" href="http://google.com">g_link</a>';
    break;
  case 'fr-FR':
   echo '<a id="g_link" href="http://google.com">g_link</a>';
    break;
  case 'en-GB':
.
.
.


<?php<?php
$lang = \JFactory::getLanguage();
$tag = $lang->getTag();
switch ( $tag ) {
  case 'de-DE':
  default:
  { ?>
    <a id="g_link" href="http://google.com">g_link</a>
<?php } 
   break;
  case 'fr-FR':
 { ?>
    <a id="g_link" href="http://google.com">g_link</a>
<?php } 
.
.
.
?>


<a id="g_link" href="http://google.com">g_link</a> is only for example ...
GreyHead 19 Sep, 2015
Answer
Hi mdma,

Either method will work - I'd probably use the first one but with the " and ' switched around as this lets you insert PHP $variables.
<?php
$lang = \JFactory::getLanguage();
$tag = $lang->getTag();
switch ( $tag ) {
  case 'de-DE':
  default:
   echo "<a id='g_link' href='http://google.com/index.php?var={$de_var}'>g_link</a>";
    break;
  case 'fr-FR':
   echo "<a id='g_link' href='http://google.com/index.php?var={$fr_var}'>g_link</a>";
    break;
  case 'en-GB':
.
.
?>
Note that the {$some_var} syntax only works if the statement is in double quotes "".

Bob
mdma 19 Sep, 2015
that is very good!
thank you for this knowledge about the using variables!!!
This topic is locked and no more replies can be posted.