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.
- 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.
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
- Download CodeIgniter 3 from the official website.
- Extract the zip file into your local server's root directory (e.g.,
C:\xampp\htdocs\my_app). - Open
application/config/config.phpand set your base URL:$config['base_url'] = 'http://localhost/my_app/'; - Open
application/config/database.phpand 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);
}
}$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();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!