PHP Code Resources Part 2

localman

New Member
PHP:
### The while Loop
##############################################################################################
<html>
<body>

<?php
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?>

</body>
</html>
### The do...while Statement
##############################################################################################

<html>
<body>

<?php
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<=5);
?>

</body>
</html>

### The for Loop
##############################################################################################
<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "<br />";
  }
?>

</body>
</html>

### The foreach Loop
##############################################################################################
<html>
<body>

<?php
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br />";
  }
?>

</body>
</html>
PHP:
### Create a PHP Function
##############################################################################################
<html>
<body>

<?php
function writeName()
{
echo "Kai Jim Refsnes";
}

echo "My name is ";
writeName();
?>

</body>
</html>

### PHP Functions - Adding parameters
##############################################################################################
<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");

function writeName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}

echo "My name is ";
writeName("Kai Jim",".");
echo "My sister's name is ";
writeName("Hege","!");
echo "My brother's name is ";
writeName("St?le","?");

?>

</body>
</html>
### PHP Functions - Return values
##############################################################################################

<html>
<body>

<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}

echo "1 + 16 = " . add(1,16);
?>

</body>
</html>
PHP:
# add the parameters passing by reference
<html>
<body>

<?php
/*
 * reference operator in the function declaration.
 */
function div1($dividee,$divider, &$quotient, &$remainder)
{
$quotient = (int)($dividee / $divider);
$remainder=$dividee % $divider;
return $dividee / $divider;
}

$result = div1(3,5, $quot, $rem);
echo "3 / 5 = " . $result . " : quotient=" . $quot . " ; remainder=" . $rem;

echo "<br />";

/*
 * reference operator when calling the function
 */
function div2($dividee,$divider, $quotient, $remainder)
{
$quotient = (int)($dividee / $divider);
$remainder=$dividee % $divider;
return $dividee / $divider;
}

$result = div2(3,5, &$quot, &$rem);
echo "3 / 5 = " . $result . " : quotient=" . $quot . " ; remainder=" . $rem;

?>

</body>
</html>
 
آخرین ویرایش:

جدیدترین ارسال ها

بالا