Home > Tutorials > PHP Tutorials > Getting Things Started With An Echo();

Getting Things Started With An Echo();

Chances are you’re familiar with the Hello World exercise by now. It’s the starting point for learning almost any programming language. PHP is no different. So let’s dive right into it. How do we display things using PHP?

<?php echo "Hello World!"; ?>

Which would display “Hello World!” on our users screen.

Now there are a few things to take note of. First off see how that echo statement ends with a semicolon. Every command in PHP must end with a semicolon. If you forget to use semicolon or use colon instead after a command you will get an error message like this

Parse error: parse error, unexpected ':', expecting ',' or ';' in echo.php on line 7

However in the above example above omitting the semicolon won’t cause an error because the echo statement was immediately followed by a closing PHP tag. This is generally the only situation when you will avoid the above mentioned Parse error – however as a general rule you shouldn’t rely on that and should simply put the closing semicolon.

Next you need to make sure you escape any quotations and apostrophe’s in you’re echo’d statement. For example if you wish to display to the user: Hello “Bob” it’s Monday – we would do the following:

<?php echo "Hello \"Bob\" it\\'s Monday"; ?>

We will take a closer look at escaping characters and variable expansion next.

Subsequently you can also echo out the values of a variable. Let’s say we have a variable – $myname – and we wish to display its value. We have two options for this:

<?php echo $myname; ?>

The latter method is a short syntax which allows you to quickly echo out stored variables. This will only work if your server is configured with short_open_tag – most modern day hosting companies are though. If you are unfamiliar with variables we take a closer look at them in our PHP Variables tutorial.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

Installing Gnome Software Center on a Chromebook

Linux is pretty much all the rage these days when it comes to Chromebooks – ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.