DEV Community

Prasad Saya
Prasad Saya

Posted on

Notes about my first PHP app

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

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:

{
  "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>."
  ]
}
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

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
Enter fullscreen mode Exit fullscreen mode

PHP Features

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

From the PHP Manual:


Conclusion

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


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.


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.


Top comments (0)