FAQ: Introduction to PHP - How is PHP Executed?

This community-built FAQ covers the “How is PHP Executed?” exercise from the lesson “Introduction to PHP”.

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

Learn PHP

FAQs on the exercise How is PHP Executed?

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!

So I was wondering why php code starts with <?php and ends with ?>.
Why the question mark? Wouldn’t it just look better without it, so why add it? Is there a specific reason for this?

Rasmus Lerdorf on implementing the first version of PHP “I wrote a very simple parser that would pick tags out of HTML files and replace them with the output of the corresponding functions in the C library.”



Maybe, but I highly doubt anyone really cares…

Besides, in pure PHP files, you’ll only find one <?php tag at the very top of the file.

In the views, we now mostly use template engines that make using the php tags obsolete.

Instead of <?php echo $variable; ?>, we’ll write {{ $variable }} instead.

1 Like

Why does PHP show <?php echo "I love PHP!"; on Codecademy?

However, in PHP website it shows <?php echo "I love PHP!"; ?>

Can someone explain please?

In a PHP-only file, there’s no need to provide a closing tag.

<?php

$hello = 'Hello, World!';

echo $hello;

It’s actually a PSR-2 recommendation to omit it:

The closing ?> tag MUST be omitted from files containing only PHP.

That said, within a HTML file, you’ll have to use the closing tag, otherwise the rest of the file would be treated as if it was PHP code.

So, for example:

<html>
<body>
  <h1><?php echo 'Hello, World!'; ?></h1>
  <p>This is a simple Hello World page.</p>
</body>
</html>

Hope this answers your question!

2 Likes