isSelected (function)

Last updated: Mon, Apr 17, 2023 11:02 am
Return to Knowledgebase


Used to check if two values are equal, and if they are, it returns the specified output (default: "selected='selected'")




# Parameter Data Type Required Description
1 $one Yes The first value to compare.
2 $two Yes The second value to compare.
3 $output = No The output to return if the two values are equal. Default is "selected='selected'".


Data Type Description of Returned Data

Further Documentation:

Suppose we have an array of colors and we want to create a select element in an HTML form with the options dynamically generated based on the array elements. We want to pre-select the option that matches the user's preferred color, which is stored in a variable called $preferred_color. We can use the isSelected() function as follows:

$colors = ['Red', 'Green', 'Blue', 'Yellow'];

echo "";

In this example, the isSelected() function checks if the current $color value is equal to $preferred_color. If they are equal, the function echoes the output "selected='selected'", which sets the "selected" attribute on the option element.