jacob1986 Posted December 1, 2015 Share Posted December 1, 2015 I have been given the problem 'write a function called getName() which has three parameters: Christian name, Surname and middle initial. Moreover, return a string in the format of "Smith, John K".' Starting code: echo getName('john', 'smith', 'k'); The only code, which I have thus been able to write - please see below... and it's pitiful! <?php$xml=<<<XML<?xml version="1.0" standalone="yes"?><john></john>XML;$sxe=new SimpleXMLElement($xml);echo $sxe->getName() . "<br>";?> Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/ Share on other sites More sharing options...
cyberRobot Posted December 1, 2015 Share Posted December 1, 2015 Perhaps the following will help: http://php.net/manual/en/functions.arguments.php Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527396 Share on other sites More sharing options...
jacob1986 Posted December 1, 2015 Author Share Posted December 1, 2015 I have this code (while it works - it doesn't seem to help me with the inclusion of the string... echo getName('john', 'smith', 'k'); <?phpfunction getName($getName = "K"){ return "John Smith $getName.\n";}echo getName();?> Prints: John Smith K. Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527398 Share on other sites More sharing options...
cyberRobot Posted December 1, 2015 Share Posted December 1, 2015 I have this code (while it works - it doesn't seem to help me with the inclusion of the string... echo getName('john', 'smith', 'k'); Your function is only set up to accept one argument. To pass three ('john', 'smith', and 'k'), you'll need to modify the function definition. Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527399 Share on other sites More sharing options...
Solution jacob1986 Posted December 2, 2015 Author Solution Share Posted December 2, 2015 <?phpfunction getName($Christianname, $Surname, $middlename = "") { return ucwords($Surname . ", " . $Christianname . " " . $middlename) ;}echo getName('john', 'smith', 'k.');?> Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527452 Share on other sites More sharing options...
cyberRobot Posted December 2, 2015 Share Posted December 2, 2015 (edited) That seems like it would work. Did you have any additional questions? Edited December 2, 2015 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527457 Share on other sites More sharing options...
jacob1986 Posted December 3, 2015 Author Share Posted December 3, 2015 No more questions on this but thank-you Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527521 Share on other sites More sharing options...
Barand Posted December 3, 2015 Share Posted December 3, 2015 To be on the safe side, in case the input is "JOHN SMITH". function getName ($first, $last, $middle='') { return ucwords(strtolower("$last, $first $middle")); } echo getName('JOHN', 'SMITH', 'K'); //--> Smith, John K Also note when using double-quotes you don't need the string concatenation. 1 Quote Link to comment https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527523 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.