nimb0z Posted February 28, 2008 Share Posted February 28, 2008 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 More sharing options...
trq Posted February 28, 2008 Share Posted February 28, 2008 Perameters being passed into a function need a name so you can use them within said function. eg; <?php function foo($a) { echo $a; // the $a here would make no sense unless it was passed in as an argument. } foo('bar'); ?> Link to comment https://forums.phpfreaks.com/topic/93471-str-and-each/#findComment-478872 Share on other sites More sharing options...
blurrydude Posted February 28, 2008 Share Posted February 28, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.