Learning PHP and MySQL

Example 05-05 Creating a capitalize function with a default parameter $each

<?php
//  Capitalize  a  string  or  the  first  letter  of  each  word 
function  capitalize(  $str,  $each=TRUE  )
{
  
//  First,  convert  all  characters  to  lower  case
  
$str  =  strtolower($str);
  if  (
$each  ===  TRUE)  {
    $str  
=  ucwords  ($str);
  
}  else  {
    $str  
=  strtoupper($str);
  
}
  
echo  ("$str  <br>");
}
capitalize
("hEllo  WoRld!");
echo  (
"Now  do  the  same  with  the  each  parameter  set  to  FALSE.<br>");
capitalize("hEllo  WoRld!",FALSE);
?>

Posted by on 08/03 at 08:19 AM

Next entry: Example 05-06 Modifying capitalize to take a reference parameter

Previous entry: Example 05-04 Using the string capitalization functions

<< Back to main