...
PHP stands for Hypertext Preprocessor. It is a server-side scripting language designed for web development but also used as a general-purpose language.
✅ Runs on the server and generates dynamic HTML
✅ Open-source and easy to learn
✅ Powers websites like Facebook, WordPress, Wikipedia
✅ Often used with MySQL for backend development
PHP code is embedded within HTML using <?php ?> tags.
<?php
echo "Hello, Ardiland!";
?>
To run PHP, you need a local server (like XAMPP, WAMP) or upload to a web server.
Variables in PHP start with a $ symbol. No need to declare types.
<?php
$name = "Maya";
$age = 21;
?>
✅ $name is a string
✅ $age is an integer
Common data types include:
Integer ($age = 25;)
Float ($price = 19.99;)
String ($name = "Maya";)
Boolean ($isActive = true;)
Array ($fruits = ["apple", "banana"];)
Null ($x = null;)
<?php
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}
?>
<?php
for ($i = 0; $i < 5; $i++) {
echo $i;
}
?>
<?php
$i = 0;
while ($i < 5) {
echo $i;
$i++;
}
?>
Functions in PHP are defined using the function keyword.
<?php
function greet($name) {
return "Hello, " . $name;
}
echo greet("Christian");
?>
<?php
$colors = ["red", "green", "blue"];
echo $colors[0]; // red
?>
<?php
$user = ["name" => "Maya", "age" => 22];
echo $user["name"]; // Maya
?>
HTML + PHP Form Handling Example:
<form method="post" action="welcome.php">
Name: <input type="text" name="name">
<input type="submit">
</form>
welcome.php:
<?php
$name = $_POST['name'];
echo "Welcome, " . $name;
?>
✅ $_GET is used when method="get"
✅ $_POST is used when method="post"
Step-by-step:
Create a database in phpMyAdmin
Use mysqli_connect() to connect
<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
?>
✅ Use mysqli_query() to run SELECT/INSERT/UPDATE queries
✅ Always sanitize user input to prevent SQL injection
✅ Use htmlspecialchars() to prevent XSS
✅ Always validate form data
✅ Hash passwords with password_hash()
✅ Never trust user input directly in SQL
PHP is used in:
WordPress CMS
E-commerce platforms like WooCommerce, Magento
Web portals, membership systems
RESTful APIs (with Slim, Laravel)
Server-side scripting and automation