PHP 101: Getting Started (Part 1)
Yesterday I introduced you to PHP and explained how to install it and run it on a page. Web designer, Warren, very rightly queried the differences between “print” and “echo”, and I should say, there is also a “return” command; I will explain briefly.
Print and Echo
Print and echo are essentially the same thing, they both output strings to the browser. In the end it comes down to personal preference on which one you use – I for one rarely use print, and usually opt for the echo command.
<?php echo 'Hello World'; ?> <?php print 'Hello World'; ?>
Both statements above would output the same string to the browser.
Return Command
The PHP return command will simply end a function and “return” an argument to the code that calls it. For example:
<?php function calc($a, $b) { $val = $a + $b; return $val; } $sum = calc(4, 5); echo $sum; // This will Echo 9 ?>
As you can see, by returning a value in a function, you can echo it later on in the code.
Back to the Start
So it seems I may have jumped ahead a bit here. Let’s start with the absolute basics. In order to write some PHP, you will need to enclose any PHP code within the correct tags. These look like this:
<?php /* Your Code Here */ ?>
You can also write that in shorthand, if your server has shorthand enabled:
<? /* Your Code Here */ ?>
The Semicolon in PHP
Another thing to take note of is the semicolon that is used at the end of most lines. This signifies the end of a PHP statement, and should always be used to end a line of PHP code. For example, this would not work:
<?php echo 'Hello World' echo 'Hello World' echo 'Hello World' echo 'Hello World' ?>
Some PHP editors will inform you of an error, which can be useful. The code should look like this:
<?php echo 'Hello World'; echo 'Hello World'; echo 'Hello World'; echo 'Hello World'; ?>
The above would output Hello World, four times.
Single or Double Quotes
When writing PHP statements you will notice that some use double quotes to declare a string, and some use single quotes. Both are fine to use and each have their benefits.
Single Quotes
When using single quotes, or apostrophes, you may find that you need to actually output an apostrophe in the string. Like so:
<?php echo 'Hello, how are you finding James' tutorials?'; ?>
When parsing this, PHP will see the first apostrophe opening the string, and will assume that the next one is closing the string. In this situation you will need to escape the apostrophe within the string, like so:
<?php echo 'Hello, how are you finding James\' tutorials?'; ?>
Using the backslash will tell PHP that it is not the end of the string, and should be returned as normal (the backslash will not be returned).
Double Quotes
Double quotes allow you more flexibility for outputting your strings. There are a few escape commands that allow for different things, these are:
<?php echo "\n"; // Declares a newline echo "\r"; // Declares a carriage return echo "\t"; // Declares a tab echo "\$"; // Outputs a dollar sign echo "\""; // Outputs a double quote ?>
These escaped characters are great for returning a formatted string to be read more clearly by human eyes. You can also echo variables from within double quotes and not single quotes, but we will come on to that another day!
Conclusion
So, today you have learned some more PHP to help benefit your web design projects. Tune in for the next tutorial, where I will be discussing variables and answering any questions.
Cheers for clearing that up James, as I usually see echo used more often than print I think I’ll go down the echo route, I think echo sounds cooler too!