Play with PHP Basis¶
Objectives¶
In this tutorial, you will try and execute PHP code in a Dev container.
Prerequisites¶
The following prerequisites must be filled to run this service:
- Docker must be installed.
- Docker Compose must be installed (it should be installed by default with Docker in most cases).
- Visual Studio Code must be installed.
- Dev containers must be installed in Visual Studio Code.
Steps¶
Initiate the project¶
- Create a new repository on GitHub.
- Clone the repository on your computer.
- Open the repository in Visual Studio Code.
Initiate the dev container¶
- Add devcontainer configuration files by clicking on the bottom left corner of Visual Studio Code and selecting "Remote-Containers: Add Development Container Configuration Files...".
- Select "PHP" in the list of available containers.
- Select the latest version of PHP (8.2 at the time of this commit) in the list of available versions.
- Add Visual Studio Code Extensions to the
devcontainer.json
by clicking on the extensions tab on the left.- Search for "PHP Debug", click on the gear icon (⚙️) and select "Add to devcontainer.json".
- Search for "PHP Intelephense", click on the gear icon (⚙️) and select "Add to devcontainer.json".
- (Optional) Search for "GitHub Copilot", click on the gear icon (⚙️) and select "Add to devcontainer.json".
- Remove unnecessary comments from the
devcontainer.json
file.
Your folder should look like that:
Create the index.php file¶
- Create a new file named
index.php
in the root of your project. - Add the following code in the file:
index.php | |
---|---|
Your folder should look like that:
Run the project¶
- Open the command palette in Visual Studio Code by pressing
Ctrl+Shift+P
orCmd+Shift+P
. - Select "Remote-Containers: Reopen in Container".
- Open the terminal in Visual Studio Code.
- Run the following command to start the server:
Use some variables¶
We are going to check what sort of browser the user is using. To do so, we are going to use the $_SERVER
variable. This variable is a superglobal, which means that it is always available in all scopes.
Update the index.php
file with the following code:
If you are still running the server, you should see something like that on Firefox:
or something lite that on Chromium:
Info
The reason you're getting similar user agent strings across different browsers is because modern web browsers tend to use a common user agent string format for compatibility and security reasons. The user agent string provides information about the browser and its version, the operating system, and sometimes other details.
For instance, Firefox and Chrome both include information like "Mozilla/5.0" because they are designed to be compatible with websites that might check for the presence of the "Mozilla" identifier in the user agent string, a practice that dates back to the early days of the web.
Use control structures and functions¶
You are going to change the behavior of the page depending on the browser the user is using.
Update the index.php
file with the following code:
index.php | |
---|---|
If you are still running the server, you should see something like that on Firefox:
or nothing on Chromium.
And now, you will add a condition for Chrome:
index.php | |
---|---|
You will take this a step further and jump in and out of PHP mode even in the middle of a PHP block. This is useful if you want to mix HTML and PHP code.
Update the index.php
file with the following code:
index.php | |
---|---|
You can also use functions to make your code more readable.
Update the index.php
file with the following code:
Variables and constants¶
You are going to use variables.
Update the index.php
file with the following code:
This script will generate an undefined variable error. This is because the variables $firefox
and $chrome
are not defined in the scope of the functions isFirefox()
and isChrome()
. To fix this, you will use the global
keyword.
Update the index.php
file with the following code:
You can use arguments to pass variables to functions. Doing so, removing the need for the global
keyword and repeating the same code.
Update the index.php
file with the following code:
Imagine that you don't want the variable $firefox
and $chrome
to be changed. You can use constants instead.
Update the index.php
file with the following code:
Switch¶
You can use the switch
statement to make your code more readable.
Update the index.php
file with the following code:
More readable ? Right,... but now you know how to use switch
statements.
Conclusion¶
You have successfully run a PHP script in a Dev container. You have also learned how to use variables, control structures, and functions.