JD Lien
WebArtisan.info

WebArtisan.info

Output From PHP

Photo by KOBU Agency on Unsplash

Output From PHP

Everything You Ever Wanted to Know about Generating Output from PHP

JD Lien's photo
JD Lien
·Jun 19, 2022·

3 min read

Table of contents

  • 1. The Basics
  • 2. Formatted Strings
  • 3. Echo Shortcut Syntax
  • 4. Non HTML Output
  • 5. Bonus: Using PHP Open/Close Tags

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.

1. The Basics

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 1
  • echo can take multiple arguments, eg (echo $one, ' ', $two;)

2. Formatted Strings

PHP also supports a printf function that supports formatting. The most commonly used formatting values are:

  • %d - signed decimal numbers
  • %u - unsigned decimal numbers (only greater than zero)
  • %f - floating point numbers (that use localization to show the decimal separator, like a comma or period).
  • %s - string
<?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.

3. Echo Shortcut Syntax

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!

4. Non HTML Output

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);

5. Bonus: Using PHP Open/Close Tags

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!

 
Share this