PHP programming uses some syntax and notations that may seem unfamiliar to the people who haven’t programmed computers. So before you go through the tutorials of PHP programming, After Installing the XAMPP we just begin by writing the First Program in PHP to print a line.
My First Program in PHP
Program to display “Hello world”
When we write the First Program in PHP then in first create a file named called index.php and put it in your web server’s root directory (DOCUMENT_ROOT) that is C Drive > Xampp > htdocs > yourFolderName > index.php with the following content:
/* This is my First Program in PHP..*/ <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html>
Use your browser to access the file with your web server’s URL, ending with the /hello.php file reference. When developing locally this URL will be something like
but this depends on the web server’s configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:
<html> <head> <title>PHP Test</title> </head> <body> <p>Hello World</p> </body> </html>
This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo statement.
Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the “
.php ” extension, which the server is configured to pass on to PHP.
Explanation of the Program
The first line of the program begins with
/* and ends with
*/ which represents that any text written between these two symbols is a comment. Comments do not affect program while running because PHP compiler ignores the comments and programmers use these comments for the documentation of the program.
Opening and Closing Syntex of PHP
When PHP parses a file, it looks for opening and closing tags, which are
?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script. Like this.
<?php echo "Hello world"; // ... more code echo "Last Line"; // the script ends here with no PHP closing tag
echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to
echo() , using parentheses will generate a parse error.
echo() function also has a shortcut syntax. Prior to PHP 5.4.0, this syntax only works with the short_open_tag configuration setting enabled.
Escape sequences are the characters which are not printed. The
\ backslash is called escape character.
|\n||Newline. This will place a cursor on the beginning of the second line.|
|\t||Tab. This will insert horizontal tab.|
|\\||Backslash. Insert backslash in a string.|
|\a||Alert. This will signify alert sign or sound without changing the position of cursor|