Jump to content

$str and $each


nimb0z

Recommended Posts

Hi,

 

I've just stared learning PHP5 with the "OReilly Learning PHP and MySQL 2nd Edition" book. I think I understood things until the 'parametets' section. I don't get whats $str and $each in the following example:

 

<?php 
// Capitalize a string or only the first letter of each word 
function capitalize( $str, $each=TRUE ) { 
  // First, convert all characters to lowercase or non-first-word letters may remain capitalized 
  $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 echo parameter set to FALSE.<br>"); 
capitalize("hEllo WoRld!",FALSE); 
?>

 

Whats are $str and $each? Are they predefined values for string and each word resp? So that means I can't use them as variable names, like $str = "something" ?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/93471-str-and-each/
Share on other sites

simply put, the capitalize function requires two pieces of information, the string that you want to look at, and a variable for each letter capitalized. In this example you're passing the $str string as the string you want the function to look at, and the $each string is actually passing the letters that are capitalized.

Try echoing the $str and $each strings and look at what is being passed to the function. You may need to use a FOR WHILE statement for the $each, but I could be wrong on that. Anything you want to pass the function you create will need to have variables passed to them in this way.

Link to comment
https://forums.phpfreaks.com/topic/93471-str-and-each/#findComment-478875
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.