adam1984 Posted May 18, 2009 Share Posted May 18, 2009 First off, thanks for all the help on my last post about a week ago. I'm still trucking along in my PHP book and have another exercise i'm almost done with, but have a question concerning the last sentence in the exericse. I'll first show the exercise in question, then I will show the code I have developed for it. Please take a look at it and let me know what you think! EXERCISE: Create a function that works with two arguments. The first argument should be a username, and the second should be an email address. Use case conversion functions to capitalize the first letter of the username. Convert the email address to lowercase characters and check that it contains the @ sign. If you can't find the @ character, return false; otherwise, return an array containing the converted arguments. Test the function. My Code: <?php function test($username, $email) { if (strstr($email, "@")){ print ucwords($username); print "<br />"; print strtolower($email); } else { print "No dice, FALSE"; } } test("adam1984", "[email protected]"); ?> My code runs fine, however I'm not sure if this is what they're asking for since it wants me to return an array containing the converted arguments??? What do you all think? I thank you in advance, Sincerely, AdMAN Quote Link to comment https://forums.phpfreaks.com/topic/158620-help-with-an-array-please/ Share on other sites More sharing options...
wildteen88 Posted May 18, 2009 Share Posted May 18, 2009 Rather then print the username/email when you have converted them assign the to a variable, eg $username, $email then use the following to return an array return array($username, $email) Change print ucwords($username); print "<br />"; print strtolower($email); } else { print "No dice, FALSE"; to $username = ucwords($username); $email = strtolower($email); return array($username, $password); } else { return; Quote Link to comment https://forums.phpfreaks.com/topic/158620-help-with-an-array-please/#findComment-836579 Share on other sites More sharing options...
adam1984 Posted May 18, 2009 Author Share Posted May 18, 2009 hm, trying what you have there.. maybe i'm missing something.. what would the entire code be inside my php tags? thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/158620-help-with-an-array-please/#findComment-836585 Share on other sites More sharing options...
wildteen88 Posted May 18, 2009 Share Posted May 18, 2009 You only need to change the code in the function, which is what I highlighted in my post above However when calling the function you'd do something like this // call the function // as the function now returns something we need to catch it $info = test("adam1984", "[email protected]"); // check that the function returned an array if(is_array($info)) { echo 'Username is: '. $info[0]. '<br />'; echo 'Email is: '. $info[1]. '<br />'; } else { echo 'FAILED'; } Quote Link to comment https://forums.phpfreaks.com/topic/158620-help-with-an-array-please/#findComment-836588 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.