CodeMIRAI
0 XP

Syntax and echo

2 min read 3 runnable examples Quiz included

echo and print

Both output text. echo is the one you'll see everywhere; it can take several values separated by commas:

PHP
<?php
echo "Hello", " ", "world", "\n";
print "print works too\n";
echo 42, "\n";
echo 7 * 6, "\n";

Single and double quotes

This is PHP's most important string rule:

  • Double quotes process variables and escape sequences like \n.
  • Single quotes take the text literally.
PHP
<?php
$name = "Asha";
echo "Hello, $name!\n";       // the variable is replaced
echo 'Hello, $name!\n';       // printed exactly as written
echo "\n";
echo "Total: {$name}'s cart\n"; // braces mark where a variable ends

Joining strings

PHP
<?php
$first = "Code";
$second = "Mirai";
echo $first . $second . "\n";
echo $first . " " . $second . "\n";
$message = "Hello";
$message .= ", world";       // append
echo $message . "\n";

Comments

PHP
<?php
// single-line comment
# also a single-line comment
/* multi-line
   comment */

Statements and case

  • Every statement ends with ;.
  • Variable names are case-sensitive ($name and $Name differ).
  • Keywords and function names are not (ECHO works, but nobody writes it).
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.