PHP echo and print


August 23, 2021, Learn eTutorial
1362

In this PHP tutorial, you will learn all about printing functions in PHP. We will discuss in detail what is an echo statement and print statement in PHP and their differences. In PHP, there are two basically two ways to display the output. The echo statement is most widely used to display output.

What is an echo statement in PHP?

The echo statement in PHP works like a language construct that never operates as a function, therefore there is no importance for parentheses. The end of the statement is indicated by the semi-colon (;). The echo statement can display as many strings as required. We can display variables, numbers, strings, values, and results of expressions by using the echo statement.

How to display the output using an echo statement?

  • Displaying single string using echo

    To display a single string, we may simply type echo preceded by the string we would like to display within single quotes (‘’) or double quotes (“”). If we want to display the value of the variable, we should use the double quotes.

    Example

    
    echo “Hi! Welcome to learnetutorials.com”;
    echo ‘We are learning about the PHP language’;
    
    

    Output:

    
    Hi! Welcome to learnetutorials.com
    We are learning about the PHP language
    
  • Displaying multiple arguments using echo

    To display the multiple arguments is similar to the single argument in here we use the comma ( , ) to separate the arguments.

    Example

    
    echo “Hi”, ”All”, ”Welcome”, ”To learnetutorials.com”;
    
    

    Output:

    
    Hi All Welcome To learnetutorials.com
    
  • Displaying variables using echo

    To display the variables either we can pass the variable directly or we can use the double quotes (“”). We use the Concatenation (.) operator to concatenate the arguments.

    Example

    
    $age = 18;
    echo “Age =”. $age;
    echo “Age = $age”;
    
    

    Output:

    
    Age = 18
    Age = 18
    
PHP - Echo

What is a print statement in PHP?

The print statement in PHP is mostly the same as the echo statement. Print statement also works as a language construct but compared to the echo statement it is more like a function, and there is no importance for parentheses like the echo statement. The end of the statement is indicated by the semi-colon (;). Unlike echo statements, the print statement always returns a value of 1 and the print statement can’t display multiple arguments. We can display variables, numbers, strings, values, and results of expressions by using the echo statement

How to display the output using a print statement?

  • Displaying single string using print

    To display a single string, it is similar to the echo statement; we may simply type print preceded by the string we would like to display within single quotes (‘’) or double quotes (“”). If we want to display the value of the variable, we should use the double quotes.

    Example

    
    print “Hi! Welcome to learnetutorials.com”;
    print ‘We are learning about the PHP language’;
    
    

    Output:

    
    Hi! Welcome to learnetutorials.com
    We are learning about the PHP language
    
  • Displaying variables using print

    To display the variables it is similar to the echo statement either we can pass the variable directly or we can use the double quotes (“”). We use the Concatenation (.) operator to concatenate the arguments.

    Example

    
    $age = 18;
    echo “Age =”. $age;
    echo “Age = $age”;
    
    

    Output:

    
    Age = 18
    Age = 18
    
PHP - Print

What are the differences between echo statements and print statements?

echo statement print statement

echo can pass multiple arguments by using the comma (,)

the print statement doesn’t accept multiple arguments

echo statement doesn’t return any value

the print statement always returns a value of 1

comparatively echo is faster than print

comparatively print is slower than echo

echo behaves more like a language construct no a function

print is also a language construct but it behaves more like a function compared to echo