Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8712899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:12:32+00:00 2026-06-13T05:12:32+00:00

Below is a function with a parameter that (in this example case) can contain

  • 0

Below is a function with a parameter that (in this example case) can contain either ‘cat’, ‘dog’ or ‘bird’. The function contains of a form and I want to be able to set the right radio button as checked, depending on what option the variable $animal contains.

How could this be achieved in an elegant way?

public function SetAsChecked($animal) {
    // $animal = 'cat', 'dog' and 'bird'

    $html = "
            <form method='post'>
                <p>Option</p>
                <input type='radio' name='animals' value='1'> Cat<br />
                <input type='radio' name='animals' value='2'> Dog<br />
                <input type='radio' name='animals' value='3'> Bird<br />
            </form>";

    return $html;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:12:34+00:00Added an answer on June 13, 2026 at 5:12 am

    Create an array of options instead of repeating HTML:

    public function SetAsChecked($animal){
    
        $options = array(
            '1' => 'Cat',
            '2' => 'Dog',
            '3' => 'Bird'
        );
    
        $optionsStr = '';
    
        foreach($options as $value => $name)
        {
            //$checked = $animal == $name ? 'checked' : ''; // use this if you want case sensitive comparison
            $checked = strcasecmp($animal, $name) == 0 ? 'checked' : ''; // case insensitive comparison
            $optionsStr .= "<input type='radio' name='options' value='$value' $checked />$name<br />";
        }
    
        $html = "
                <form method='post'>
                    <p>Option</p>
                    $optionsStr
                </form>";
    
        return $html;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable in a package ( rec in this case) that needs
How can I get rid of this error and other while compiling the below
The below example shows how I'm building the query string that will return a
I want to write a function that behaves differently depending on its type parameter.
Here is my code below that works, but passess only the first parameter. But
From Test.html I include a test.js that runs function d() . This function has
I want to create a function in an external javascript file that I can
When I put the below function into leon repl , I get java.lang.IllegalArgumentException: Wrong
I am using the below function of javascript to enable and disable the radio
Anyone experienced exception using below function? tdse.GetObject(tmpFolderWebDavURL, EnumOpenMode.OpenModeView, null, XMLReadFilter.XMLReadAll) as Folder; Seems if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.