Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

PHP (8.5) Module 3

Control flow interview questions

11 interview questions on control flow from PHP (8.5): short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

11 questions: 3 warm-ups and 8 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    Are elseif and else if the same in PHP?

    Answered in the lesson if, else, elseif and the ternary operator

  2. Fresher

    How do you loop over the keys and values of an array in PHP, and does changing the loop variable change the array?

    Answered in the lesson Loops: for, while, do-while and foreach

  3. Fresher

    What is the difference between include and require, and when do you use their _once forms?

    Answered in the lesson include and require: splitting code into files

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A checkout function prices delivery with if statements nested five levels deep: member or not, coupon or not, metro or not, order size, weekend or not. Members sometimes pay the non-member price. How do you find the cause, and how do you restructure the function so that the next mistake is easy to see?

    What it tests
    Reading a long chain of conditions, the order of tests, guard clauses, and boundary tests that prove a refactor changed nothing.
    First step
    Write the rules down as a table of inputs and fees, with one row just below and one exactly on each threshold, for members and non-members. Run the current function on every row before changing anything: the rows that come out wrong point to the branch that is tested too late or never reached.
    Follow-ups

    From the lesson if, else, elseif and the ternary operator

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Fresher to mid

    Reading a signup handler, you find $valid = $hasEmail and $hasPhone;, and its tests pass. What does the line really store, why did the tests miss it, and how do you find and fix similar lines in a large codebase?

    What it tests
    The precedence of and and or against =, test inputs that make a condition false, and finding a risky pattern across a codebase.
    First step
    Explain the grouping first: = binds before and, so PHP runs ($valid = $hasEmail) and $hasPhone, and $valid ends up equal to $hasEmail. Then look at the test inputs: if every test that reaches the line has both an email and a phone, or neither, the bug cannot show.
    Follow-ups

    From the lesson if, else, elseif and the ternary operator

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Fresher to mid

    Write gradeReport(array $marks): array for an array of names to marks. It returns the count of each grade (A from 85, B from 70, C from 50, F below 50) and the names with the highest mark. How do you do it in one loop, and what happens with ties and an empty class?

    What it tests
    foreach over keys and values, counters that start at zero, tracking a maximum with ties, and the empty input.
    First step
    Start the four counters at 0 and the list of top names empty, then make one pass with foreach ($marks as $name => $mark): add one to the counter of the mark's grade, and compare the mark with the best mark seen so far.
    Follow-ups

    From the lesson Loops: for, while, do-while and foreach

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Fresher to mid

    An export script trims every field of $rows in a foreach by reference, then a second foreach writes the CSV file. The file has the right number of rows, but its last row is a copy of the second-to-last. Explain why, give the one-line fix, and write a version without references.

    What it tests
    The reference that foreach leaves behind, how a later loop writes through it, and choosing a fix.
    First step
    Look at the variable names of the two loops. If both use $row and the first takes &$row, then after the first loop $row is still a reference to the last element, and every value the second loop assigns to $row is written into that element.
    Follow-ups

    From the lesson Loops: for, while, do-while and foreach

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  5. Mid to senior

    A nightly PHP command-line job pages through an API and is sometimes still running in the morning, until someone kills it. Why does PHP not stop it, which loop bugs cause this, and how do you make the job end safely every night?

    What it tests
    The time limit on the command line, conditions that never become false, guards on loops over outside data, and failing in a way someone notices.
    First step
    Check the time limit first: on the command line PHP sets none by default, so it will not stop the job by itself. Then read the exit condition of the paging loop and ask what happens if the cursor stops changing or never becomes null.
    Follow-ups

    From the lesson Loops: for, while, do-while and foreach

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Fresher to mid

    A report script works when you run it from its own folder, but its cron job fails every night with "Failed opening required '../config/app.php'". Why, and how do you write include paths that work wherever the script is started from?

    What it tests
    The working directory against the file's own folder, how relative paths and bare names are resolved, and __DIR__.
    First step
    Compare the two working directories. From a terminal it is the folder you typed the command in, while cron starts the job from whatever folder its own settings give. A path that starts with ../ is resolved against that folder, not against the folder of the script.
    Follow-ups

    From the lesson include and require: splitting code into files

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  7. Mid to senior

    A legacy site loads every page with include $_GET['page'] . '.php';. Explain the risk to the team that owns the code, fix it without breaking the existing links, and describe the test that proves the fix.

    What it tests
    Recognising local file inclusion, an allowlist that keeps old links working, and a test that fails on the weak version and passes on the fix.
    First step
    Say who decides what runs: the address does, so any name a visitor writes picks a file on the server, and a name with ../ in it reaches files outside the pages. Then collect the page names the site really uses, from its links and its access logs, because those are the names the fix must keep.
    Follow-ups

    From the lesson include and require: splitting code into files

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Fresher to mid

    A layout template shows an internal error message to customers on some pages. The controller set $message earlier for its log, and the template prints $message whenever it is set. Why can the template see it, and how do you limit what each template can read?

    What it tests
    The shared scope of include, a function as a scope boundary, extract() and its flags, and where escaping belongs.
    First step
    State the rule first: an included file inherits the variables of the line that includes it, so a template included at the top level of a controller sees every variable the controller has set, including ones meant only for the log.
    Follow-ups

    From the lesson include and require: splitting code into files

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All PHP (8.5) interview questions · The PHP (8.5) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.