helpmehelpmehelpme Posted December 12, 2011 Share Posted December 12, 2011 I'm trying to write a function that accepts two arguments, first and last name, then returns the initials of the first and last name in a single string, and I'm having trouble getting started. This is all I got. <?php function first_and_last ($first_name, $last_name){ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/ Share on other sites More sharing options...
mikosiko Posted December 12, 2011 Share Posted December 12, 2011 ...and I'm having trouble getting started... here is a good place to start working on your problems http://php.net/manual/en/language.functions.php Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297131 Share on other sites More sharing options...
Winstons Posted December 12, 2011 Share Posted December 12, 2011 Do you mean this function first_and_last ($first_name, $last_name){ return substr($first_name, 0, 1) . '. ' . substr($last_name, 0, 1) . '.'; } After, you can call this function echo first_and_last('John', 'Smith'); Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297135 Share on other sites More sharing options...
bluejay002 Posted December 12, 2011 Share Posted December 12, 2011 Alternatively, you can do it this way: function first_and_last($first_name, $last_name) { return $first_name[0] . '. ' . $last_name[0] . '.'; } Cheers, Jay Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297148 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 okay, I have index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Functions!</title> </head> <body> <p>Please enter your first and last name so I can give you your initials.</p> <form action="functions.php" method="post"> <p>First Name: <input type="text" name="first_name" size="20" /></p> <p>Last Name: <input type="text" name="last_name" size="20" /></p> <input type="submit" name="submit" value="Get My Initials" /> </form> </body> </html> and function.php <?php function first_and_last($first_name, $last_name ) { return $first_name[0] . '. ' . $last_name[0] . '.'; } ?> if i'm posting $first_name and $last_name from the index.php page to the functions.php page where would I put the $_POST? or how would i write it into the code Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297153 Share on other sites More sharing options...
Winstons Posted December 12, 2011 Share Posted December 12, 2011 In function.php you must write <?php function first_and_last($first_name, $last_name ) { return $first_name[0] . '. ' . $last_name[0] . '.'; } echo first_and_last($_POST['first_name'], $_POST['last_name']); Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297154 Share on other sites More sharing options...
bluejay002 Posted December 12, 2011 Share Posted December 12, 2011 You can also check this tutorial to give you more light with PHP functions. http://www.w3schools.com/php/php_functions.asp Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297155 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 oh duh, i was thinking you had to do the $_POST up in the function somewhere...thanks for your help.. Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297157 Share on other sites More sharing options...
bluejay002 Posted December 12, 2011 Share Posted December 12, 2011 Well, you could since $_POST is superglobal but that defies the purpose of having a function and only makes it less reusable for any other variables that wish to use it. Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297161 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 can you do like a loop or something that would let the user enter like 5 other names and have it print out all of the initials? Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297166 Share on other sites More sharing options...
Winstons Posted December 12, 2011 Share Posted December 12, 2011 Do you mean it ? function first_and_last() { $names = func_get_args(); return array_map(create_function('$item', 'return $item[0] . ".";'), $names); } echo '<pre>'.print_r(first_and_last('John', 'Brad', 'Joseph'), 1).'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297191 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 i need one where the user can enter the names themselves and have it cycle through and print out the initials of all the names the user has entered... Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297195 Share on other sites More sharing options...
Winstons Posted December 12, 2011 Share Posted December 12, 2011 Is it ? function first_and_last() { $names = func_get_args(); $str = ''; foreach($names as $name) $str .= $name[0] . '.<br/>'; return $str; } echo first_and_last('John', 'Brad', 'Joseph'); Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297198 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 i have this, but for some reason that i cant figure out it's giving me extra letters. if i enter kelly norton, it outputs fknl or jerry lewis, fjll, here's the code: index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Functions!</title> </head> <body> <p>Please enter your first and last name so I can give you your initials.</p> <form action="functions.php" method="post"> <?php for ($i = 0; $i < 5; $i++) { // Maybe do a fieldset to separate each group. print '<legend>Enter Name</legend>'; print('<label for="first_name%d">First Name:</label>', $i); print('<input id="first_name%d" type="text" name="name[][first]" size="20" />', $i); print('<label for="last_name%d">Last Name:</label>', $i); print('<input id="last_name%d" type="text" name="name[][last]" size="20" />', $i); //print '</fieldset>'; } ?> <input type="submit" name="submit" value="Get My Initials" /> </body> </html> functions.php <?php if (isset($_POST['name']) && is_array($_POST['name'])) { foreach ($_POST['name'] AS $fullname) { list($first, $last) = each($fullname); print first_and_last($first, $last); } } function first_and_last($first_name, $last_name ) { return $first_name[0] . '. ' . $last_name[0] . '.'; } //echo first_and_last($_POST['first_name'], $_POST['last_name']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297204 Share on other sites More sharing options...
Winstons Posted December 12, 2011 Share Posted December 12, 2011 Your code has 'notice' Therefore you must correct it, this way: index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Functions!</title> </head> <body> <p>Please enter your first and last name so I can give you your initials.</p> <form action="functions.php" method="post"> <?php for ($i = 0; $i < 5; $i++) { // Maybe do a fieldset to separate each group. print '<legend>Enter Name</legend>'; printf('<label for="first_name%d">First Name:</label>', $i); printf('<input id="first_name%d" type="text" name="name[][first]" size="20" />', $i); printf('<label for="last_name%d">Last Name:</label>', $i); printf('<input id="last_name%d" type="text" name="name[][last]" size="20" />', $i); //print '</fieldset>'; } ?> <input type="submit" name="submit" value="Get My Initials" /> </body> </html> functions.php <?php if (isset($_POST['name']) && is_array($_POST['name'])) { foreach ($_POST['name'] AS $fullname) { list($first, $last) = each($fullname); print first_and_last($first, $last); } echo '<br/>'; } function first_and_last($first_name, $last_name ) { return (!empty($first_name) && !empty($last_name)) ? $first_name[0] . '. ' . $last_name[0] . '.' : ''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297222 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 i did that, but it's still giving me the extra f and l letters in addition to the initials. Im stumped..ive been working on this for two days now and i cant seem to figure out where the extra letters are coming from Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297234 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 okay, i did that, but im still getting the extra letters. Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297239 Share on other sites More sharing options...
Winstons Posted December 12, 2011 Share Posted December 12, 2011 OK. This is residual code. And extra letters is missing fnction.php <?php if (isset($_POST['name']) && is_array($_POST['name'])) { $array = $_POST['name']; for($i = 0; $i < sizeof($array); $i++) { $first = !empty($array[$i]['first']) ? $array[$i]['first'] : ''; $last = !empty($array[$i + 1]['last']) ? $array[$i + 1]['last'] : ''; echo first_and_last($first, $last); } } function first_and_last($first_name, $last_name ) { return (!empty($first_name) && !empty($last_name)) ? $first_name[0] . '. ' . $last_name[0] . '.' : ''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297241 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 12, 2011 Author Share Posted December 12, 2011 sweeeeeet....that fixed it thanks Quote Link to comment https://forums.phpfreaks.com/topic/253007-function/#findComment-1297242 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.