• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / echo php – Output One or More Strings

echo php – Output One or More Strings

December 13, 2021 Leave a Comment

php echo is one of the most used constructs in php. The echo is not a function, but a language construct and allows us as php programmers to output one or more strings.

echo "Hello World!"; // Output: Hello World!

The php echo statement is one of the most used and most useful statements in the php programming language.

With echo, in php, you can pass one string or multiple strings separated by commas.

echo "Hello World!"; // Output: Hello World!
echo "Hello"," World!"; // Output: Hello World!

You can also pass other types of data to echo, and php will attempt to coerce these data types into a string. For integers, this works, but for arrays, a warning will be thrown.

echo 1; // Output: 1
echo [0,1]; // Output: Warning: Array to string conversion on line 2 
                       Array

If we want to output an array, we should use the json_encode function first, and then pass it to echo.

echo json_encode([0,1]); // [0,1]

Using echo to Output A String in php

Outputting a string using echo is very easy. All we need to do is pass a string to the php echo statement.

echo "This is a string we are outputting with echo!"; // Output: This is a string we are outputting with echo!

How to Use echo in php to Output Multiple Strings

Outputting multiple strings using echo is very easy. All we need to do is pass the strings to the php echo statement separated by commas.

echo "This is a string.", " This is a second string."; // Output: This is a string.  This is a second string.

One thing to note here is that the strings are all printed on the same line. To output multiple strings to multiple lines, you should use \r\n.

echo "This is a string.", "\r\nThis is a second string."; // Output: This is a string.
This is a second string.

Using echo to Output HTML in php

One of the most useful ways to use the php echo is when outputting HTML to a web page. Many times, as web developers, we want to design pages which are dynamic and with echo, we can accomplish this.

To output HTML using echo and php, we just need to build a string which contains our HTML.

Let’s say I want to use php to create a web page. In this web page, I want to echo the title and a summary paragraph to the browser.

We can do this easily with the following php code:

echo "<h2>Page Title</h2><p>Page Summary</p>";

This will output the following HTML:

Page Title

Page Summary

How to Output a Variable Using echo in php

We can also use echo to output php variables. To do this, we just need to pass the variable to echo:

$var1 = "A string to output with echo";
echo $var1; // Output: A string to output with echo

If we have multiple variables we want to output, we can concatenate them or pass them separated by commas.

$var1 = "String 1";
$var2 = "String 2";
echo $var1 . ' ' . $var2; // Output: String 1 String 2
echo $var1, ' ', $var2; // Output: String 1 String 2

Hopefully this article has been helpful for you to understand all the ways you can use echo in php to output strings.

Other Articles You'll Also Like:

  • 1.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 2.  php asinh – Find Hyperbolic Arcsine of Number Using php asinh() Function
  • 3.  Using strtolower() in php to Convert String to Lowercase
  • 4.  Remove Specific Character from String Variable in php
  • 5.  Check if Variable Exists in php with isset() Function
  • 6.  Check if String Ends with Given String in php with str_ends_with()
  • 7.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 8.  preg_replace php – Use Regex to Search and Replace
  • 9.  php array_shift() – Remove the First Element from Array
  • 10.  Reversing an Array in php with array_reverse() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy