Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/158620-help-with-an-array-please/
Share on other sites

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;

 

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';
}

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.