PHP: Comprehensive Developer Guide
1. Installation and Environment: Laragon
We will use Laragon, which provides a lightweight, fast, and flexible environment for PHP development.
Why Laragon?
*
Performance: It is faster and more optimized compared to alternatives like XAMPP.
*
Flexibility: You can easily switch between Apache and NGINX servers.
*
Modern Features: It offers features like automatic virtual hosts and SSL support.
Installation Steps
1.
Download: Start the download from the Laragon.org website.
Install: Complete the installation by following the "Next" steps. It typically installs to
C:\laragon.
Start: Open Laragon and click the "Start All" button to activate the servers.
- Test: Go to
http://localhostin your browser. If you see the Laragon welcome screen, the installation is successful.
2. Laragon Features and Usage
Laragon has specific capabilities that speed up the development process.
Automatic Domain Names (.test)
Laragon automatically converts every folder you create inside the www folder into a domain name.
*
Example: If you create a folder named C:\laragon\www\myproject, you can access it via http://myproject.test in your browser.
- This feature allows you to have a safe test environment without dealing with DNS settings.
SSL (HTTPS) Support
To run your projects with the https:// protocol, simply select Apache > SSL > Enabled from the Laragon menu.
Tools
Laragon includes tools such as Terminal, MySQL management (HeidiSQL/PhpMyAdmin), and service management.
3. PHP Basics and Syntax
PHP codes can be embedded in HTML and run on the server side.
Syntax Rules
PHP code blocks start with <?php and end with ?>.
<h1><?php echo "Hello, World!"; ?></h1>
In this example, the echo command is used to print text to the screen.
Variables and Data Types
Variables start with the $ sign. PHP automatically determines the data type.
$name = "Hamza"; // String
$age = 25; // Integer
.
Conditions and Loops
if-else is used for logic, and for or while are used for loops.
$number = 10;
if ($number > 0) {
echo "Positive";
}
for ($i = 0; $i < 5; $i++) {
echo $i;
}
.
Functions
Functions can be defined to avoid code repetition.
function add($a, $b) {
return $a + $b;
}
echo add(5, 3);
.
4. Data Transfer Between Pages
There are four main ways to move data from one page to another in web applications:
1.
Forms (POST/GET): The most common method for receiving data from users.
// receiver.php
$name = $_POST['name'];
2.
URL Parameters (GET): Sends data via the link (e.g., page.php?id=5).
- Session: Stores data on the server-side. Ideal for user login processes.
session_start();
$_SESSION['user'] = "Ahmet";
4.
Cookies: Stores small pieces of data in the browser (client-side).
5. Database Operations (CRUD)
You can perform Create, Read, Update, and Delete operations on a MySQL database with PHP.
Connection (MySQLi Example)
$conn = new mysqli("localhost", "root", "", "database_name");
if ($conn->connect_error) { die("Error: " . $conn->connect_error); }
.
Basic Operations
*
Create (Insert): Data is saved using the INSERT INTO query.
*
Read (Select): Data is fetched using the SELECT query and typically printed using a while loop.
*
Update: Existing data is modified using the UPDATE query.
*
Delete: Data is removed from the table using the DELETE query.
6. Project: Building a Comprehensive Blog Site
Let's build a blog site with database connection and a membership system to reinforce what we've learned.
1. Database Setup
Create a database named blog and create users (kullanicilar) and posts (yazilar) tables inside it.
CREATE TABLE users (id INT..., username VARCHAR...);
CREATE TABLE posts (id INT..., title VARCHAR..., content TEXT...);
.
2. File Structure
Organize your project into folders:
*includes/: Common files like connection, header, footer.
*pages/: Login, register, post listing pages.
*actions/: Background codes where form processing happens.
*css/: Design files.
3. Database Connection (PDO)
In this project, we will use the PDO structure, which is safer.
// includes/connect.php
try {
$db = new PDO("mysql:host=localhost;dbname=blog", "root", "");
} catch (PDOException $e) {
die("Error: " . $e->getMessage());
}
4. Membership System (Login/Register)
*
Register: Save user passwords to the database by encrypting them with password_hash.
*
Login: Verify the password with password_verify and if successful, start a session with $_SESSION.
*
Security: Check for sessions at the beginning of pages to block non-logged-in users or hide menus.
5. Content Management
Adding Posts: Only logged-in users can add posts. Data from the form is saved with the
INSERTcommand.
Listing: All posts are fetched with SELECT * FROM posts and listed on the page with a foreach loop.
6. Conclusion
With this project, you have combined PHP's building blocks, session management, and database interaction in a real-world scenario. You can run the application at http://localhost/blog.