Factors of a number in PHP

Find all factors of a positive integer

A factor divides a number with no remainder. This straightforward version is easy to read and is appropriate for beginner exercises.

<?php
\ = 50;
\ = [];

for (\ = 1; \ <= \; \++) {
    if (\ % \ === 0) {
        \[] = \;
    }
}

echo implode(', ', \);
?>
Factors are numbers which divide the number evenly.
Factors of 12 are 1, 2, 3, 4, 6, 12
Factors ( of a number ) are numbers which when divide the number they leave 0 as reminder.

Factors using loop

We will start from 1 and continue upto half of the number. There can't be any factor of the number from half value till the number. So there is no need to check ( upper limit of the loop ) beyond the half value of the number.
We may include the number itself as all numbers are divided by itself.
Math modulus returns us the reminder of a division, for all factors the reminder is always 0.
\=50; // change this number
for(\=1;\<=(\/2); \++){
	if(\%\ == 0 )
		echo \" $i,\" ;
}
echo  $n1 ;
Output is here
1, 2, 5, 10, 25,50

Using sticky form

Learn more about sticky form here. User will enter a number and the factors of the number will be displayed on submit.
\=\Array['n1'] ?? '';
echo \"<form method=POST action=''>
<input type=text name=n1 value=''>
<input type=submit value=Submit><br>\";
for($i=1;$i<=($n1/2); $i++){
	if($n1%$i == 0 )
		echo \" $i,\" ;
}
echo  $n1 ;

DEMO using sticky form

Enter a number and then Submit