Bird
Raised Fist0
PHPprogramming~20 mins

How PHP executes on the server - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP Server Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
PHP Script Execution Output
What is the output of this PHP code when executed on a server?
PHP
<?php
function test() {
    static $count = 0;
    $count++;
    return $count;
}

echo test();
echo test();
echo test();
?>
A321
B111
C123
DSyntax Error
Attempts:
2 left
💡 Hint
Static variables inside functions keep their value between calls.
🧠 Conceptual
intermediate
1:30remaining
PHP Server Execution Flow
Which step correctly describes what happens first when a PHP file is requested by a browser?
AThe browser executes the PHP code directly.
BThe PHP code is compiled into a binary executable on the client.
CThe server sends the PHP code as plain text to the browser.
DThe PHP interpreter parses and executes the PHP code on the server.
Attempts:
2 left
💡 Hint
Remember where PHP code runs: server or client?
🔧 Debug
advanced
2:00remaining
Identify the Error in PHP Execution
What error will this PHP code produce when executed on the server?
PHP
<?php
$number = 10;
if ($number = 5) {
    echo "Number is five.";
} else {
    echo "Number is not five.";
}
?>
AOutputs: Number is five.
BOutputs: Number is not five.
CSyntax Error due to assignment in if condition.
DRuntime Error: Undefined variable.
Attempts:
2 left
💡 Hint
Check the difference between '=' and '==' in conditions.
📝 Syntax
advanced
1:30remaining
PHP Syntax Error Identification
Which option contains a syntax error that will prevent PHP from executing the script?
PHP
<?php
function greet($name) {
    echo "Hello, $name!";
}
greet("Alice");
?>
A
&lt;?php
function greet($name) {
    echo "Hello, $name!";
}
greet("Alice"
?&gt;
B
&lt;?php
function greet($name) {
    echo "Hello, $name!";
}
greet("Alice");
?&gt;
C
&lt;?php
function greet($name) {
    echo "Hello, $name!";
}
greet("Alice");
D
&gt;?
;)"ecilA"(teerg
}
;"!eman$ ,olleH" ohce    
{ )eman$(teerg noitcnuf
php?&lt;
Attempts:
2 left
💡 Hint
Look for missing parentheses or semicolons.
🚀 Application
expert
2:30remaining
PHP Output Buffering Behavior
What will be the output of this PHP code when executed on the server?
PHP
<?php
ob_start();
echo "Start-";
ob_clean();
echo "End";
ob_end_flush();
?>
AStart-End
BEnd
CStart
DNo output
Attempts:
2 left
💡 Hint
ob_clean() clears the output buffer before sending it.

Practice

(1/5)
1. What happens when a PHP file is requested by a browser?
easy
A. The PHP code is downloaded as a file to the browser
B. The browser runs the PHP code directly
C. The server runs the PHP code and sends the output to the browser
D. The server sends the PHP code as plain text to the browser

Solution

  1. Step 1: Understand PHP execution location

    PHP code runs on the server, not in the browser.
  2. Step 2: Identify what the browser receives

    The server sends only the output (usually HTML) to the browser, not the PHP code itself.
  3. Final Answer:

    The server runs the PHP code and sends the output to the browser -> Option C
  4. Quick Check:

    PHP runs on server = A [OK]
Hint: Remember: PHP runs on server, browser gets output only [OK]
Common Mistakes:
  • Thinking browser executes PHP code
  • Expecting PHP code to be visible in browser source
  • Confusing PHP with client-side languages like JavaScript
2. Which of the following is the correct way to start a PHP block in a file?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall PHP opening tags

    The standard and recommended way to start PHP code is with
  2. Step 2: Check each option

    uses which is correct. Others are invalid or incomplete tags.
  3. Final Answer:

    -> Option A
  4. Quick Check:

    Standard PHP tag = B [OK]
Hint: PHP code always starts with php [OK]
Common Mistakes:
  • Using short tags like which may be disabled
  • Confusing PHP tags with HTML or script tags
  • Missing the 'php' keyword after
3. What will be the output of this PHP code when accessed via a browser?
<?php
echo "Hello, " . "world!";
?>
medium
A. Syntax Error
B. echo "Hello, " . "world!";
C. Hello, . world!
D. Hello, world!

Solution

  1. Step 1: Understand echo statement

    The echo command outputs the combined string "Hello, world!" to the browser.
  2. Step 2: Confirm string concatenation

    The dot operator joins "Hello, " and "world!" correctly without errors.
  3. Final Answer:

    Hello, world! -> Option D
  4. Quick Check:

    echo concatenates strings = D [OK]
Hint: echo prints combined strings directly [OK]
Common Mistakes:
  • Expecting PHP code to show in browser
  • Confusing concatenation operator
  • Thinking dot prints as text
4. Identify the error in this PHP code snippet:
<?php
echo "Welcome to PHP!"
?>
medium
A. Missing quotes around string
B. Missing semicolon after echo statement
C. Incorrect PHP opening tag
D. Using echo instead of print

Solution

  1. Step 1: Check syntax of echo statement

    PHP statements must end with a semicolon ; to separate commands.
  2. Step 2: Locate missing semicolon

    The echo line lacks a semicolon at the end, causing a syntax error.
  3. Final Answer:

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

    Statements need ; = C [OK]
Hint: Always end PHP statements with a semicolon ; [OK]
Common Mistakes:
  • Forgetting semicolon at line end
  • Confusing PHP tags with HTML tags
  • Assuming echo doesn't need semicolon
5. You want to create a PHP page that shows the current year dynamically. Which code snippet correctly does this?
<?php
// Options:
// A: echo date("Y");
// B: echo "date(Y)";
// C: echo date(Y, Y);
// D: echo "Y";
?>
hard
A. echo date("Y");
B. echo "date(Y)";
C. echo date(Y, Y);
D. echo "Y";

Solution

  1. Step 1: Understand PHP date function usage

    The date function requires the format string in quotes to return the current year.
  2. Step 2: Evaluate each option

    echo date("Y"); correctly calls date("Y") to output the year. echo "date(Y)"; prints the string literally. echo date(Y, Y); misses quotes causing undefined constants and wrong output (1970). echo "Y"; prints letter Y only.
  3. Final Answer:

    echo date("Y"); -> Option A
  4. Quick Check:

    Correct date format string = A [OK]
Hint: Use quotes around format in date() function [OK]
Common Mistakes:
  • Omitting quotes around format string
  • Printing function call as string
  • Confusing output with literal text
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