PHP Syntax, Variables ও Data Types

লেসন ১০ / ৩৯

PHP Tag

সব PHP কোড <?php ... ?> এর ভিতরে থাকে।

<!DOCTYPE html>
<html>
<body>
    <h1>হ্যালো</h1>
    <?php
        echo "<p>PHP থেকে আসছে</p>";
    ?>
</body>
</html>
💡 শুধু PHP ফাইলে (no HTML) শেষে ?> না দেওয়াই ভালো — accidental whitespace-এর সমস্যা এড়ায়।

Case Sensitivity

<?php
// ✅ Variables case-sensitive
$name = "করিম";
echo $NAME;      // ❌ Undefined
echo $Name;      // ❌ Undefined

// ❌ Keywords ও Functions case-insensitive
echo "Hi";
ECHO "Hi";       // কাজ করবে
Echo "Hi";       // কাজ করবে

Comments

<?php
// এক লাইনের comment
# এটাও এক লাইনের (shell-style)

/*
   একাধিক লাইনের
   comment
*/

/**
 * DocBlock comment
 * ফাংশন/class documentation
 */

Echo ও Print

<?php
// echo — দ্রুত, একাধিক argument নেয়
echo "হ্যালো";
echo "নাম: ", "করিম", "!\n";   // multiple args

// print — এক argument, 1 return করে
print "Hi";
$x = print "Hi";   // $x = 1

// Short echo (শুধু HTML-এ)
<p>নাম: <?= $name ?></p>

Variables

<?php
$name = "করিম";          // String
$age = 25;                // Integer
$height = 5.8;            // Float
$isActive = true;         // Boolean
$colors = ["লাল", "নীল"];  // Array
$user = null;             // Null

// Rules
// ✅ $ দিয়ে শুরু
// ✅ অক্ষর/_ দিয়ে শুরু
// ❌ সংখ্যা দিয়ে শুরু নয়
// ✅ Case-sensitive: $name ≠ $Name

// Variable-এ variable
$var1 = "hello";
$var2 = "var1";
echo $$var2;  // "hello"

Data Types (৮টি)

<?php
// Scalar (৪টি)
$str = "text";         // String
$num = 42;             // Integer
$price = 19.99;        // Float/Double
$flag = true;          // Boolean

// Compound (৩টি)
$arr = [1, 2, 3];     // Array
$obj = new stdClass(); // Object
$callable = function() { ... }; // Callable

// Special (১টি)
$empty = null;         // Null

// Type দেখতে
var_dump($num);    // int(42)
gettype($str);      // "string"
is_int($num);       // true
is_string($str);    // true

String

<?php
// Single quote — literal
echo 'নাম: $name';   // "নাম: $name"

// Double quote — interpolation
echo "নাম: $name";    // "নাম: করিম"
echo "তথ্য: {$user['name']}";

// Heredoc (multi-line, variable parse হয়)
$text = <<<EOT
নাম: $name
বয়স: $age
EOT;

// Nowdoc (variable parse হয় না)
$text = <<<'EOT'
Raw text $name
EOT;

// Concatenation
$full = "হ্যালো, " . $name . "!";
$full .= " স্বাগতম";

String Functions

strlen("Hello")          // 5
strtoupper("hello")      // HELLO
strtolower("HELLO")      // hello
ucfirst("hello world")   // Hello world
ucwords("hello world")   // Hello World
trim("  hi  ")           // "hi"
str_replace("a", "b", "cat")  // "cbt"
substr("Hello", 0, 3)    // "Hel"
strpos("Hello", "l")     // 2
explode(",", "a,b,c")    // ["a","b","c"]
implode("-", ["a","b"])  // "a-b"
sprintf("%.2f", 3.14159) // "3.14"

Type Juggling ও Casting

$num = "42";            // string
$int = (int) $num;       // int
$int = intval($num);     // int
$float = (float) "3.14"; // 3.14
$str = (string) 42;      // "42"
$bool = (bool) 1;        // true

// Type juggling (automatic)
$x = "10" + 5;   // 15 (int)
$y = "10" . 5;   // "105" (string)