Learning PHP – Lesson 1 – PHP print and echo

PHP print and echo
Views: 37
0 0
Read Time:3 Minute, 45 Second

In our previous article 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.

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.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Learning PHP Previous post Learning PHP – Introduction
Learn the PHP variables Next post Learning PHP – Lesson 2 – Learn the PHP variables
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x