Jump to content

getName()


Go to solution Solved by jacob1986,

Recommended Posts

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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/299615-getname/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527399
Share on other sites

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.

  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/299615-getname/#findComment-1527523
Share on other sites

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.