Learning PHP – Lesson 1 – print and echo

In our previous article (https://keepforyourself.com/coding/learning-php-introduction/) we introduced PHP and help to setup a small environment for the lessons that will came. In this lesson we are going to talk about on how we will be able to output on screen what we want by using the print and the echo constructs in PHP.

Since nowadays PHP looks like some sort of obscure technology, if you are looking for a structured approach we strongly recommend you to have a look at this book

In the previous lesson we output the “Hello World!” string, in this lesson I want to go a little bit deeper:

PHP – echo

From the immense php documentation (https://www.php.net/manual/en/function.echo.php)

(PHP 4, PHP 5, PHP 7, PHP 8)

echo — Output one or more strings

Description

echo(string ...$expressions): void

Outputs one or more expressions, with no additional newlines or spaces.

echo is not a function but a language construct. Its arguments are a list of expressions following the echo keyword, separated by commas, and not delimited by parentheses. Unlike some other language constructs, echo does not have any return value, so it cannot be used in the context of an expression.

In other words, the below will output something. If you want to try the below example, just copy and paste the code below into lesson-01.php and execute by typing php lesson-01.php

  <?php 
     # PHP Lesson-02 - echo
     echo "First\n";
     echo ("Second\n");
     echo "Third\n", "Forth\n";
     echo ("Fifth\n") , ("Sixth\n");
  ?>

The output should not surprise you.

PHP – print

From the immense php documentation (https://www.php.net/manual/en/function.print.php)

(PHP 4, PHP 5, PHP 7, PHP 8)

print — Output a string

Description

print(string $expression): int

Outputs expression.

print is not a function but a language construct. Its argument is the expression following the print keyword, and is not delimited by parentheses.

The major differences to echo are that print only accepts a single argument and always returns 1.

In other words, the below will output something as the previous example, the only difference is that you can’t pass more than one argument and the construct is always returning 1. The example below shows how to use the print construct

<?php
    print("first sentence\n");
    print "second sentence\n";
?>

Output formatting

Now that we have seen how to output something here is a list of formatters that must be used with the double quotes “”

SequenceMeaning
\nlinefeed
\rcarriage return
\thorizontal tab
\vvertical tab
\eescape
\fform feed 
\\backslash
\$dollar sign
\”double-quote
\[0-7]{1,3}the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. “\400” === “\000”)
\x[0-9A-Fa-f]{1,2}the sequence of characters matching the regular expression is a character in hexadecimal notation
\u{[0-9A-Fa-f]+}the sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the string as that codepoint’s UTF-8 representation

As example see the output of the code below

<?php
    echo "a\nb\nc\n";
    echo "=========================================\r";
    echo "\twent back on the same line ";
    echo "\n\n\n\v1\t2\t3\t\v4";
?>

Output concatenation

Strings in PHP can be concatenated by using the dot “.” operator

<?php
    echo "This" . " " . "is" . " " . "concatenated\n";
?>

Difference between ‘ and “

In PHP there is an important difference between single quote ‘ and double quotes “

Within the single quotes the variables are not expanded (we will see this in the details in the next few lessons) while within the double quotes the variables are expanded and also you have the ability to use the escape sequences we have seen previously for formatting the output. As example for now look at the code below

<?php
    echo "start ======\n";
    echo '.......continue.......\n';
    echo "\n.....end\n";
?>

As you can see from the output the middle line is printed as it is. We will see more about this difference when looking at the variables in the next few lessons.

Learning PHP – Introduction

Learning PHP – print and echo

Learning PHP – The variables

Learning PHP – if else

Learning PHP – Arrays

Learning PHP – The loops

Learning PHP – The Functions

If you really like this series of articles, please share and help us to grow.

d3

d3 is an experienced Software Engineer/Developer/Architect/Thinker with a demonstrated history of working in the information technology and services industry. Really passionate about technology, programming languages and problem solving. He doesn't like too much the self celebration and prefers to use that time doing something useful ...i.e. coding

You may also like...