Output From PHP
Everything You Ever Wanted to Know about Generating Output from PHP

Search for a command to run...
Everything You Ever Wanted to Know about Generating Output from PHP

No comments yet. Be the first to comment.
After a few days of using the 512GB MacBook Neo with TouchID, here are my impressions after some heavy, real-world use. I'll disclaim this review by saying that I consider myself a power user; on my m

2024 was a strange year for me that was a mix of trying new things, accomplishing the best work of my career, and stark transitions into unexplored territory. I think it's important to take a moment every once in a while to step back and look at one'...

What I've Been Up To Four Months Into Freelancing

Leaving My Job with No Plan — My New Career as a Freelancer

With a few glasses of wine in me and some nostalgia-fueled synthwave playing, I found myself reflecting on my past, and my history with computers. I got to thinking of my childhood, and more specifically, my personal history with computers. I love te...

The first thing you want to know in any language is "How do I print stuff?". Here I'll show you how to write output from PHP.
To generate output, PHP has a language construct called echo (and a similar one called print. Note that parentheses are optional.
<?php
echo "Hello world!\n";
print "Hello world!\n";
These work very similarly, with a few small differences:
echo returns void, print returns an int of 1echo $one, ' ', $two;)PHP also supports a printf function that supports formatting. The most commonly used formatting values are:
<?php
$name = 'JD Lien';
$country = 'Canada';
$age = 40;
printf(
"My name is %s from %s and I am %d years old.",
$name,
$country,
$age
);
My name is JD Lien from Canada and I am 40 years old.
When you need to output something in a block of plain HTML, PHP has a handy echo shortcut that greatly simplifies the output.
<?php
$data = 'some data';
?>
<p>You can write <?=$data?> right in your HTML like this.</p>
Note: In the real world, any data output should be filtered or escaped to prevent cross-site scripting (XSS) attacks!
Of course not all output from a webserver will be webpages. Sometimes you need a plaintext file, like CSV or something for data interchange, or JSON data for an API.
When doing this, you must ensure you set the correct header on the output. This is done simply by using the header() function like so:
<?php
header('Content-Type: application/json');
$data = ['message' => 'Here is a message!'];
echo json_encode($data);
Of course you can set any header values that are required in this way, calling the function multiple times to set more header values. By default it will replace any previously defined headers, unless a second 'false' argument is passed.
header('Content-Type: text/plain');
// The Content-Type will be replaced here
header('Content-Type: application/json');
// Here, both header values will be present
header('WWW-Authenticate: Negotiate');
header('WWW-Authenticate: NTLM', false);
PHP was originally used as a templating language, so you must start any PHP code with <?php. Old versions of PHP also allowed you to leave out the "php" part, eg <? ?>, but this is no longer supported.
It is recommended in the PSR-2 Style Guide to never end a document with the closing ?> tag, so that you don't end up with extra whitespace at the end of a document that could cause issues. So don't add the closing tag unless you have to add something that is not PHP code at the very end of a document.
Thanks for reading! If you enjoy learning about PHP, please follow me on Twitter so you don't miss any of my other web development content!