Notes about my first PHP app
Tags: php, webdev
Published: 2023-01-07 07:06:08

This is my first app written using PHP. I have been studying and trying coding PHP casually for the last couple of weeks. I am using the PHP Manual and other online free resources for my study. 

I am an experienced programmer using other languages. I am able to select some basic and intermediate topics and was trying the code in small scripts. On the way, I thought of building a small example app. This would allow me try some features as I build the functionality for the app. It was a quiz app.

In this post I talk about the app and the PHP features I had used.

---

## Contents
- [The Data](#data)
- [The App](#app)
- [Utility Files](#util-files)
- [Data Exceptions](#exceptions)
- [Development](#development)
- [PHP Features](#features)
- [Conclusion](#conclusion)

---

## The Data <a name="data"></a>

Like most of apps there is data, the quiz data. This is a Json file, `quiz_data.txt`, with an array of documents. Each document has the quiz question, options, option type, answers, and notes fields. 

Here is a sample Json document:

```json
{
  "id": 1,
  "question": "Is <code>1800</code> a leap year?.",
  "type": "S",
  "options": [
    "(a) True",
    "(b) False"
  ],
  "answers": [
    "b"
  ],
  "notes": [
    "False.",
    "A leap year is divisible by <code>4</code>, but not divisible by <code>100</code>.",
    "When divisible by <code>100</code>, must also be divisible by <code>400</code>."
  ]
}
```
I had used the PHP functions `file_get_contents` and `json_decode` to read the Json file and convert documents as associate arrays. This array data is used in the app.

---

## The App <a name="app"></a>

The app's code is in few files. These are `.php`, `.js`, `.css` and `.txt` files. Some of the PHP files are for starting the app, navigating to other pages and for performing user interaction. Other PHP files are utility files. These have class, function and constant definitions.

The app starter PHP script is the `quiz_start.php`. This reads the Json file and prepares data for using in the app. This page has navigation to the following `quiz_form.php` page.

The quiz_form page shows the quiz questions, allows user to select answers, checks the correctness of the selections, and then navigates to the next question. Finally, when the questions are exhausted, shows a result page `quiz_result.php`. That’s the end of the app. The user has an option to navigate back to the starter page from here or quit.

The user can also navigate to the starter page from quiz_form page.

The starter page also starts a session. The quiz data array is stored in a `$_SESSION` automatic global variable for usage in the app's other pages. Other data tracked as session variables are the quiz data counter and correct answers counter. These are used in accessing the quiz question to be displayed and for the result page to show the percentage success.

The quiz_form page reads the sessions variables and updates the counters. There is also a `quiz_app.js` JavaScript file with a function to evaluate the answer for each question. The user selects the answers from the options and clicks the Answer JavaScript button. This executes the JS function and shows if the user selections were correct or not.

---

## Utility Files <a name="util-files"></a>

These are the included files. These are accessed in the PHP pages using the `require` or `include` expressions. The app has three include files: `file_util.php`, `exception_util.php` and `constants.php`.

These have definitions of `FileUtil`, `ExceptionUtil` and `Constants` classes.

The `FileUtil` class has a function that reads the Json file, decodes it to PHP array and returns the array. The `ExceptionUtil` has a function to handle the exceptions that can occur in accessing, reading, and decoding the input file data. `Constants` class defines a set of constants used throughout the app (for example, the input Json data file name).

---

## Data Exceptions <a name="exceptions"></a>

The app starts with reading the Json file.

There is a possibility that the input file may not exist (or have an incorrect name), or is empty or has incorrectly formatted Json. These three exceptional situations are handled in the app. In these scenarios the exception handling code shows appropriate message on the starter page and exits the app.

---

## Development <a name="development"></a>

The app is developed using PHP 8.2.0, Apache HTTP Server 2.4, HTML, CSS and JavaScript on a Windows computer.

The source code is structured in the directories (dir) as follows:

```
app root (dir)
    quiz_start.php
    quiz_form.php
    quiz_result.php
    redirect_start.php
    data (dir)
        quiz_data.txt
    public (dir)
        quiz_app.js
        quiz_app_styles.css
    includes (dir)
        constants.php
        file_util.php
        exception_util.php
```

---

## PHP Features <a name="features"></a>

These are some notable functions, class, variables, type, class, expression and OO features used in the app. 

From the PHP Manual:

- Functions: [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php), [json_decode](https://www.php.net/manual/en/function.json-decode.php), [iconv](https://www.php.net/manual/en/function.iconv.php), [session_start](https://www.php.net/manual/en/function.session-start), [isset](https://www.php.net/manual/en/function.isset), [header](https://www.php.net/manual/en/function.header)
- Variables: [$_SESSION](https://www.php.net/manual/en/reserved.variables.session.php), [$_POST](https://www.php.net/manual/en/reserved.variables.post)
- Type: [Arrays](https://www.php.net/manual/en/language.types.array.php)
- Class: [JsonException](https://www.php.net/manual/en/class.jsonexception)
- Expression: [require](https://www.php.net/manual/en/function.require.php)
- OO Features: [Classes](https://www.php.net/manual/en/language.oop5.basic.php), [Static methods](https://www.php.net/manual/en/language.oop5.static.php) and [Class constants](https://www.php.net/manual/en/language.oop5.constants.php)
- Configuration: [include path]( https://www.php.net/manual/en/ini.core.php#ini.include-path)


---

## Conclusion <a name="conclusion"></a>

This is a **picture of the app** running in a web browser. It’s the quiz_form page. It shows the quiz data as the user selects the answer options and clicks the Answer button. 

![Quiz App's quiz_form page showing the quiz data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xws0a5xbr6uxjbwjw09.png)

---

Writing the code as I was studying and learning helped me grasp the PHP features well. These are some of the features I had used in this app. Until now I am feeling quite good about the PHP.

Here is the app's **source code** at the GitHub: [a-demo-quiz-app-using-php](https://github.com/prsaya/a-demo-quiz-app-using-php.git).

---

That said, there are two features I had explored but didn’t include in coding the app. 

The first is the namespaces. 

The second is usage of classes and objects for the quiz data. This also meant serialization of objects and the implementation of the two magic methods `__serialize` and `__unserialize` (for the object to be used as session variable). This coding worked fine, but it is more code and processing. I will be working on it further and try to use this efficiently.


---