<?php // Single quotes — literal (no variable interpolation) $name = 'Ko Min'; echo 'Hello $name'; // "Hello $name" (literal $) // Double quotes — variable interpolation echo "Hello $name"; // "Hello Ko Min" echo "Hello {$name}!"; // "Hello Ko Min!" // Concatenation with . $full = "Hello, " . $name . "!"; // Concatenation assignment .= $text = "Hello"; $text .= ", World"; // "Hello, World" // Heredoc — multiline string $html = <<<HTML <h1>Hello $name</h1> <p>Welcome!</p> HTML; ?>
| Function | ဘာလုပ်တယ် | Example → Result |
|---|---|---|
strlen($s) | Length | strlen("Hello") → 5 |
strtolower($s) | Lowercase | "HELLO" → "hello" |
strtoupper($s) | Uppercase | "hello" → "HELLO" |
ucfirst($s) | First char uppercase | "hello" → "Hello" |
ucwords($s) | Each word capitalize | "ko min" → "Ko Min" |
trim($s) | Remove whitespace | " hello " → "hello" |
ltrim($s) | Left trim | " hi" → "hi" |
rtrim($s) | Right trim | "hi " → "hi" |
str_replace($f,$r,$s) | Find & replace | "Hello World" → "Hello PHP" |
str_contains($s,$f) | Contains? (PHP 8) | str_contains("hello","ell") → true |
str_starts_with($s,$f) | Starts with? (PHP 8) | str_starts_with("hello","he") → true |
str_ends_with($s,$f) | Ends with? (PHP 8) | str_ends_with("hello","lo") → true |
strpos($s,$f) | Find position | strpos("hello","ll") → 2 |
substr($s,start,len) | Extract portion | substr("hello",1,3) → "ell" |
str_repeat($s,$n) | Repeat string | str_repeat("ab",3) → "ababab" |
str_pad($s,len,$pad) | Pad string | str_pad("5",3,"0",STR_PAD_LEFT) → "005" |
wordwrap($s,width) | Wrap long text | word wrap at 80 chars |
nl2br($s) | Newline → <br> | "\n" → "<br>" |
htmlspecialchars($s) | Escape HTML (XSS safe) | "<script>" → "<script>" |
strip_tags($s) | Remove HTML tags | "<p>Hi</p>" → "Hi" |
number_format($n,2) | Format number | 1234.5 → "1,234.50" |
sprintf($fmt,...) | Format string | sprintf("%.2f",3.1) → "3.10" |
<?php // explode — string ကို array ဖြစ်အောင် $csv = "apple,banana,mango,orange"; $fruits = explode(",", $csv); // ["apple", "banana", "mango", "orange"] echo $fruits[0]; // "apple" echo count($fruits); // 4 // Limit $parts = explode(",", $csv, 2); // ["apple", "banana,mango,orange"] // implode — array ကို string ဖြစ်အောင် $names = ["Ko Min", "Ma Aye", "Ko Kyaw"]; echo implode(", ", $names); // "Ko Min, Ma Aye, Ko Kyaw" echo implode(" | ", $names); // "Ko Min | Ma Aye | Ko Kyaw" // join() is alias of implode() echo join("-", ["a", "b", "c"]); // "a-b-c" ?>
<?php // ⚠️ User input ကို display မလုပ်ခင် မဖြစ်မနေ escape လုပ်!! $userInput = $_POST['name'] ?? ''; // ❌ Dangerous! XSS attack ဖြစ်နိုင် echo $userInput; // ✅ Safe! HTML characters escape ဖြစ်တယ် echo htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8'); // Example: // Input: <script>alert('hack')</script> // Output: <script>alert('hack')</script> // Browser shows text, not executes script! // Helper function pattern function e($str) { return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); } echo e($userInput); // shorter ?>
<?php // sprintf — format and return $price = 9.5; echo sprintf("Price: $%.2f", $price); // "Price: $9.50" echo sprintf("%05d", 42); // "00042" echo sprintf("%-10s|%10s", "Left", "Right"); // "Left | Right" // number_format echo number_format(1234567.89, 2, ".", ","); // "1,234,567.89" // Common format specifiers // %s → string // %d → integer // %f → float (%.2f = 2 decimal places) // %05d → zero-padded 5 digit int // %b → binary // %x → hexadecimal ?>
← PHP 05 | Next: PHP Lesson 07 → Forms →