Hi guys. After getting a lot of insight for my last post, I've decided to post another problem I've been having.

I'm attempting to create a function that will transform lowercase words into Words With The First Letter Capitalized, but I'm doing something wrong as I keep getting error messages.

Here's my code:

<?php
function converter($upper, $string){
$upper =  ucwords($string);
$string = "some words.<br />";
}

print converter("some words.", $string );
?>

The only catch here is that I have to display everything with the

print converter("some words.", $string );

function. I'm not exactly sure how to deal with arguments expertly so I'm thinking I have issues there. Help? And thanks again.

    If that is all you want to do, then there's really no need to create a new function, since you can just do:

    print ucwords("text to convert");
    // or //
    $string = "text to convert";
    print ucwords($string);
    

    But, if you do want to create a function (maybe to add some other functionality than just the ucwords() stuff), just pass the string as one argument, then return the converted value:

    function converter($string)
    {
       $string = ucwords($string);
       return $string;
    }
    
    // USAGE:
    $test = "text to convert";
    print converter($test);
    

      Thank you so much for your quick reply. 🙂
      What you gave IS a much better way of doing it, and is how I would have went about it as well. However, my assignment specified that I would have to call the function with

      print converter("some words.", $string );
      

      Which is why I'm a bit stuck... I'm not used to thinking like a programmer yet I guess. :quiet:

        It is not clear to me then what the actual requirement is. What are the two function arguments supposed to represent?

          Hi. That's also where I'm kind of confused. =\ The problem states:

          1. Create a function called converter() with 2 arguments
          2. first argument is title; second is a string
          3. the function will be called like this: print converter("title", $string);

          And the output should look like:

          the original string: hello brown cow

          converted string: Hello Brown Cow

          I'm having trouble making a function that would be called with:
          print function("title", $string);

          I know that I just have to work backwards somehow, but everything I've tried has resulted in an error.

            Yep, doesn't seem to explain what the "title" is for. 😕

            Pending clarification, you could just ignore it. 🙂

            function converter($title, $string)
            {
               return ucwords($string);
            }
            

            Seems silly to me, as if part of the problem description was omitted, or else it's just plain wrong.

              My guess is that the next part of the assignment will call for you to accommodate different values for the first argument, e.g., now you're doing "title case", but later you will also implement for lower case and uppercase, perhaps when "lower" and "upper" are passed as arguments respectively.

              That said, if my hunch is correct, then note that we are more likely to create special constants for use to select the operation, rather than pass strings.

                laserlight;10966367 wrote:

                My guess is that the next part of the assignment will call for you to accommodate different values for the first argument, e.g., now you're doing "title case", but later you will also implement for lower case and uppercase, perhaps when "lower" and "upper" are passed as arguments respectively.

                That said, if my hunch is correct, then note that we are more likely to create special constants for use to select the operation, rather than pass strings.

                Ah...that makes a lot of sense. (I never think of it as "title case", just as "ucwords() case". 🙂 )

                  Write a Reply...