Ushbu hujjat PHP (Hypertext Preprocessor) dasturlash tili bo'yicha eng muhim tushunchalar, zamonaviy PHP 8.x imkoniyatlari, OOP (Ob'ektga Yo'naltirilgan Dasturlash) va ilg'or amaliyotlarni o'z ichiga olgan to'liq qo'llanmadir.
- PHP Kirish va Sintaksis
- O'zgaruvchilar va Ma'lumot Turlari
- Massivlar (Arrays) bilan Ishlash
- Funksiyalar va Arrow Functions
- OOP (Object-Oriented Programming)
- PHP 8+ Yangi Imkoniyatlari
- Exception & Error Handling
- cURL & HTTP REST API So'rovlari
PHP β bu asosan veb-dasturlash va backend tizimlar yaratish uchun mo'ljallangan mashhur skript tilidir.
<?php
// Oddiy konsolga chiqarish
echo "Hello, World!\n";PHP da o'zgaruvchilar $ belgisi bilan boshlanadi va PHP dinamik tiplangan til hisoblanadi.
<?php
$name = "Ozodbek"; // String
$age = 24; // Integer
$price = 99.99; // Float
$isDeveloper = true; // Boolean
$skills = ['PHP', 'Go']; // Array
$nothing = null; // NULLPHP da massivlar juda kuchli ma'lumot tuzilmasi hisoblanib, u ham indeksli, ham assotsiativ bo'lishi mumkin.
<?php
// Assotsiativ massiv
$user = [
"id" => 1,
"name" => "Ozodbek",
"role" => "Backend Developer"
];
// Massiv metodlari
array_push($user, "Laravel"); // Element qo'shish
$keys = array_keys($user); // Kalitlarni olishPHP 7.4+ va PHP 8.0+ versiyalarida qisqa arrow funksiyalar va tip ko'rsatmalaridan foydalanish mumkin.
<?php
// Qisqa arrow function
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(fn($n) => $n * $n, $numbers);
// Aniq tiplarga ega funksiya
function calculateTotal(float $price, int $quantity): float {
return $price * $quantity;
}Zamonaviy PHP to'liq Ob'ektga Yo'naltirilgan Dasturlashni (OOP) qo'llab-quvvatlaydi.
<?php
interface NotificationInterface {
public function send(string $message): bool;
}
abstract class BaseUser {
public function __construct(protected string $email) {}
}
class User extends BaseUser implements NotificationInterface {
public function send(string $message): bool {
// Xabar yuborish mantiqi
return true;
}
}<?php
class Product {
public function __construct(
public int $id,
public string $name,
public float $price = 0.0
) {}
}
// Named arguments yordamida obyekt yaratish
$item = new Product(id: 101, name: "Laptop", price: 1200.50);<?php
$statusCode = 200;
$message = match ($statusCode) {
200, 201 => 'Muvaffaqiyatli bajarildi',
400 => 'Bad Request',
404 => 'Topilmadi',
500 => 'Server xatoligi',
default => 'Noma'lum status',
};<?php
try {
$file = fopen("non_existent_file.txt", "r");
if (!$file) {
throw new Exception("Fayl topilmadi!");
}
} catch (Exception $e) {
echo "Xatolik yuz berdi: " . $e->getMessage();
} finally {
echo "\nJarayon yakunlandi.";
}PHP yordamida tashqi API larga JSON so'rov yuborish:
<?php
$url = "https://api.example.com/data";
$payload = json_encode(["key" => "value"]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Accept: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);