Contents
- 1 Common questions beginners ask about PHP
- 1.1 What exactly is PHP, and why does it work the way it does?
- 1.2 How does PHP actually handle the data people send you through forms?
- 1.3 Why do PHP variables look so weird with the dollar sign?
- 1.4 What's the difference between a function and what PHP gives you built-in?
- 1.5 What's this constant thing, and why would you use it instead of a variable?
- 1.6 How do PHP and HTML actually work together?
- 1.7 What's AJAX, and why would JavaScript and PHP work together?
- 1.8 What are sessions, and how do they let you remember a user?
- 1.9 What's the difference between PHP 5 and PHP 7?
- 1.10 What are namespaces, and why would you use them?
- 1.11 How do you actually execute a PHP script?
- 1.12 What are common error types in PHP?
- 1.13 What are arrays, and when would you use them?
- 1.14 What does it mean when someone says "type hinting"?
- 1.15 How do you work with files in PHP?
- 1.16 What's autoloading, and why does it matter?
- 1.17 What's the relationship between PHP and databases?
- 1.18 Getting started is just getting started
Common questions beginners ask about PHP
There's this moment every developer remembers. You're staring at your first PHP file, and the cursor blinks back at you like a patient teacher waiting for the obvious question you're about to ask. The thing is, that question—whatever it is—has been asked thousands of times before. And that's not a weakness. It's the beginning of understanding something real.
When I started with PHP, I had so many questions that I felt embarrassed asking them. They seemed too basic, too foundational. But here's what I learned: those foundational questions matter. They matter because they build the mental model that everything else rests on. They matter because getting them right early means you avoid spending months debugging something that should have been clear from the start.
This article is for those early moments. Not to dismiss your questions, but to honor them—to sit down with you and talk through the things that everyone wonders about when they first encounter PHP. These aren't the advanced concepts that impress people at conferences. These are the questions that make you dangerous in the best way: competent, aware, and ready to actually build something.
What exactly is PHP, and why does it work the way it does?
Let me be direct. PHP is a server-side scripting language. That means the code runs on a server, not in your browser. When someone visits your website, PHP wakes up, does its work, and sends back HTML to their browser. Your visitor never sees the PHP itself. They see the result.
This matters more than it sounds. Unlike JavaScript, which runs in the browser and can be inspected by anyone who opens developer tools, PHP stays on the server. That's power and responsibility all at once.
PHP was created by Rasmus Lerdorf back in 1994. It wasn't designed as some grand vision of what web development should be. It started as something practical—a way to generate dynamic web pages. That pragmatic origin is baked into PHP's DNA. It's not pristine or elegant by academic standards, but it works. It works at scale. It works for everything from small personal projects to massive enterprise systems that handle millions of transactions daily.
What makes PHP unique is that it's built specifically for web development. It understands HTML. It understands databases. It understands forms, cookies, sessions, and all the messy reality of how websites actually function. You can embed PHP directly into HTML, which means you can start small—just a few lines of code in a template—and grow from there.
PHP is free. It's open-source. It runs on virtually every platform: Windows, Linux, macOS. It plays well with MySQL and other databases. These aren't small things. They're why PHP powers so much of the web. WordPress, Laravel, Symfony—they all exist because PHP makes certain things practical.
How does PHP actually handle the data people send you through forms?
This is where the interaction happens. Someone fills out a form on your website. They click submit. Then what?
PHP receives that data through two main channels: $_GET and $_POST. These are superglobals—special PHP variables that automatically contain data from requests.
$_GET collects data sent through the URL itself. When you search something on Google, the search term appears in the address bar. That's GET. It's visible, which means it's great for things like search queries or filters, but terrible for passwords or sensitive information. Anyone can see it. Anyone can modify it. $_GET data is limited in size too—browsers and servers have limits on how long URLs can be.
$_POST is different. The data is sent in the body of the request, hidden from the URL. It's more secure because someone casually glancing at a screen won't see the data. POST can handle larger amounts of information, which is why it's used for file uploads, login forms, and anything sensitive. It's the right choice for form data in most cases.
Here's something crucial: just because data came from a form doesn't mean it's safe. A beginner's mistake—I made it—is trusting form data. Someone could modify the form's HTML before submitting it. Someone could send requests directly to your PHP without using the form at all. Always validate. Always sanitize. Treat every piece of data like it might be malicious, because it might be.
PHP gives you ways to do this. You can check if data exists, if it's the right type, if it matches a pattern. This is not paranoia. This is professionalism.
Why do PHP variables look so weird with the dollar sign?
The first time you see $name = "Alice"; your brain might stumble. Why the dollar sign? Why not just name = "Alice"; like in other languages?
The dollar sign is PHP's way of saying "This is a variable, not a literal word." It's a visual signal. When you're reading PHP code and you see a $, you instantly know something's being stored or retrieved. When you see plain text without a dollar sign, you know it's something else—a function name, a constant, a keyword.
Variable names have rules. They must start with a letter or underscore, not a number. They can contain letters, numbers, and underscores, but nothing else. No spaces, no special characters like + or -. They're case-sensitive, so $name and $Name are completely different variables. This matters. It'll trip you up. You'll create $userName in one place and $username in another and wonder why things don't work.
The dollar sign convention might feel weird at first, but it becomes natural quickly. It's one of PHP's signatures, and once you've written a few hundred lines, you won't think about it anymore.
What's the difference between a function and what PHP gives you built-in?
PHP comes with hundreds of functions already built in. echo prints text to the screen. strlen measures a string's length. array_push adds something to an array. These are gifts—decades of decisions about what web developers actually need, baked into the language itself.
But PHP also lets you write your own functions. You can create a function that does something specific to your application, something PHP couldn't have predicted. You write it once, test it, and then use it over and over.
The difference matters psychologically more than technically. Built-in functions are thoroughly tested and optimized. They're fast. They're reliable. When you use an internal function, you're trusting the whole PHP community's work. That's powerful.
User-defined functions are your creativity. They're where you solve problems that are specific to your situation. A function that validates emails in your format. A function that calculates pricing based on your business rules. A function that fetches and formats data the way your templates need it.
Both kinds matter. Beginners often try to write their own version of something that already exists in PHP, not knowing. That's a learning moment. Read the documentation. Understand what PHP provides. Then build on top of it, not instead of it.
What's this constant thing, and why would you use it instead of a variable?
Constants look similar to variables, but they're fundamentally different. You define a constant using the define() function, and once it's defined, it cannot be changed. The value is locked.
define("SITE_NAME", "My Website");
define("DATABASE_HOST", "localhost");
define("API_TIMEOUT", 30);
After that point in your script, SITE_NAME will always be "My Website". You don't use a dollar sign with constants. You just write SITE_NAME, not $SITE_NAME. If you try to redefine a constant, PHP will ignore it. If you try to undefine it, PHP will complain.
Why would you want something you can't change? Think about values that don't change. Your site's name. Your database host. The number of items per page. These things are fixed for a given version of your application. They might change when you deploy the next version, but during this deployment, they're stable.
Constants are promises. They say to the next person reading your code—whether that's your future self three months from now or a colleague—"This value never changes." That clarity is valuable. It prevents accidental modifications. It makes code easier to understand because there's less hidden state changing around.
Configuration values, fixed strings, important numbers—these live in constants. Variables are for data that changes during execution. Constants are for the framework your code runs within.
How do PHP and HTML actually work together?
This is the question that separates understanding PHP from just memorizing syntax. Many beginners think PHP and HTML are interchangeable, or that HTML is somehow a subset of PHP. They're not. They're different creatures that work together beautifully.
HTML is static. It's just text describing structure. A browser reads HTML and renders it. That's the end.
PHP is dynamic. It's code that executes on a server and generates HTML as output. A browser never sees the PHP itself. The browser only sees HTML—which might have been created by PHP a microsecond before it arrived.
This distinction is profound. It means you can write PHP that generates different HTML based on what's in a database. You can write PHP that creates HTML only if certain conditions are met. You can write PHP that combines data from multiple sources into a single HTML page. HTML can't do any of that alone. PHP can't render a page without HTML. Together, they're complete.
Here's the mental model: PHP is a chef preparing a meal (the HTML) in the kitchen (the server). When you sit at a restaurant (your browser), you don't see the kitchen. You don't see the process. You just see the finished plate. That's HTML. But what's on the plate depends entirely on what the chef decided to prepare, and that decision is PHP.
In practice, you write PHP code mixed with HTML. You might have a template file that's mostly HTML, with a few PHP tags where dynamic data needs to appear. The PHP executes first, filling in the blanks, and then the complete HTML goes to the browser.
<h1><?php echo $pageTitle; ?></h1>
<p>Welcome, <?php echo $userName; ?></p>
When that executes, if $pageTitle is "Home" and $userName is "Alice", the browser receives:
<h1>Home</h1>
<p>Welcome, Alice</p>
The PHP is gone. Only HTML remains. But the HTML is exactly what the user needed to see at that moment. That's the magic.
What's AJAX, and why would JavaScript and PHP work together?
You might have encountered the word AJAX and wondered if it's another language to learn. It's not. It's a technique.
AJAX stands for Asynchronous JavaScript and XML. Let's break that down. JavaScript runs in the browser. Asynchronous means the browser doesn't freeze while waiting for a response. XML (or more commonly these days, JSON) is just a format for exchanging data.
What AJAX lets you do is have JavaScript make requests to PHP on the server without reloading the page. The user keeps working. Somewhere in the background, JavaScript has sent a request to your PHP code. PHP processes it, returns data, and JavaScript receives it and updates the page.
A practical example: You're typing into a search box. With each keystroke, JavaScript sends what you've typed so far to PHP. PHP searches the database and returns matching results. JavaScript displays those results below the search box, all without the page refreshing.
This creates the smooth, responsive experience you expect from modern websites. It's not magic. It's just JavaScript and PHP having a conversation while the human watches.
The conversation flow is simple:
- User does something in the browser
- JavaScript detects it and sends a request to a PHP endpoint
- PHP processes the request and returns data (usually JSON)
- JavaScript receives the response and updates the page
Both languages do what they're best at. JavaScript handles the browser experience. PHP handles the server logic and data access. Neither one needs to do the other's job.
What are sessions, and how do they let you remember a user?
Here's a scenario: someone logs into your website. They provide a username and password. You verify it. Now they click on a different page. How does that new page know they're logged in? The HTTP protocol doesn't remember. Each request is independent. Your server would have to ask them to log in again on every single page.
Sessions solve this. A session lets you store information about a user across multiple pages and across multiple requests, for as long as they're visiting your site.
When a user first visits, you call session_start() at the beginning of your PHP script. This must happen before any output is sent to the browser—before any HTML, before any echo statements. Once the session has started, PHP creates a session ID and stores it in a cookie on the user's browser. That cookie comes along with every future request from that user.
Then you can store data in the $_SESSION superglobal:
session_start();
$_SESSION['user_id'] = 42;
$_SESSION['username'] = 'alice';
$_SESSION['logged_in'] = true;
On the next page, you call session_start() again, and you can access that data:
session_start();
if ($_SESSION['logged_in']) {
echo "Welcome back, " . $_SESSION['username'];
}
The actual session data lives on the server. The browser just holds the ID. This is important because it means the user can't tamper with the session data. They can't say "Hey, make me an admin." They have only the ID. The server controls what that ID means.
Sessions aren't permanent. By default, they last until the user closes their browser or until a timeout period passes (usually 24 minutes). If you need data to persist longer, you'd store it in a database and tie it to the user's account. Sessions are for the current visit.
This is profound: sessions are how websites remember you while you're there. They're how shopping carts work. They're how you stay logged in as you click around. They're one of the fundamental patterns of web development.
What's the difference between PHP 5 and PHP 7?
If you're starting now, you probably don't need to worry about this. But it's worth knowing because legacy systems still run PHP 5, and understanding what changed helps you understand why modern PHP is designed the way it is.
The biggest change was object-oriented programming support. PHP 5 introduced real OOP features—access modifiers like public, protected, and private. Interfaces. Abstract classes. Constructors and destructors. These aren't just nice-to-have bells and whistles. They're the foundation of modern organized code.
Before PHP 5, you could write objects, but they were clunky. You had to be creative. PHP 5 made OOP feel native. Then PHP 7 came along and made it even better, with type declarations for function parameters and return values. This matters because types catch errors early. If you say a function should return an integer but it returns a string, PHP 7 will yell at you immediately rather than letting subtle bugs creep through.
The practical upshot: if you're learning now, learn modern PHP. Learn PHP 7 or 8. The foundations are solid. The language has matured. You're learning something stable, not something you'll need to relearn in two years.
What are namespaces, and why would you use them?
Namespaces are about preventing chaos as your code grows. Imagine you write a class called User. Someone on your team writes a class called User. Someone else does too. Suddenly you have three User classes and PHP doesn't know which one you mean when you try to use it.
Namespaces let you organize code into logical groups. You might have App\Models\User and App\Controllers\User and External\Library\User. Each one is distinct. Each one exists in its own namespace. When you need a specific one, you specify the namespace.
namespace App\Models;
class User {
// User model
}
In another file:
namespace App\Controllers;
class User {
// User controller
}
They're different. They don't conflict. Your code is organized by purpose and responsibility.
Namespaces also encourage standardization. When you organize code with namespaces, you're likely to develop consistent patterns. App\Models contains data models. App\Controllers contains request handlers. App\Services contains business logic. This clarity helps both you and anyone working with your code.
Beginners can write excellent code without namespaces if their projects are small. But as your project grows, as you start using third-party libraries, as your team expands, namespaces become essential. They're not a burden. They're a tool that makes larger codebases manageable.
How do you actually execute a PHP script?
This might sound simple, but there's more to it than you might think. The most common way is through a web server. You put your PHP file on a server running web server software like Apache or Nginx. When someone visits the URL pointing to that file, the web server triggers PHP, which executes the code and returns the result.
But there's another way: the command line. You can execute PHP scripts directly from the terminal without any web server involved. This is useful for background tasks, scheduled jobs, testing, or any situation where you need PHP to run on a schedule or independently.
php script.php
That's it. The script executes. Any output goes to the terminal. Any errors display there too.
Why would you do this? Imagine you have a task that needs to run every night—sending reminder emails, cleaning up old data, generating reports. You can write a PHP script and schedule it with a tool like cron on Linux. Every night at 2 AM, that script runs. No web browser involved. PHP just does its job and moves on.
This flexibility is one of PHP's strengths. It's not just for web pages. It's a scripting language with web development as its specialty, not its limitation.
What are common error types in PHP?
Errors are inevitable. Understanding them helps you fix them faster and, more importantly, prevent them.
Parse errors (also called syntax errors) mean PHP can't even read your code. You forgot a semicolon, or you have mismatched quotes, or you left off a closing brace. PHP stops immediately and tells you roughly where the problem is. These are the easiest to fix because PHP is explicit about them.
Fatal errors mean PHP understood your code syntactically, but something went wrong while it was running. You tried to call a function that doesn't exist. You tried to access a property on something that's not an object. You ran out of memory. These stop execution. Anything after a fatal error doesn't happen.
Warnings mean something looks suspicious but PHP is going to keep going anyway. You accessed an array key that doesn't exist. You passed the wrong number of arguments to a function. These are valuable signals. They usually mean a bug in your logic, even if your code doesn't completely break.
Notices are even milder. They're informational. They say "Hey, just so you know, something here is a bit odd." A beginner might ignore them, but experienced developers take them seriously because they catch real bugs early.
The key is configuration. PHP can be configured to report different levels of errors. In development, you want it loud and clear. You want to see everything. In production, on a live server, you might want warnings and notices hidden from users (though still logged for your reference).
Setting error reporting is one of the first things to understand:
error_reporting(E_ALL);
ini_set('display_errors', 1);
This says "Report everything" and "Show it to me." Do this in development. In production, you'd log errors to a file instead of displaying them to users.
What are arrays, and when would you use them?
An array is a container. It holds multiple values. That's the simple definition, but arrays are powerful because of how flexible they are.
The most basic array is a list:
$colors = array("red", "yellow", "blue");
You access items by their position, starting from zero:
echo $colors[0]; // outputs: red
echo $colors; // outputs: blue
This is useful when order matters. A list of search results. A list of comments. A list of usernames.
But arrays can also be associative. Instead of numeric indices, you use keys:
$person = array(
"name" => "Alice",
"age" => 30,
"email" => "alice@example.com"
);
echo $person["name"]; // outputs: Alice
This is more readable. $person["name"] is clearer than $person. It's closer to how you think about data.
Arrays can be nested:
$users = array(
array("name" => "Alice", "age" => 30),
array("name" => "Bob", "age" => 25)
);
echo $users[0]["name"]; // outputs: Alice
Now you have a structure that represents multiple people, each with properties. This is how you'd fetch multiple records from a database and work with them.
Strings are also arrays of characters, sort of. You can access individual characters:
$text = "hello";
echo $text[0]; // outputs: h
Arrays are so fundamental to PHP that entire categories of functions exist just to manipulate them. count() tells you how many items. array_push() adds an item. array_merge() combines arrays. array_filter() keeps only items that match a condition. Once you're comfortable with arrays, these functions become tools you reach for constantly.
What does it mean when someone says "type hinting"?
Type hinting is a way of being explicit about what kind of data a function should receive and what it should return. It's a promise and a contract.
function add(int $a, int $b): int {
return $a + $b;
}
This says: "This function expects two integers and will return an integer." If you try to pass something else, PHP will either convert it (if possible) or throw an error.
Why is this valuable? Because it catches mistakes. If you accidentally pass a string when you meant to pass a number, you find out immediately instead of hours later when some calculation is wrong.
As code grows, type hints make it easier to understand what functions expect. You don't have to guess or read documentation. You can see the types right there.
Modern PHP encourages type hints. They're not required for simple scripts, but they become increasingly important as projects grow and teams expand. A beginner can write good PHP without them. A professional learns them early so the habit is natural.
How do you work with files in PHP?
Working with files is part of many web applications. You might need to read configuration files, process uploads, generate reports, or log information.
PHP gives you functions for this. file_get_contents() reads an entire file into a string. file_put_contents() writes data to a file. fopen() opens a file for reading or writing. fread() reads from an open file. fwrite() writes to an open file. fclose() closes a file when you're done.
// Read a file
$content = file_get_contents("data.txt");
// Write to a file
file_put_contents("output.txt", "Hello, World!");
File handling involves error checking. What if the file doesn't exist? What if you don't have permission to read it? What if the disk is full? Beginners often skip this, but it matters. You should always verify that operations succeeded before assuming they did.
if (file_exists("data.txt")) {
$content = file_get_contents("data.txt");
} else {
echo "File not found";
}
There's also the concept of file uploads. When someone submits a form with a file, PHP receives it and makes it available through the $_FILES superglobal. You then need to validate it (is it really a file? is it the right type? is it the right size?) and move it somewhere safe on your server.
File handling quickly becomes nuanced, but the basics are straightforward: open, read or write, close. The best practice is always checking that each step succeeded before moving to the next.
What's autoloading, and why does it matter?
Autoloading is a way of automatically loading classes when you need them, without having to manually include every file.
Without autoloading, your PHP files are full of include and require statements at the top:
require "classes/User.php";
require "classes/Product.php";
require "classes/Database.php";
As your project grows, this list becomes endless. It's tedious and error-prone. You might forget to include something, or include something twice.
Autoloading says: "When I try to use a class that hasn't been loaded yet, go find the file automatically." PHP has a standard way of doing this, called PSR-4 (PHP Standards Recommendation 4). Most modern frameworks use it.
The autoloader expects classes to be organized in a directory structure that matches the namespace. A class in namespace App\Models would live in the file app/Models/ClassName.php. When you try to use it, the autoloader looks in the right place and includes it automatically.
use App\Models\User;
$user = new User(); // The autoloader finds and includes the User class
This is cleaner, more flexible, and it scales. Large projects with hundreds of classes become manageable. You don't have to think about file inclusion. You just use the class, and it works.
Autoloading is built into modern frameworks and into Composer (PHP's package manager). As a beginner, you might not write your own autoloader, but understanding how it works will help you use frameworks effectively and structure your own projects better.
What's the relationship between PHP and databases?
PHP shines when it's connected to a database. A database stores data permanently. PHP retrieves that data and presents it dynamically.
The flow is simple: a user requests something, PHP queries the database, PHP formats the results as HTML, and the HTML goes to the user. If the user is updating something, PHP receives the new data, validates it, and stores it in the database.
Most commonly, you're using MySQL or PostgreSQL. PHP connects to the database using either MySQLi (MySQL Improved) or PDO (PHP Data Objects). Both are fine for beginners, though PDO is more flexible because it works with multiple database systems.
A basic query looks like:
$result = mysqli_query($connection, "SELECT * FROM users WHERE age > 18");
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'];
}
You're asking the database for data, getting back results, and processing them one row at a time.
Creating, reading, updating, and deleting data—the CRUD operations—are the bread and butter of web development. You'll do these constantly. PHP makes them straightforward.
The important thing to understand is that PHP is temporary. A PHP script runs, does something, and stops. The database persists. Data survives even if the server crashes. That's why databases matter. They're where your application's memory lives.
Getting started is just getting started
These questions and their answers are the foundation. They're not the whole story. PHP is vast. There's security to understand, performance to consider, design patterns to learn, frameworks to explore. But all of that is built on top of these basics.
The fact that you're asking these questions means you're approaching this seriously. You're not just copying code and hoping it works. You're trying to understand what's happening. That habit—that curiosity—is more valuable than any specific piece of knowledge. Knowledge changes. Frameworks come and go. Languages evolve. But the habit of understanding what your code does, of asking questions until the answers make sense, that's permanent.
When you're debugging something at midnight because a feature isn't working and you remember something from this article, you'll feel that moment when it clicks. When you understand why your code is doing what it's doing instead of just doing what you told it to do, that understanding stays with you.
PHP has powered millions of websites. It powers sites you use every day. It's not cool or trendy anymore. It's something quieter and more valuable: it's reliable. It's practical. It's built by people who use it, for purposes that matter.
Your journey with PHP starts with these questions and these answers, but it doesn't end here. Each project teaches you more. Each mistake becomes a lesson. And somewhere down the road, you'll find yourself mentoring a beginner, answering these same questions, passing forward what you've learned—because this community grows by sharing, by asking, and by taking those early questions seriously enough to answer them well.