1

I have a block of text with ternary operators something like this:

$base ="This option". ($enhances ==  1? 'enhances' : 'does not enhance') ."your". ($ability==1? 'gaming' : 'coding') ." abilities.";

Which should output the text "This option [enhances / does not enhance] your [gaming / coding] abilities", depending on the variables set.

Original text is long and there are multiple words to change depending on the variable. I would like to keep the base text as it is and do the modifications by changing the variable values used in the ternary operators. This is because the base text is bound to change many times, and I would like to change it only to one place.

I would then like to create a html table using only variables to change the base text:

$enhances=1;
$ability=0;
echo "
<tr> 
    <td><div class=\"factors\">Enhances = 1<br> Ability = 0</div></td>
    <td><div class=\"message\">$base</div></td>
</tr>
";
$ability=1;
$enhances=1;
echo "  
<tr> 
    <td><div class=\"factors\">Enhances = 1 <br> Ability = 1</div></td>
    <td><div class=\"message\">$base</div></td>
</tr>

";

This how ever outputs something like this:

Enhances = 1
Ability = 0
This option does not enhance your coding abilities.
Enhances = 1 
Ability = 1
This option does not enhance your coding abilities.

Is there a way to re-evaluate the $base variable after setting the variables? What is the correct way to do this kind of text formatting with PHP? I am a PHP newbie, so I'm not sure what can be done with PHP and what not. Or even if my terms are correct. Hopefully I was descriptive enough.

Similar post, but with only one variable inside a variable: PHP: Evaluating variable in string, and then changing value of variable

Community
  • 1
  • 1
Juha-Matti
  • 13
  • 2

4 Answers4

2

Try this one:

$enhances=1;
$ability=0;
echo '
<tr> 
    <td><div class="factors">Enhances = 1<br> Ability = 0</div></td>
    <td><div class="message">'.getStatement($enhances, $ability).'</div></td>
</tr>';

$ability=1;
$enhances=1;

echo '  
<tr> 
    <td><div class="factors">Enhances = 1 <br> Ability = 1</div></td>
    <td><div class="message">'.getStatement($enhances, $ability).'</div></td>
</tr>';

function getStatement($enhances, $ability) {
  return "This option". ($enhances ==  1? ' enhances ' : 'does not enhance') ."your". ($ability==1? ' gaming ' : ' coding') ." abilities.";
}
Mubashar Iqbal
  • 398
  • 7
  • 18
1

you could define base as function, i.e.,

function fn($enhances, $ability){
    return "This option". ($enhances ==  1? 'enhances' : 'does not enhance') ."your". ($ability==1? 'gaming' : 'coding') ." abilities.";
}

and then use it in echo statements as

echo "something ".fn($enhances, $ability)."something else";
ewcz
  • 12,819
  • 1
  • 25
  • 47
0

the simplest thing you could do is wrap this into a function.

function write_message($ability, $enhances) {
   $message = 'This option ' . ($enhances ? 'enhances' : 'does not enhance') . ' your ' . ($ability ? 'gaming' : 'coding') . ' abilities';
   return $message;
}

And then use it as follows

$ability=1;
$enhances=1;

echo "
<tr> 
    <td><div class=\"factors\">Enhances = 1 <br> Ability = 1</div></td>
    <td><div class=\"message\"> " . write_message($ability, $enhances) . "</div></td>
</tr>
";
Bazcovic
  • 3
  • 3
0

It seems a bit overkill to me to create a new function every time you want to format some different string. PHP has the ability to do that already. You can use printf to achieve this like so:

<?php

$enhanceText = [
    'enhances',
    'does not enhance',
];
$abilityText = [
    'gaming',
    'coding',
];

$base = "This option %s your %s abilities<br />";

$enhances = 0;
$ability = 0;
printf($base, $enhanceText[$enhances], $abilityText[$ability]);

$enhances = 1;
$ability = 0;
printf($base, $enhanceText[$enhances], $abilityText[$ability]);

$enhances = 0;
$ability = 1;
printf($base, $enhanceText[$enhances], $abilityText[$ability]);

$enhances = 1;
$ability = 1;
printf($base, $enhanceText[$enhances], $abilityText[$ability]);

Outputs:

This option enhances your gaming abilities
This option does not enhance your gaming abilities
This option enhances your coding abilities
This option does not enhance your coding abilities
LeonardChallis
  • 7,759
  • 6
  • 45
  • 76