LESSON 03 / 05

Strings & Output

Single vs double quotes, joining with the dot, heredoc for big blocks, and printf for perfectly formatted output.

๐ŸŽ“Beginnerlevel
โฑ๏ธ30 minto finish
โœ๏ธ2activities

๐Ÿ“‚ PHP Basics๐Ÿท๏ธ Strings

๐ŸŽฏ By the end of this lesson you canโ€ฆ

  • Choose single or double quotes
  • Join strings with . and .=
  • Format output with printf and number_format
๐Ÿ˜ PHP quotes.php ๐Ÿ‘† Tap a line to explain it
1<?php2$name = "Kabir";3echo "Hi $name\n";4echo 'Hi $name\n', "\n";5echo "Hi {$name}!\n";6$msg = "Good";7$msg .= " morning";8echo $msg . ", " . $name . "\n";
๐Ÿ˜ PHP formatting.php ๐Ÿ‘† Tap a line to explain it
1<?php2$name = "Kabir";3$price = 1234.5;4printf("Price: โ‚น%.2f\n", $price);5printf("%-8s|%5d\n", "Pens", 12);6echo number_format(1234567.891, 2), "\n";7echo <<<TEXT8Dear $name,9  Your order is ready.10TEXT;
๐Ÿ”ฎ What will this display?
What does this print?
1<?php2$x = 5;3echo 'x is $x';
๐Ÿ› ๏ธ Receipt line

Read an item and a price. Print <item>: โ‚น<price with 2 decimals>, e.g. Notebook: โ‚น45.00.

๐Ÿ“Œ Key takeaways

  • Double quotes replace variables ("Hi $name"); single quotes donโ€™t ('Hi $name').
  • . joins; .= appends.
  • printf("%.2f", $x) and number_format($x, 2) format numbers.

Finished reading & practising?

Saved in this browser. Create a free account to keep it forever.