Session 3.1 – Introduction to PHP
Module 3: PHP Basics | Duration: 1 hr
Learning Objectives
By the end of this session, students will be able to:
- Understand what PHP is and its role in web development
- Identify the key features and capabilities of PHP
- Write and execute basic PHP programs
- Understand PHP syntax and code structure
- Embed PHP code within HTML documents
- Use different types of comments in PHP
Introduction to Server-Side Scripting
Before diving into PHP, it's essential to understand the concept of server-side scripting. Unlike client-side languages like JavaScript that run in the browser, server-side scripts execute on the web server and send the results to the client.
Client-Side Scripting
- Executes in the browser
- Visible to users (view source)
- Limited access to server resources
- Examples: JavaScript, HTML, CSS
Server-Side Scripting
- Executes on the web server
- Code hidden from users
- Full access to server resources
- Examples: PHP, Python, Node.js, Ruby
What is PHP?
Definition
PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language especially suited for web development and can be embedded into HTML.
Key Facts About PHP
Created in 1994
Originally created by Rasmus Lerdorf as "Personal Home Page Tools"
78% of Websites
Powers approximately 78% of all websites with a known server-side language
Open Source
Free to download and use under the PHP License
Popular Platforms Built with PHP
WordPress, Facebook, Wikipedia, Slack, Etsy, MailChimp, and many more major platforms rely on PHP.
PHP Features and Capabilities
Simple & Fast
Easy to learn with a straightforward syntax similar to C and Java
Embedded in HTML
Can be seamlessly integrated within HTML code
Database Support
Supports MySQL, PostgreSQL, MongoDB, Oracle, and more
Cross-Platform
Runs on Windows, Linux, macOS, and Unix systems
Secure
Built-in security features for web applications
OOP Support
Object-oriented programming capabilities
What Can PHP Do?
- Generate dynamic page content
- Create, open, read, write, delete, and close files on the server
- Collect form data
- Send and receive cookies
- Add, delete, modify data in your database
- Control user access
- Encrypt data
- Output images, PDF files, and even Flash movies
Basic PHP Syntax
PHP Tags
PHP code is enclosed within special tags that tell the server where PHP code begins and ends:
<?phplt;?phplt;?php
// Your PHP code goes here
?>
Important Notes
- PHP files must have a
.phpextension - PHP code can be placed anywhere in the document
- The closing tag
?>can be omitted if the file contains only PHP code (recommended) - PHP statements end with a semicolon (
;)
PHP Statements
Every PHP statement must end with a semicolon:
<?phplt;?phplt;?php
echo "Hello, World!";
echo "Welcome to PHP";
?>
Case Sensitivity
In PHP:
- Keywords (if, else, while, echo, etc.) are NOT case-sensitive
- Variables ARE case-sensitive
<?phplt;?phplt;?php
// These are all valid and equivalent
ECHO "Hello";
echo "Hello";
EcHo "Hello";
// But variables ARE case-sensitive
$name = "John";
$NAME = "Jane";
// $name and $NAME are different variables!
?>
Your First PHP Program
Example 1: Simple Output
<?phplt;?phplt;?php
echo "Hello, World!";
?>
Hello, World!
Example 2: Using Variables
<?phplt;?phplt;?php
$greeting = "Hello";
$name = "Alice";
echo $greeting . ", " . $name . "!";
?>
Hello, Alice!
Example 3: Multiple Echo Statements
<?phplt;?phplt;?php
echo "<h1>Welcome to PHP</h1>";
echo "<p>PHP is a powerful server-side language.</p>";
echo "<p>It can generate dynamic web content.</p>";
?>
Welcome to PHP
PHP is a powerful server-side language.
It can generate dynamic web content.
Embedding PHP in HTML
One of PHP's greatest strengths is its ability to be embedded directly within HTML code. This allows for dynamic content generation.
Example: Mixed HTML and PHP
<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<?php
$currentTime = date("h:i A");
$currentDate = date("F j, Y");
echo "<p>Current time: " . $currentTime . "</p>";
echo "<p>Today's date: " . $currentDate . "</p>";
?>
<p>This is regular HTML content.</p>
</body>
</html>
Multiple PHP Blocks
You can have multiple PHP code blocks in a single file:
<!DOCTYPE html>
<html>
<body>
<?php $title = "My Page"; ?>
<h1><?php echo $title; ?></h1>
<p>Content goes here</p>
<?php
for ($i = 1; $i <= 5; $i++) {
echo "<p>Line " . $i . "</p>";
}
?>
</body>
</html>
Short Echo Tag
PHP 5.4+ supports a short echo tag: <?= $variable ?> which is equivalent to <?phplt;?phplt;?php echo $variable; ?>
Session Summary
Key Points
- PHP is a server-side scripting language designed for web development
- PHP code executes on the server and sends HTML to the client
- PHP is open-source, cross-platform, and powers millions of websites
- PHP code is enclosed in
<?phplt;?phplt;?php ... ?>tags - All PHP statements must end with a semicolon (;)
- PHP keywords are case-insensitive, but variables are case-sensitive
- Comments can be single-line (//) or multi-line (/* */)
- PHP can be seamlessly embedded within HTML code
- The
echostatement is used to output text and HTML
Next Session Preview
In the next session, we will explore PHP Variables and Data Types, learning how to store and manipulate different types of data in PHP.
Comments in PHP
Comments are used to document code and are ignored by the PHP engine. They help make code more readable and maintainable.
Types of Comments
1. Single-Line Comments
2. Multi-Line Comments
Best Practices for Comments