My struggle with php is with understanding the overall structure of how everything works together. Googling for troubleshooting help is only really useful once you’re in the detailed mechanics of a php error. It doesn’t really help when the problem (like mine).
I’ve spent way too many hours trying to figure these things out, and I finally met with my instructor and feel some hope, so I’m sharing my aha moments with you!
Think of the html page of a php site like the body of a car. The php functions are the engine. This isn’t a perfect analogy, but let’s run with it. If you want to go anywhere, the car body needs an engine. The “engine” is the page with the php functions. The html page sends data to the engine page. The engine page handles that data however you tell it to. Then it can direct the user to the next page, to the next “car”.
Let’s imagine the user enters a lot full of cars. You want them to follow a certain path. For each turn in the path, the user gets in a new car. Sometimes the car they are in is a dump truck and it drops them into the next car (header function). Some times they stay in the car they are in and the engine keeps the car running. Other times the user can get out of the car and get in another one all by themselves.
With that analogy in mind, here is a reliable way to set up a php file system for a noob like me:
Each html page that needs to do some function has an “engine” page that holds the php functions.
- For a form page use the <form action=”xxxx.php” method =”post”> to send the user’s form $_POST data to the engine.
- In the form page’s engine use: header(‘location:xxxx.php’); to steer the customer to the next page. (Hint: replace xxxx with the name of the page where you want to send the user. That’s really all there is to redirecting the user. Wow!).
- For a regular page that doesn’t use a form and hence doesn’t need to redirect the user, you can simply use: require(‘xxxx.php’); on the html page itself. The engine page doesn’t need to redirect the user.
- The html page can be its own “engine” by creating all the functions on the page instead of a separate file, but it’s good to separate the functions from the html because it’s easier to organize.
There are probably an infinite number of other ways to do this, but this is at least one way that works.
If planning to use sessions throughout the site
• session_start(); has to go on every page.
• session_start(); has to go above any html
As an emerging developer, it’s really hard to know when a variable is referencing another variable so it has to have that name or if it could be named whatever you want, like “tacos”.
At what point is the variable declared and at what point is it referencing the original declaration. It is hard to know. As my instructor said. When you have a baby, you can name it whatever you want, but after that you have to use the name you gave it. Same thing with variables. You can name them whatever you want when they are “born”, but after that you have to call it by its name. It would be helpful if there were a way to visually see when a variable is born in the code.
Isolate if the problem is happening:
- before the data goes in the database
- in the database
- after it comes out of the database
var_dump(‘xxxxx’); is very useful here
I hope this helps!
The info I’ve described seems simple now that I understand it. I’m sure an experienced developer think it’s obvious, but for a noob like me, it is certainly not obvious. I hope this helps you!