PHP statements tell PHP to do something. Most new programmers write their first PHP statement as echoing “Hello world!” The echo statement is for outputting data to the web browser. For our first statement, as people of above average intelligence and beauty, we will say hello not only to the world but to the whole universe.
1. Make sure your local WampServer is running.
2. Open the code editor (Notepad++), create a new file and type in:
<?php echo "Hello universe!"; ?>
Notice all the empty space in the code? That’s okay. We don’t need to write everything on one line. PHP doesn’t care about white space inbetween the code. This allows us to format the code for better readability.
The semicolon at the end of the line #2 lets PHP know that this is the end of the statement. You need to ALWAYS end a statement with a semicolon, or PHP will not know where one statement ends and one begins.
3. Save this file to the “www” directory and name it: hellouniverse.php
4. Open your web browser and go to: http://localhost/introduction/hellouniverse.php
You should see “Hello universe!” displayed in your browser.
In computer programming there are usually many different ways to do the same thing. Let’s explore that now.
5. Open hellouniverse.php and change the code to:
<?php echo "Hello" . " universe!"; ?>
Reload this page in your web browser and it should display the same thing. By putting a period inbetween the quotations as above, we are able to combine or “catenate” them together.
Try echoing some of your own phrases. See how you do. If your browser displays an error, double-check your code for quotations and semicolons.
6. Another way to do this is to include the PHP code mixed in with HTML. For example:
<?php echo "<p>Hello universe!</p>"; ?>
The output will be the same as above, but now we are using HTML and PHP together which is usually the case when using PHP for web sites.
Go back to PHP Programming: The Basics.
Tags: beginner, echo, php, programming, statements