Several examples of how PHP algorithms are collated involve the following.
PHP bubbling
PHP Two-way
PHP to calculate prime
PHP multiplication Table
Example of a php bubbling method
Copy Code code as follows:
PHP bubbling from small to large
Function Maopao (& $arr)
{
if (!empty ($arr))
{
for ($i =0; $i <count ($arr); $i + +)
{
if ($arr [$i]> $arr [$j])
{
Start swapping
$temp = $arr [$i];
$arr [$i] = $arr [$j];
$arr [$j] = $temp;
}
}
}
return $arr;
}
}
PHP Binary Lookup code example
Copy Code code as follows:
Two-Way Search
function Erfenfa ($a, $arr)
{
Print_r ($arr);
if (!empty ($a) &&!empty ($arr))
{
$start = 0;
$end = count ($arr)-1;
$i = 0;
while ($start <= $end) {
$i + +;
$step = Floor ($end/2);
if ($a = = $arr [$step])
{
Print_r ($arr [$step]);
return $a;
}
if ($a > $arr [$step])
{
$start = $step;
}
if ($a < $arr [$step])
{
$end = $step;
}
}
}
}
PHP Calculate Prime-Calculates the prime number between A and B. code example
Copy Code code as follows:
PHP Calculate Prime-Calculates the prime number between A and B.
function Sushu ($a, $b)
{
if (!empty ($a) &&!empty ($b))
{
if ($b < $a) return;
$temp = Array ();
for ($i = $a; $i <= $b; $i + +)
{
$j = Intval (sqrt ($i));
$flag = true;
if ($i <=3)
{
$temp [$i] = $i;
}else
{
for ($x =2; $x <= $j; $x + +)
{
if ($i% $x ==0)
{
$flag = false;
Break
}
}
if ($flag)
{
$temp [$i] = $i;
}
}
}
return $temp;
}
}
PHP Output Multiplication Table-Recursive code example
Copy Code code as follows:
PHP Output Multiplication Table-recursive
function Digui ($a, $step)
{
if ($a > $step) return;
if (!empty ($a) &&!empty ($step))
{
for ($i =1; $i <= $a; $i + +)
{
echo $i. ' * ' $a. $a * $i. " \ t ";
if ($i = = $a) echo '
‘;
}
$a = $a + 1;
Digui ($a, $step);
}
}
PHP Output multiplication Table-Circular code example
Copy Code code as follows:
PHP Output Multiplication Table-loop
function Chengfa ($a, $step)
{
if (!empty ($a) &&!empty ($step))
{
for ($i = $a; $i <= $step; $i + +)
{
for ($j =1; $j <= $i; $j + +)
{
echo $j. ' * ' $i. $i * $j. " \ t ";
if ($i = = $j) echo '
‘;
}
}
}
}