Introduction to PHP & CodeIgniter 3 20220507

🐘 Introduction to PHP & CodeIgniter 3

Build dynamic, database-driven web applications with ease. Learn the fundamentals of PHP and master the lightweight, lightning-fast CodeIgniter 3 framework.

📋 Prerequisites: To follow along, you should have:
  • Basic knowledge of HTML, CSS, and relational databases (MySQL).
  • A local development environment like XAMPP, MAMP, or WAMP.
  • Fundamental understanding of PHP syntax (variables, arrays, loops).

1. Why PHP in Modern Web Development?

Despite the rise of JavaScript frameworks, PHP still powers nearly 75% of the web, including giants like WordPress, Facebook (historically), and Laravel. It remains the most accessible language for server-side web development due to its easy deployment, massive community, and seamless integration with MySQL.

2. Enter CodeIgniter 3 (CI3)

Writing raw PHP for large applications can quickly become messy. This is where frameworks come in. CodeIgniter 3 is a powerful, lightweight PHP framework known for its:

⚡ Blazing Fast

CI3 has a very small footprint. It requires minimal configuration and executes incredibly fast.

🎓 Gentle Learning Curve

Unlike heavier frameworks, CI3 doesn't force you to use complex design patterns or strict naming conventions.

🛠️ Rich Built-in Libraries

Comes with pre-built libraries for database manipulation, form validation, session management, and file uploading.

Excellent Documentation

CI3 is famous for having some of the most readable and beginner-friendly documentation in the PHP ecosystem.

⚠️ Industry Context: CodeIgniter 3 has reached its official End of Life (EOL). For brand new enterprise projects, modern frameworks like Laravel or CodeIgniter 4 are recommended. However, CI3 is still massively used in legacy systems, freelance projects, and is a fantastic tool for learning the MVC pattern.

3. Understanding the MVC Architecture

CodeIgniter is built on the MVC (Model-View-Controller) pattern. This separates your application logic from its presentation.

🧠

Model

Handles data logic. It interacts with the database to fetch, insert, update, or delete records.

🎨

View

The presentation layer. These are your HTML files (often with embedded PHP) that the user actually sees.

🚦

Controller

The traffic cop. It receives the user's request, talks to the Model to get data, and passes it to the View.

4. Setting Up Your First Project

  1. Download CodeIgniter 3 from the official website.
  2. Extract the zip file into your local server's root directory (e.g., C:\xampp\htdocs\my_app).
  3. Open application/config/config.php and set your base URL:
    $config['base_url'] = 'http://localhost/my_app/';
  4. Open application/config/database.php and configure your MySQL credentials (hostname, username, password, database).

5. Creating Your First Controller

Controllers live in the application/controllers/ folder. Let's create a simple Pages.php controller.

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Pages extends CI_Controller { // This method will be called when you visit: /pages/view public function view($page = 'home') { // Check if the view file exists if (!file_exists('application/views/pages/'.$page.'.php')) { show_404(); } // Pass data to the view $data['title'] = ucfirst($page); // Load the views (Header, Page Content, Footer) $this->load->view('templates/header', $data); $this->load->view('pages/'.$page, $data); $this->load->view('templates/footer', $data); } }
Pro Tip: In CI3, you will use $this-> constantly. It refers to the current controller instance, allowing you to load libraries, models, and helpers seamlessly.

6. Building the View

Views are stored in application/views/. Let's create application/views/pages/home.php.

<h1><?php echo $title; ?></h1> <p>Welcome to our CodeIgniter 3 application!</p> <?php if (isset($articles)): ?> <ul> <?php foreach ($articles as $article): ?> <li><?php echo $article['title']; ?></li> <?php endforeach; ?> </ul> <?php endif; ?>

7. Fetching Data with Models

Never put database queries directly in your controller! Create a model in application/models/. CI3's Query Builder makes database interactions incredibly easy and secure.

<?php class News_model extends CI_Model { public function get_articles() { // Using CI3 Query Builder $query = $this->db->get('articles'); return $query->result_array(); } public function get_article_by_slug($slug) { $query = $this->db->get_where('articles', array('slug' => $slug)); return $query->row_array(); } }

To use this in your controller, simply load it and call the method:

// Inside your Controller method $this->load->model('news_model'); $data['articles'] = $this->news_model->get_articles();

🎯 Next Steps for CI3 Mastery

  1. Form Validation: Learn how to use the built-in form_validation library to sanitize and validate user input before it hits your database.
  2. Sessions & Authentication: Implement user login systems using CI3's session library.
  3. Security: Always enable $config['csrf_protection'] = TRUE; in your config file to prevent Cross-Site Request Forgery attacks.
  4. URL Routing: Customize your URLs in application/config/routes.php to make them SEO-friendly (e.g., mapping /news/123 to news/view/123).

Ready to build your first dynamic app?

PHP and CodeIgniter 3 provide a rock-solid foundation for understanding how server-side web applications truly work under the hood. Once you master CI3, transitioning to modern frameworks like Laravel will be a breeze! 🐘

If you found this guide helpful, please share it with your fellow backend developers!