To answer your question, you can use style to make your text colored.
<p> Your Favorite Color is <span style=“color: <?= $colors[strtoupper($your_color)]; ?> “><?= $your_color ?> </p>
However, you should never trust user input!
So, for example, use html entities when you assign your variable
<?php
$your_name = htmlentities($_POST['yourName']);
$your_color = htmlentities($_POST['yourColor']);
For assigning the color, putting the color values in an array is an effective way to decouple user input, but the problem is that unless you check for invalid keys, you might get a color you’re not expecting and generate a warning. What would happen if the user types “chartreuse” as their favorite color?
<?php print $color[‘chartreuse’]; ?>
There is no key named “chartreuse”, so php will print out a warning. You don’t want your users to see that. Do this instead
$colors = [
'red' => '#ff0000',
'blue' => '#0000ff',
'green' => '#008000',
'yellow' => '#ffff00',
'purple' => '#800080',
'orange' => '#ffa500',
'brown' => '#a52a2a',
'black' => '#000000',
'white' => '#ffffff',
'pink' => '#ffc0cb',
];
// initialize
$color_value = ‘’;
// conditionally set based on user input
if(in_array(stringtolower($your_color), $colors)) {
$color_value = $colors[$your_color];
}
What if you want to tell the user that their choice of color is unsupported? Assign color name at the same time as color value:
$color_value = ‘’;
$color_name = ‘invalid color’;
if(in_array(stringtolower ($your_color), $colors)) {
$color_value = $colors[$your_color];
$color_name = $your_color;
}
Now your HTML looks like
<p> Hello <?=$your_name ?></p>
<p> Your Favorite Color is <span style=“color: <?= $color_value ?>”><?= $color_name?> </p>
A better way to change the font color, though, is to use css:
<style >
.blue { color: #0000ff}
.green { color: #008000}
... etc
</style >
Then, all you have to do is specify the class for your colored text; you don’t even have to worry about $color_value:
<p> Your Favorite Color is <span class=“<?= stringtolower ($color_name) ?>”><?= $color_name?> </p>
——-
Update:
Method 1:
<?php
$colors = [
'red' => '#ff0000',
'blue' => '#0000ff',
'green' => '#008000',
'yellow' => '#ffff00',
'purple' => '#800080',
'orange' => '#ffa500',
'brown' => '#a52a2a',
'black' => '#000000',
'white' => '#ffffff',
'pink' => '#ffc0cb',
];
$submitted = FALSE;
if($_GET[‘submit’]) {
$submitted = TRUE;
$color_value = ‘’;
$color_name = ‘invalid color’;
$your_color = htmlentities($_GET[‘color’]);
$your_name = htmlentities($_GET[‘name’]);
if(in_array(stringtolower ($your_color), $colors)) {
$color_value = $colors[$your_color];
$color_name = ucwords($your_color);
}
// note: if you used post (i.e., to change stored data) you would take action here and then REDIRECT to this or another page. This is called the post/redirect/get pattern)
}
?>
<html>
<head>
</head>
<body>
<?php if($submitted): ?>
<p> Hello <?= $your_name ?></p>
<p style=“color: <?= $color_value ?>” >Your Favorite Color is <?= ucwords($color_name) ?> </p>
<?php endif; ?>
<form method=“get”>
<div>
<label>Your name:
<input type=“text” name=“name”>
</label>
</div>
<div>
<label>Favorite color:
<input type=“text” name=“color”>
</label>
</div>
</form>
</body>
</html>
Method 2:
<?php
$submitted = FALSE;
if($_GET[‘submit’]) {
$submitted = TRUE;
$color_name = htmlentities($_GET[‘color’]);
$your_name = htmlentities($_GET[‘name’]);
// note: if you used post (i.e., to change stored data) you would take action here and then REDIRECT to this or another page. This is called the post/redirect/get pattern)
}
?>
<html>
<head>
<style>
.blue { color: #00f; }
.red { color: #f00; }
.green { color: #008000; }
.yellow { color: #ff0; }
</style>
</head>
<body>
<?php if($submitted): ?>
<p> Hello <?= $your_name ?></p>
<p class=“<?= $color_name ?>” >Your Favorite Color is <?= ucwords($color_name) ?> </p>
<?php endif; ?>
<form method=“get”>
<div>
<label>Your name:
<input type=“text” name=“name”>
</label>
</div>
<div>
<label>Favorite color:
<select name=“color”>
<option value=“blue”>Blue</option>
<option value=“red”>Red</option>
<option value=“green”>Green</option>
<option value=“yellow”>Yellow</option>
</select>
</label>
</div>
</form>
</body>
</html>
strtoupperalong with the string to access it thus the undefined index.fuchiastrtolowerstill receiving the same error.Your favorite color is <span style="color: <?php echo $colors[strtolower($your_color)]; ?>"><?php echo strip_tags($your_color) ?></span>$colorsarray, and so it dutifully prints out 0000ff. If you want to have the html show it as blue text, you need to know how to make an element’s text style be blue. Ask the google how to change html font color.