FAQ: Introduction to PHP Functions - Defining Functions

This community-built FAQ covers the “Defining Functions” exercise from the lesson “Introduction to PHP Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Defining Functions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Just curious,

I see that in every example given for printing multiple lines of text, it is formatted like this:

function greetLearner()
{
  echo "Hello, Learner!\n";
  echo "I hope you're enjoying PHP!\n";
  echo "Love, Codecademy";
}

I tried formatting it like this:

function greetLearner()
{
  echo "Hello, Learner!\n hope you're enjoying PHP!\nLove, Codecademy";
}

There was no difference in how it printed. Is one method better than the other, and if so, why?

Thanks!

I’d say it’s a matter of preference. The first example may be slightly more readable, but as long as you know what \n means, the second example is pretty readable too. Just an aside, this would work too because if you hit the enter/return key in the middle of a string, you get a new line:

function greetLearner()
{
  echo "Hello, Learner!
I hope you're enjoying PHP!
Love, Codecademy";
}

Happy coding!

1 Like

Aside

echo is simple by all appearances but in reality it is a very expensive operation, or at least it used to be. We can build huge strings in PHP and then echo only once. Something to keep in mind when writing generation code.

3 Likes

Makes sense; thank you!

1 Like

OK, now I’m going to research this!

As soon as I’m done researching Mac OS X vs. Linux, that is…

Codecademy has created a monster! :laughing:

2 Likes