18 Forms Part 2

Class 18 (and Lab Session 5): PHP & Forms

Today's class

Make sure to go over the content from the lecture first, which is below Then, you can start working on Lab 5.

Docker Addendum

Here is an introduction where we review docker issues (opens in a new tab) as they continue to be a source of trouble and confusion for a fraction of you.

We are using docker to keep a consistent environment between all of us, with the same versions of everything. Remember to use docker ps to check what containers are running and as a general diagnostic tool. You should also use docker system prune to clean any unused containers. Remember that the container is like a new pc that we buy with all the software we want installed, while the image is the like the pc model with the software pre-installed. As we use docker, depending on the command we may it may end up buying new computers (containers) for us to utilize. This sometimes happen when we use the play button in docker desktop or we say things like docker run in the command line. We don't want to do this all the time as each container takes up space. This is also why we use the docker-compose.yml file, which is a set of configuration instructions that we can provide to docker.

Everytime that you modify your docker-compose.yml file though, a new computer is purchased. If you want right now and you haven't done so already, remove the version line (line 1) in your docker-compose.yml file, save, and then do a docker system prune from the command line.

Debugging in PHPStorm

While we are on the subject of our computers, and processes the next video recaps how to debug in PHPStorm (opens in a new tab)

Highlights here are to:

  • Make sure that you setup a keymap in Settings->Keymap
  • Set breakpoints to allow you to stop on a line of code when the code is executing
  • While in debugging mode, you can use the stepping functions to go over your code more in-depth:
    • Step over screesnhot of step over icon will execute that entire line, even if it includes a function call
    • Step into screenshot of step into icon will execute the line, but if there's a function call it will jump inside that function call so you can go through that function line by line if you wish.
    • Step out screenshot of step out icon will finish executing a function and take you back to whoever called it.
  • Also while in debugging mode, use the calculator icon screesnhot of calculator icon to evaluate expressions on the fly, which can be also be useful for debugging.
  • In addition to evaluating expressions, you can also setup watch expressions that will stop the code whenever the values there change.

I understand that using the IDE debugger is not the first choice, especially since some of you are used to using print statements. However, if you want to strengthen your programming skills, the debugger is a crucial skill to add to your portfolio.

Super Globals in PHP

This next short video (opens in a new tab) goes through some of the different variables in PHP.

Remember that when you see code out there where a variable starts with the prefix of $_, that will indicate a super global variable, that PHP has reserved its use and stands for something. You can review the list of super globals in the PHP Documentation (opens in a new tab)

Processing Data in PHP

This in-depth video (opens in a new tab) covers how we end up processing data that is provided to us via these super globals in PHP.

Part of processing data comes with the idea of associate arrays, which works like dictionaries in python.

Keep in mind:

  • Many of the super globals and server requests end up being associate arrays
  • $days["jan"] could return back to you a meaningful value (if the key jan exists)
  • you can use isset (opens in a new tab)
  • hadling file uploads via $_FILES (opens in a new tab) allows you to access lots of different meta information about files, which would be useful when sending information to the server.
  • use include (opens in a new tab) as a way of importing functions and classes from other files. It doesn't have to be mentioned at the top of your file, just before you want to start referencing that code.
    • include "hello.php";

PHP Forms and Validation

Here we go over security issues related to PHP forms (opens in a new tab) and how to ensure that we validate any information that is being passed in.

PHP allows us to be retrieve information submitted directly from users. Not just any users. It allows ANY USER ON THE INTERNET to submit information. Based on what the internet has produced over the years, I don't need to tell you that it would be wise for us to put some guardrails in place.

I also don't need to provide links to any of it either.

Some ways to protect us from any security issues is by validating the data to ensure that it's correct. We can do some of this by ensuring that data that is provided to us is in the correct format. We should also do this validation both on the client side and server side. To learn more about form validation, you can visit W3Schools site (opens in a new tab)

Intro to Regex

Because validation is so important, here we go over the idea of regex (opens in a new tab) which will help us apply it to PHP.

While there are lots of sites that allow you do this type of thing, in the video I used RegEx Pal (opens in a new tab), since I liked the syntax highlighting. If you prefer another, feel free to mention it in the class discord under #cool-resources

Here is a small table having some of the most popular characters that we reviewed, though the video goes over more.

Regex symbolExplanation
.matches any single character (except a line break)
*matches 0 or more of the preceding character/group
+matches 1 or more preceding character/group
?matches 0 or 1 of the preceding character/group
()used to make a group
[]used to make a special group where you match one from the group

Advanced Regex

Because regex is so powerful and useful, we continue with our discussion of regex (opens in a new tab)

While we went over the idea of negation ([^0-9]), there was also some discussion of start (^) and end ($) anchors to make sure the string matches the beginning or the end. In addition, we briefly discuss the use of the curly braces {} to provide maximum or minimum number of repetitions of tokens that can be more specific than *, ?, or +.

We also discussed how to use it particularly in PHP where we place the regex within this string "/[A-Za-z0-9]{5}/" to denote when we want to discuss a regex string and how we use preg_match (opens in a new tab) and preg_split (opens in a new tab).

FYI: visit W3Schools page on Regular Expressions for further information (opens in a new tab)