Okay, real talk: PHP gets a lot of hate. People call it ugly, outdated, whatever. But here's the thing—WordPress runs on PHP. Drupal runs on PHP. Facebook started on PHP. It powers a huge chunk of the web.
Learning PHP is worth it if you want to build dynamic websites. Let's get into it.
Why Should You Care?
PHP was built specifically for the web. Need a form that sends an email? PHP. Need to fetch data from a database and display it? PHP. Need to build a whole CMS? PHP's got you.
It's not pretty, but it works. And there's tons of documentation and tutorials out there.
Setting Things Up
sudo apt install php php-mysql php-curl php-gd php-mbstring php-xml
That one command installs PHP and all the common extensions you'll need.
Your First PHP Script
<?php
echo "Hello, World!";
?>
Save that as index.php, put it in your web server's document root, and visit the page. You've officially written PHP.
The <?php tells the server "hey, this is PHP code." The ?> says "okay, we're done with PHP." Everything between runs on the server and becomes HTML when it reaches the browser.
Variables: The $ Sign
PHP variables all start with a dollar sign. It's their thing.
$name = "cjboon";
$age = 25;
$is_active = true;
Simple. No var/let/const confusion. Everything's just $.
Arrays: Lists of Things
Arrays hold multiple values. Super useful.
// Regular array
$fruits = ["apple", "banana", "orange"];
echo $fruits[0]; // prints "apple"
// Associative array - like a dictionary
$user = ["name" => "cjboon", "email" => "test@test.com"];
echo $user["name"]; // prints "cjboon"
Associative arrays are everywhere in PHP. They're how objects work internally.
Making Decisions
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}
Same as most languages. If the condition is true, do this. Otherwise, do that.
Loops: Going Through Lists
// Loop through array
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
The foreach loop is probably the most used loop in PHP. It just grabs each item in the array, one by one.
Functions: Reusable Code
function greet($name) {
return "Hello, " . $name;
}
echo greet("cjboon");
The dot (.) joins strings together. Simple concatenation.
Getting Input From Users
When someone submits a form or visits a URL with parameters, PHP makes it easy to grab that data.
// URL like: example.com/page.php?name=cjboon
echo $_GET["name"]; // prints "cjboon"
// Form submitted with POST
echo $_POST["email"];
// Sessions - persist data across pages
$_SESSION["user"] = $user;
$_GET is for data in the URL (like search queries). $_POST is for form submissions. $_SESSION lets you remember who logged in.
Connecting To A Database
Most PHP apps need a database. Here's the modern way using PDO:
$pdo = new PDO(
"mysql:host=localhost;dbname=mydb",
"username",
"password"
);
$stmt = $pdo->query("SELECT * FROM users");
while ($row = $stmt->fetch()) {
echo $row["name"];
}
This is safe from SQL injection if you use prepared statements. But that's a topic for another day.
Where To Go From Here
You know enough to:
- Build a simple form that saves to a database
- Create a login system
- Make a blog from scratch
Start small. Break things. That's how you learn.