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
Share on other sites

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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.