Bird
Raised Fist0
PHPprogramming~5 mins

First PHP program (Hello World) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - First PHP program (Hello World)
Start PHP script
↓
Execute echo statement
↓
Output: Hello World
↓
End script
The PHP script starts, runs the echo command to print 'Hello World', then ends.
Execution Sample
PHP
<?php
echo "Hello World";
This code prints the text 'Hello World' to the screen.
Execution Table
StepActionEvaluationOutput
1Start PHP scriptN/A
2Execute echo statementecho "Hello World";Hello World
3End scriptN/A
💡 Script ends after printing 'Hello World'.
Variable Tracker
VariableStartAfter Step 2Final
No variablesN/AN/AN/A
Key Moments - 2 Insights
Why do we use <?php and ?> tags?
These tags tell the server where PHP code starts and ends, as shown in step 1 and step 3 of the execution_table.
What does the echo statement do?
Echo prints the text to the screen immediately, as seen in step 2 where 'Hello World' is output.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 2?
ANo output
Becho "Hello World";
CHello World
DPHP code
💡 Hint
Check the Output column in row for step 2 in the execution_table.
At which step does the PHP script end?
AStep 3
BStep 2
CStep 1
DAfter Step 2
💡 Hint
Look at the Action column in the execution_table for the step labeled 'End script'.
If we remove the closing ?> tag, what happens?
AThe script will not run
BThe script still runs and prints 'Hello World'
CSyntax error occurs
DOutput will be empty
💡 Hint
PHP allows omitting the closing tag at the end of a file without error.
Concept Snapshot
<?php ... ?> tags mark PHP code start and end.
echo prints text to the screen.
This program outputs: Hello World
No variables needed.
Script runs top to bottom.
Full Transcript
This PHP program starts with the <?php tag and ends with ?>. Inside, it uses echo to print the text 'Hello World' to the screen. The execution starts by reading the PHP code, then runs the echo statement which outputs the text immediately. After that, the script ends. There are no variables used in this simple program. The closing ?> tag is optional if the PHP code is at the end of the file. This is the simplest way to write a PHP program that shows output.

Practice

(1/5)
1. What does the echo statement do in a PHP program?
easy
A. It prints text or output to the screen.
B. It starts the PHP program.
C. It ends the PHP program.
D. It declares a variable.

Solution

  1. Step 1: Understand the role of echo

    The echo statement is used in PHP to display text or output on the screen.
  2. Step 2: Differentiate from other PHP parts

    Starting and ending PHP code uses <?php and ?>, not echo. Variables are declared differently.
  3. Final Answer:

    It prints text or output to the screen. -> Option A
  4. Quick Check:

    echo = print output [OK]
Hint: Remember echo means print output on screen [OK]
Common Mistakes:
  • Confusing echo with PHP start tag
  • Thinking echo ends the program
  • Mixing echo with variable declaration
2. Which of the following is the correct way to start a PHP script?
easy
A. <script>
B. <php>
C. <?>
D. <?php

Solution

  1. Step 1: Identify the PHP opening tag

    PHP scripts start with <?php to tell the server to process PHP code.
  2. Step 2: Check other options

    <php> and <?> are invalid tags. <script> is for JavaScript, not PHP.
  3. Final Answer:

    <?php -> Option D
  4. Quick Check:

    PHP starts with <?php [OK]
Hint: PHP always starts with <?php tag [OK]
Common Mistakes:
  • Using <php> instead of <?php
  • Confusing PHP tags with HTML or JavaScript tags
  • Omitting the PHP start tag
3. What will be the output of this PHP code?
<?php
echo "Hello World!";
?>
medium
A. echo "Hello World!";
B. Hello World!
C. Syntax Error
D. No output

Solution

  1. Step 1: Understand the echo statement

    The echo command prints the string inside quotes to the screen exactly as it is.
  2. Step 2: Check the code syntax

    The code uses correct PHP tags and syntax, so it will output the string "Hello World!".
  3. Final Answer:

    Hello World! -> Option B
  4. Quick Check:

    echo prints "Hello World!" [OK]
Hint: echo prints text inside quotes directly [OK]
Common Mistakes:
  • Expecting code to print the echo statement itself
  • Missing semicolon causing syntax error
  • Not using correct PHP tags
4. Find the error in this PHP code:
<?php
echo "Hello World!"
?>
medium
A. Incorrect quotes around text
B. Wrong PHP start tag
C. Missing semicolon after echo statement
D. Missing closing PHP tag

Solution

  1. Step 1: Check the echo statement syntax

    In PHP, each statement must end with a semicolon. The echo line is missing it.
  2. Step 2: Verify other parts

    The PHP start tag and quotes are correct. The closing PHP tag is optional but present.
  3. Final Answer:

    Missing semicolon after echo statement -> Option C
  4. Quick Check:

    Statements need semicolon [OK]
Hint: Always end PHP statements with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolon at end of statement
  • Confusing PHP tags with HTML tags
  • Using wrong quotes like single without closing
5. You want to write a PHP program that prints "Hello World!" and then prints "Welcome to PHP." on the next line. Which code is correct?
hard
A.
<?php
echo "Hello World!";
echo "Welcome to PHP.";
?>
B.
<?php
echo "Hello World!\nWelcome to PHP.";
?>
C.
<?php
print "Hello World!" print "Welcome to PHP.";
?>
D.
<?php
echo "Hello World!" echo "Welcome to PHP.";
?>

Solution

  1. Step 1: Check syntax for multiple echo statements

    Each echo must end with a semicolon.
    <?php
    echo "Hello World!";
    echo "Welcome to PHP.";
    ?>
    correctly uses two echo statements with semicolons.
  2. Step 2: Check other options for errors

    <?php
    echo "Hello World!\nWelcome to PHP.";
    ?>
    uses \n inside double quotes but PHP does not automatically print new lines in HTML output.
    <?php
    print "Hello World!" print "Welcome to PHP.";
    ?>
    misses semicolon between print statements.
    <?php
    echo "Hello World!" echo "Welcome to PHP.";
    ?>
    misses semicolon after first echo.
  3. Final Answer:

    Two separate echo statements with semicolons -> Option A
  4. Quick Check:

    Separate echo with semicolons for multiple lines [OK]
Hint: Use separate echo lines with semicolons for multiple outputs [OK]
Common Mistakes:
  • Missing semicolons between statements
  • Using print without semicolons
  • Expecting \n to create new line in browser output
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard