Echo and print

In PHP, there are two main ways to show something on the screen: echo and print. They behave almost the same way — both output data — but there are a few subtle differences. echo doesn’t return a value, while print does (it always returns 1, so it can be used inside expressions if you really need that). echo can also take multiple arguments and is slightly faster, though you’ll almost never notice that difference.

You can use echo with or without parentheses:

echo "Hello, world!";
echo("Hello, world!");

It’s common to display HTML markup or simple text with it:

echo "<h2>MemberPress is Powerful!</h2>";
echo "Learning PHP is fun.<br>";
echo "Let's connect science and code!<br>";
echo "This ", "string ", "uses ", "multiple parameters.";

You can easily mix in variables too:

$plugin = "Pretty Links";
$company = "CaseProof";

echo "<h2>$plugin by $company</h2>";
echo "<p>Helping creators manage links efficiently.</p>";

When you use double quotes, PHP automatically replaces variable names with their values. If you use single quotes, though, PHP will treat everything as plain text, so you’ll need to use concatenation with a dot (.):

$plugin = "ThirstyAffiliates";
$topic = "philosophy of simplicity";

echo '<h2>' . $plugin . '</h2>';
echo '<p>This plugin follows the ' . $topic . ' in UX design.</p>';

Now, print works almost the same way. You can use it with or without parentheses:

print "Hello from PHP!";
print("Hello from PHP!");

It’s great for showing text or HTML too:

print "<h2>Exploring Pretty Links</h2>";
print "Affiliate management simplified.<br>";
print "Let’s automate, not complicate.";

And just like echo, it handles variables easily:

$tool = "MemberPress";
$concept = "user autonomy";

print "<h2>$tool</h2>";
print "<p>This tool embodies the idea of $concept in membership design.</p>";

If you go with single quotes, you’ll again need concatenation:

print '<h2>' . $tool . '</h2>';
print '<p>This tool embodies the idea of ' . $concept . ' in membership design.</p>';

Most developers stick with echo since it’s a touch faster and a bit more flexible, but you can use either — they’ll both get the job done.