Every PHP script executes on a server. When the server process the script, it sent back the output in the form of plain HTML to the browser
<?php //Your PHP code ?>
Note: You can write your code in a text editor. There are various text editors for PHP like sublime text, notepad++, brackets, bluefish. I prefer notepad++, you can choose any editor you like. You can also use notepad.
Below is an example that would print the text “Hello World”
<?php echo "Hello World"; ?>
<?php and ?> used in this program are the PHP delimiters. The delimiters in a php script can be written in two ways –
1. Canonical tag form – The first is what we have used is called the canonical tag form. In this approach, the PHP script starts with a <?php and ends with ?>.
2 – Short open tag or SGML – There is another form of these delimiters which is called short open tag or SGML and is often used by php developers. In this approach the php script starts with <? and ends with ?>.
Note: To work with the short open tag approach, you have to set the short_open_tag setting in the php.ini file to ‘On’.
As we are in a learning stage, we will be using the canonical tag form in the entire tutorial series.
Below is an example containing html and PHP together. If you want to name this file as ‘test’ then you have to save the file as test.php.
<!doctype html> <html> <body> <h1><?php echo "This is a test page";?></h1> <p> <?php echo "This is a test paragraph";?> </p> </body> </html>
When this page is opened in a browser, the heading would appear ‘This is a test page’ and a paragraph which would contain the text “This is a test paragraph”.
Note : In PHP, statements ends with a semicolon;
In PHP, keywords (such as echo, while), functions are not case sensitive. That means writing echo and Echo both are legal and would work the same. For example-
<?php echo "Hello World"; EcHo "Hello World"; ECHO "Hello World"; ?>
Note: Variable names in PHP are case sensitive!
For example, $a and $A are not the same but two different variables, we will learn about variables in depth in other chapter.
Predefined keywords in PHP
In a programming language, Keywords are a set of predefined words that are used for specific purposes. These words cannot be used to declare variable names and functions. Here is a table containing predefined keywords in PHP.