如何将echo部分中的选项值存储到php变量中?

the problem in storing option value from echo part into PHP variable

$var=$_POST[select_tag_name]

$query="SELECT subject.`SubjectName` 
        FROM `subject` 
            LEFT JOIN `class-sub-info` USING(SubjectId) 
        WHERE `ClassID`=$classid";

    $result=mysqli_query($con,$query);

    if(mysqli_num_rows($result) > 0) 
    {

        while($row=mysqli_fetch_assoc($result))
        {
            echo "<option value=".$row["SubjectId"].">".$row["SubjectName"]."</option>";
        }   

    }

expecting an easy way to store that value into a php variable

Use Compensation Operator (.) to combine the output.

First create a variable like $result and assign empty value. after that use Compensation Operator to assign the output into variable.

 $var=$_POST[select_tag_name]
    //create a variable $result and assign empty value
    $subject= '';
    $query="SELECT subject.`SubjectName` 
            FROM `subject` 
                LEFT JOIN `class-sub-info` USING(SubjectId) 
            WHERE `ClassID`=$classid";

        $result=mysqli_query($con,$query);

        if(mysqli_num_rows($result) > 0) 
        {

            while($row=mysqli_fetch_assoc($result))
            { 
                //Use the Compensation Operator(.) to assign the output in variable
                $subject.="<option value=".$row["SubjectId"].">".$row["SubjectName"]."</option>";
            }   

        }

//At last display that variable
    echo $subject;