0

I have tried to create a function that will take two arguments and in theory, echo out the result. After doing some Googling, I got the impression that this should be accomplished using an array, however I'm not too sure on the logic.

I was hoping I could call the function like so - kb_article("How to do something", "Q12345"), to get the formatting of:

Related KB Article(s):
How to do something - Q12345

    function kb_article($title, $code)
{
    echo "<h2>Related KB Article(s): </h2><br />";
    echo $title + " - " + $code;
}

How can this be achieved?

1
  • That's not how you do string concatenation in PHP - you need to use . instead of + Commented May 13, 2013 at 16:52

3 Answers 3

2

String concatenation is performed using . ( instead of + ). Here's the corrected version of your function:

function kb_article($title, $code)
{
    echo '<h2>Related KB Article(s):</h2><br />';
    echo $title . " - " . $code;
}
Sign up to request clarification or add additional context in comments.

2 Comments

My mistake, used too many languages since I last used PHP! Thank you for the reply, I would upvote you, however I've recently been downvoted on a previous post and thus I lost the ability to do so! :(
@DaveMelia, nevermind. Just accept this as answer for future visitors. Also notice that you usually can delete questions that have been down voted (for example this one) and your reputation will grow back again.
0

You can just enclose the whole string in double quotes...

function kb_article($title, $code)
{
    echo "<h2>Related KB Article(s): </h2><br />";
    echo "$title  -  $code";
}

kb_article("title","code");

// outputs the expected title - code

Or as previously posted, do string concatenation properly.

function kb_article($title, $code)
{
    echo "<h2>Related KB Article(s): </h2><br />";
    echo  $title . " - " . $code;
}

kb_article("title","code");

1 Comment

Thank you also, I didn't realize I could use them both in the same string!
0

You need to use . instead of + to concatenate in php. Like so:

echo $title." - ".$code;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.