Bird
Raised Fist0
PHPprogramming~20 mins

MVC architecture overview in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
MVC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP MVC code snippet?

Consider a simple MVC setup where the Controller calls the Model to get data and then passes it to the View. What will be printed?

PHP
<?php
class Model {
    public function getData() {
        return 'Hello from Model';
    }
}

class View {
    public function render($data) {
        echo $data;
    }
}

class Controller {
    private $model;
    private $view;

    public function __construct() {
        $this->model = new Model();
        $this->view = new View();
    }

    public function show() {
        $data = $this->model->getData();
        $this->view->render($data);
    }
}

$controller = new Controller();
$controller->show();
ANo output
BHello from Model
CFatal error: Call to undefined method
DHello from View
Attempts:
2 left
💡 Hint

Think about which class returns the string and which class prints it.

🧠 Conceptual
intermediate
1:00remaining
Which MVC component is responsible for handling user input?

In the MVC architecture, which component typically handles user input and decides what to do next?

AModel
BView
CDatabase
DController
Attempts:
2 left
💡 Hint

Think about who acts like the traffic cop directing traffic.

🔧 Debug
advanced
2:30remaining
Why does this MVC PHP code cause an error?

Look at this PHP MVC code. Why does it cause a fatal error?

PHP
<?php
class Model {
    public function getData() {
        return 'Data';
    }
}

class View {
    public function render() {
        echo $data;
    }
}

class Controller {
    private $model;
    private $view;

    public function __construct() {
        $this->model = new Model();
        $this->view = new View();
    }

    public function show() {
        $data = $this->model->getData();
        $this->view->render();
    }
}

$controller = new Controller();
$controller->show();
AController constructor missing parameters
BModel class missing getData method
CUndefined variable $data in View::render()
DView class cannot echo strings
Attempts:
2 left
💡 Hint

Check the variable usage inside the View's render method.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this MVC PHP snippet?

Find the option that fixes the syntax error in this PHP MVC code:

class Controller {
    public function show() {
        $data = $this->model->getData()
        $this->view->render($data);
    }
}
AAdd a semicolon after getData() call
BChange getData() to getData;
CRemove parentheses from getData
DAdd curly braces around getData()
Attempts:
2 left
💡 Hint

Look carefully at the end of the line calling getData().

🚀 Application
expert
2:00remaining
How many items are in the array passed from Model to View?

The Model returns an array of users to the Controller, which passes it to the View. How many users will the View receive?

PHP
<?php
class Model {
    public function getUsers() {
        return [
            ['name' => 'Alice', 'age' => 30],
            ['name' => 'Bob', 'age' => 25],
            ['name' => 'Charlie', 'age' => 35]
        ];
    }
}

class View {
    public function render($users) {
        echo count($users);
    }
}

class Controller {
    private $model;
    private $view;

    public function __construct() {
        $this->model = new Model();
        $this->view = new View();
    }

    public function showUsers() {
        $users = $this->model->getUsers();
        $this->view->render($users);
    }
}

$controller = new Controller();
$controller->showUsers();
A3
B2
C1
D0
Attempts:
2 left
💡 Hint

Count the number of user arrays inside the returned array.

Practice

(1/5)
1. In the MVC architecture, which component is responsible for handling user input and updating the model?
easy
A. Controller
B. Model
C. View
D. Database

Solution

  1. Step 1: Understand MVC roles

    The Model manages data, the View displays data, and the Controller handles user input.
  2. Step 2: Identify the component for user input

    The Controller receives user actions and updates the Model accordingly.
  3. Final Answer:

    Controller -> Option A
  4. Quick Check:

    Controller handles input [OK]
Hint: Controller connects user actions to data changes [OK]
Common Mistakes:
  • Confusing Model with Controller
  • Thinking View handles input
  • Assuming Database is part of MVC
2. Which of the following is the correct way to describe the role of the Model in MVC?
easy
A. It processes user input and updates views.
B. It manages the data and business logic.
C. It displays data to the user.
D. It handles routing and URLs.

Solution

  1. Step 1: Recall Model's responsibility

    The Model stores and manages data and business rules.
  2. Step 2: Match description to Model

    Only It manages the data and business logic. correctly states that the Model manages data and logic.
  3. Final Answer:

    It manages the data and business logic. -> Option B
  4. Quick Check:

    Model manages data and logic [OK]
Hint: Model is about data and rules, not display or input [OK]
Common Mistakes:
  • Mixing up Model and View roles
  • Thinking Controller manages data
  • Confusing routing with Model
3. Given this PHP MVC snippet:
<?php
class Controller {
  public function show() {
    $data = Model::getData();
    View::render($data);
  }
}

class Model {
  public static function getData() {
    return ['name' => 'Alice'];
  }
}

class View {
  public static function render($data) {
    echo 'Hello, ' . $data['name'];
  }
}

$controller = new Controller();
$controller->show();
?>

What is the output when this code runs?
medium
A. Hello, name
B. Array
C. Hello, Alice
D. Error: Undefined index

Solution

  1. Step 1: Trace data flow in code

    The Controller calls Model::getData(), which returns ['name' => 'Alice'].
  2. Step 2: Check View rendering

    View::render() echoes 'Hello, ' plus the value of $data['name'], which is 'Alice'.
  3. Final Answer:

    Hello, Alice -> Option C
  4. Quick Check:

    Output = 'Hello, Alice' [OK]
Hint: Follow data from Model to View for output [OK]
Common Mistakes:
  • Printing the array directly
  • Using wrong array key
  • Confusing static method calls
4. Identify the error in this MVC PHP code snippet:
<?php
class Controller {
  public function display() {
    $data = Model::fetchData();
    View::show($data);
  }
}

class Model {
  public static function getData() {
    return ['age' => 30];
  }
}

class View {
  public static function show($data) {
    echo 'Age: ' . $data['age'];
  }
}

$controller = new Controller();
$controller->display();
?>
medium
A. Model::fetchData() method does not exist
B. View::show() method is missing
C. Controller class is not instantiated
D. Array key 'age' is incorrect

Solution

  1. Step 1: Check method calls in Controller

    Controller calls Model::fetchData(), but Model only has getData() method.
  2. Step 2: Verify Model methods

    Model::fetchData() does not exist, causing a fatal error.
  3. Final Answer:

    Model::fetchData() method does not exist -> Option A
  4. Quick Check:

    Model::fetchData() method does not exist [OK]
Hint: Match method names exactly between classes [OK]
Common Mistakes:
  • Assuming method names are interchangeable
  • Ignoring case sensitivity
  • Overlooking missing methods
5. You want to add a feature in your PHP MVC app to display a list of users. Which MVC components should you update and how?
hard
A. Update Model to display list, View to fetch users, Controller to store data
B. Update View to fetch users, Controller to display list, Model to handle input
C. Update Controller to fetch users, Model to display list, View to handle input
D. Update Model to fetch users, Controller to pass data, View to display list

Solution

  1. Step 1: Assign responsibilities per MVC

    The Model fetches data, Controller processes logic, View shows output.
  2. Step 2: Map feature to components

    Model fetches users, Controller gets data and sends to View, View displays user list.
  3. Final Answer:

    Update Model to fetch users, Controller to pass data, View to display list -> Option D
  4. Quick Check:

    Model=data, Controller=logic, View=display [OK]
Hint: Model gets data, Controller links, View shows it [OK]
Common Mistakes:
  • Letting View fetch data directly
  • Making Controller handle display
  • Confusing roles of MVC parts
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