PHP (Hypertext Preprocessor) is a widely-used, open-source, server-side scripting language designed for web development. It is also a general-purpose language that can be embedded into HTML.
- Dynamic Web Content: Generates HTML dynamically on the server.
- Database Integration: Built-in support for databases like MySQL, PostgreSQL, and SQLite.
- Cross-Platform: Runs on various operating systems (Linux, Windows, macOS).
- Extensive Library Support: Standard libraries and third-party frameworks enhance functionality.
- Ease of Integration: Embeds directly into HTML and integrates with various web technologies.
- PHP Tags:
<?php echo "Hello, PHP!"; ?>
- Comments:
// Single-line comment # Another single-line comment /* Multi-line comment */
- Declared with a
$
prefix and no explicit type declaration (dynamic typing).$name = "PHP"; $version = 8.0; $isActive = true;
- Primitive:
string
,integer
,float
,boolean
- Compound:
array
,object
,callable
,iterable
- Special:
NULL
,resource
- Arithmetic:
+
,-
,*
,/
,%
- String:
.
(concatenation) - Comparison:
==
,===
,!=
,!==
,<
,>
,<=
,>=
- Logical:
&&
,||
,!
- Assignment:
=
,+=
,-=
,.=
if ($age > 18) {
echo "Adult";
} elseif ($age > 12) {
echo "Teenager";
} else {
echo "Child";
}
- For Loop:
for ($i = 0; $i < 10; $i++) { echo $i; }
- While Loop:
while ($count < 5) { echo $count++; }
- Foreach Loop:
foreach ($array as $key => $value) { echo "$key: $value"; }
- Definition:
function greet($name) { return "Hello, $name!"; } echo greet("PHP");
- Default Parameters:
function greet($name = "Guest") { return "Hello, $name!"; }
- Indexed Arrays:
$colors = ["red", "green", "blue"]; echo $colors[0]; // Outputs: red
- Associative Arrays:
$ages = ["Alice" => 25, "Bob" => 30]; echo $ages["Alice"]; // Outputs: 25
-
Class and Object:
class Car { public $brand; public function __construct($brand) { $this->brand = $brand; } public function honk() { return "Honk! I am a $this->brand."; } } $car = new Car("Toyota"); echo $car->honk();
-
Inheritance:
class ElectricCar extends Car { public $batteryLife; public function charge() { return "Charging the battery."; } }
- Reading a File:
$content = file_get_contents("example.txt"); echo $content;
- Writing to a File:
file_put_contents("example.txt", "Hello, PHP!");
- Using PDO (PHP Data Objects):
try { $pdo = new PDO("mysql:host=localhost;dbname=testdb", "username", "password"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $pdo->query("SELECT * FROM users"); foreach ($stmt as $row) { print_r($row); } } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
- Use
try
,catch
, andfinally
for exception handling:try { if (!file_exists("example.txt")) { throw new Exception("File not found"); } } catch (Exception $e) { echo $e->getMessage(); }
- Use strict types when possible:
declare(strict_types=1);
- Enable error reporting during development:
error_reporting(E_ALL); ini_set('display_errors', 1);
- Sanitize user input to prevent security vulnerabilities (e.g., SQL injection, XSS).
- Use prepared statements for database queries.
- Official PHP Manual: https://www.php.net/docs.php
- PHP: The Right Way: https://phptherightway.com/
- Composer: Dependency manager for PHP (https://getcomposer.org/)
PHP is a versatile language, especially suited for web development, with a rich ecosystem of tools and frameworks like Laravel, Symfony, and WordPress.