Saturday 23 April 2016

printf() in PHP

Thil ziahchhuah (Text Output) nan echo() function leh print() function hi PHP-ah chuan hman tam ber a ni thin a. Mithiam zawk te chuan print aiin echo hi a performance tha zawkin an tarlang a. Tun tumah hian kan hman tam lutuk loh thilziah chhuah(text output)-na hmanraw changkang deuh zawk printf chungchang inthlahrung takin in sawi lang ve leh dawn teh ang.
printf () hi print format tihna a ni a, kan output tur format-na a ni. Entirna nen lo tarlang ila a chiang leh zual ngei ang:
E.g. 
printf("There are %d items in your basket", 3);

Example-a kan hmuh ang hian printf chuan argument pahnih a nei a. %d hi specifier an ti a, format-na hmanrua ber a ni a, Kan argument pahnihna `3` hi kan format tur string a ni. Argument hi Specifier kan neih ang zat zat kan nei thei bawk a ni. Specifier hrang hrang te chu:
  • % : % character
  • b : Binary integer-a dahna
  • c : ASCII character-a dahna
  • d : Signed decimal integer-a dahna
  • e : scientific notation-a dahna
  • f : floating-point-a dahna
  • o : octal number-a dahna
  • s : string-a dahna
  • u : unsigned decimal-a dahna
  • x : lowercase hexadecimal-a dahna
  • X : uppercase hexadecimal-a dahna
Argument leh specifier hi a inzat chhung chuan specifier hi duh zat zat neih theih a ni a, mahse % symbol hi a hmaah dah zel tur a ni. Entirnan:
E.g.
printf("My name is %s. I'm %d years old, which is %X in hexadecimal",'Sir Sama', 33, 33);
//Output: "My name is Sir Sama. I'm 33 years old, which is 21 in hexadecimal".
Warning: Argument pakhat lek pawh i hmaih chuan parse error a awm nghal ang a. ') was unexpected encountered' tih ang vel error a lo awm ang.
Hmanna dang:
1.
printf("<span style='color:#%X%X%X'>Hello</span>", 65, 127, 245);

Output: <span style='color:#417FF5'>Hello</span>

Hrilhfiahna: hetah hian color red=65, green=127, blue=245 chu hexadecimala convert buai ngai lovin printf hmangin awlsam zawkin kan convert thei a ni.
2.
printf("The result is: $%.2f", 123.42 / 12);

Output
: The result is $10.29
Hrilhfiahna
: Pawisa (currency) hi 2 digit precision nei vek tura ngaih a ni a, mahse calculation a tam chuan digit 3/4 te a ni chho mai thin a, chumi pumpelh nan chuan %.2f hmangin 2 digit chauh hmang turin kan siam thei a ni. Hei hi number_format() hmang pawhin tihtheih a ni tho bawk a ni.
3.
printf("The result is $%15f\n", 123.42 / 12);

output
: The result is $ 10.285000
Hrilhfiahna: %15f\n hian $ leh kan number inkarah 15 space a dah a ni.
Real World Example:
//id: integer, max width 10
//code: string max width 2
$records =$mysqli->query("SELECT id, code from country");

$divider=printf("+%-10s+%-13s+",'-','-');

$lines[]=$divider;

$lines[]=printf("|%10s|%13s|",'id','countryCode');
//header
$lines[]=$divider;

while($line=$records->fetch_assoc()) {
//store the formatted output
   
$lines[]=printf("| %10u | %2.2s |", $line['id'],$line['code']);

}
$table=implode("\n",$lines);
echo
$table;
Output:
+----+-------------+
| id | countryCode |
+----+-------------+
| 1 | ES |
| 2 | AN |
| 3 | AF |
| 4 | AX |
| 5 | AL |
| 6 | DZ |
| 7 | AS |
| 8 | AD |
| 9 | AO |
| 10 | AI |
+----+-------------+

No comments :

Post a Comment