AnyCompiler

Strings

PHP ka concatenation operator single dot hai, . — + nahi, jo arithmetic ke liye reserved hai. $first . " " . $last teen tukdon ko ek string mein jod deta hai. Single-quoted strings ko taqreeban mukammal taur par literally liya jaata hai (sirf \\ aur \' hi un ke andar special hain); double-quoted strings ek important tareeke se mukhtalif hain: double-quoted string ke andar directly likha gaya koi bhi $variable apni value se khud ba khud replace ho jaata hai, bilkul koi concatenation operator nahi chahiye.

<?php
$first = "Ada";
$last = "Lovelace";
$full = $first . " " . $last;
echo $full, "\n";
$age = 36;
echo "Hello, $first! You are $age years old.\n";
echo strtoupper($full), "\n";
echo strlen($full), "\n";
echo str_replace("Lovelace", "Byron", $full), "\n";
Output
Ada Lovelace
Hello, Ada! You are 36 years old.
ADA LOVELACE
12
Ada Byron

"Hello, $first! You are $age years old.\n" ne $first aur $age dono ko un ki jagah par interpolate kar diya — ain yahi text single quotes mein likho to PHP literal characters $first aur $age print karega, koi substitution nahi hoga. strtoupper(), strlen(), aur str_replace() un saikdon string functions mein se teen hain jo PHP ki standard library ship karti hai — PHP ki web-templating jarron ka seedha nateeja, jahan text generate aur reshape karna hi poora kaam hai. Next: numbers and operators — arithmetic, aur sab se notorious PHP gotcha: loose (==) vs strict (===) comparison, ek real, hairat angez diagnostic se liya gaya.

Practice exercise

$first = "Ada" aur $last = "Lovelace" banao, phir double-quote interpolation istemal kar ke echo "Hello, $first $last!" karo, us ke baad ek newline.