UNIT 1: INTRODUCTION TO PHP (Highly Detailed Explanation)
1. What is PHP?
PHP (Hypertext Preprocessor) is a widely-used, open-source, general-purpose scripting language
that is especially suited for web development and can be embedded into HTML. It is executed on
the server, and the output is sent to the client browser in plain HTML format. PHP was originally
created by Rasmus Lerdorf in 1994. It started as a set of CGI scripts for tracking website visitors and
evolved into a full-fledged language.
Why use PHP?
- Easy to learn for beginners
- Efficient for professionals
- Integrates well with HTML, CSS, and JavaScript
- Offers powerful database connectivity (especially MySQL)
- Secure and scalable
2. Features of PHP
- Open Source: Freely available
- Simplicity: Clean syntax, similar to C/JavaScript
- Platform Independent: Runs on Windows, Linux, Unix, macOS
- Database Support: Native support for MySQL, PostgreSQL, SQLite, etc.
- Server-Side Execution
- Error Reporting
- Large Community
- Secure and Scalable
3. PHP Syntax
PHP scripts are enclosed within <?php ?> tags.
Example:
<?php
echo "Hello, world!";
?>
You can mix HTML and PHP together:
<html>
<body>
<h2>Today is:</h2>
<?php
echo date("l, d F Y");
?>
</body>
</html>
4. Structure of a PHP Program
- Opening Tags: <?php
- Variable Declarations: $number = 10;
- Control Structures (if, switch, loops)
- Functions
- Output Statements: echo "Result: " . $number;
- Closing Tag (Optional)
5. PHP Variables
PHP is a loosely typed language.
Declaration:
$var1 = "Welcome";
$var2 = 123;
Rules:
- Start with $
- Start with letter or underscore
- No special characters or spaces
- Case-sensitive
6. Data Types in PHP
- String: "Hello"
- Integer: 100
- Float: 10.5
- Boolean: true, false
- Array: ["apple", "banana"]
- Object: new ClassName()
- NULL: NULL
- Resource: fopen()
7. Comments in PHP
- Single-line: // or #
- Multi-line: /* ... */
8. Output Statements
- echo: echo "Hello!";
- print: print "Hello!";
Note: echo is faster and can take multiple parameters.
9. PHP Operators
- Arithmetic: +, -, *, /, %
- Assignment: =, +=, -=
- Comparison: ==, ===, !=
- Logical: &&, ||, !
- String: . (concatenation)
10. Conditional Statements
- if, if-else, if-elseif-else
- switch statement for multi-condition checking
11. Loops in PHP
- for: for ($i = 1; $i <= 5; $i++)
- while: while ($i <= 5)
- do-while
- foreach: for arrays
12. Nesting of Loops
Useful for matrices and multi-dimensional data.
Example:
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo "$i x $j = " . $i*$j;
13. Error Reporting in PHP
Use error_reporting(E_ALL); and ini_set("display_errors", 1); to enable error visibility during
development.
14. Best Practices
- Use === instead of ==
- Validate inputs
- Use prepared statements
- Comment code properly
- Separate logic and presentation
15. Real-World Use Case: Form Handling
<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!";
?>
Conclusion:
Unit 1 covers core PHP elements like syntax, variables, data types, conditionals, loops, and
structure. Mastering these is essential for advanced PHP topics.