Introduction to PHP
PHP is a popular general-purpose scripting language especially suited to web development. It is fast, flexible, and pragmatic, powering everything from blogs to the most popular websites in the world.
Key Features
- Server-side scripting
- Easy database integration
- Open source and free
- Cross-platform compatibility
- Large community support
Installation
Install PHP from php.net or use a package manager:
sudo apt install php
Data Types
- String
- Integer
- Float
- Boolean
- Array
- Object
- NULL
Operators
- Arithmetic: +, -, *, /, %
- Assignment: =, +=, -=, etc.
- Comparison: ==, !=, <, >, <=, >=
- Logical: and, or, &&, ||, !
Conditional Statements
if ($a > $b) {
echo "a is greater than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is less than b";
}
Loops
for ($i = 0; $i < 5; $i++) {
echo $i;
}
while ($a < 10) {
$a++;
}
Functions
function greet($name) {
return "Hello, $name!";
}
Arrays
$arr = array(1, 2, 3);
echo $arr[0];
Form Handling
$name = $_POST['name'];
echo $name;
File Handling
$file = fopen("test.txt", "r");
while(!feof($file)) {
echo fgets($file);
}
fclose($file);
Other Features
- Sessions and cookies
- Object-oriented programming
- Exception handling