PHP Tutorial – 02 – Using PHP
To start writing PHP create a plain text file with a .php file extension and open it in the editor of your choice – for example Notepad, Dreamweaver, Netbeans, or PHPEclipse. This file can include any HTML, as well as PHP scripting code. Begin by first entering the following standard HTML elements into the document.
<html> <head><title>Learning PHP</title></head> <body></body> </html>
Embedding PHP
PHP code can be embedded anywhere in the document in one of four different ways. First, there is the standard notation.
<?php ... ?>
The second notation is a short version of the first where the “php” part is left out. Although this notation is shorter the longer one is preferable if the PHP code needs to be portable. This is because support for the short delimiter can be disabled in the PHP configuration.
<? ... ?>
A third alternative is to embed the PHP code within a HTML script element with the language attribute set to "php".
<script language="php">...</script>
One last style you may encounter is that the script is embedded between ASP tags. This style is disabled by default, but can be turned on from the PHP configuration file.
<% ... %>
Outputting text
Printing text in PHP is done by typing either echo or print followed by the output, within the body element of the document. Each statement (except the last one in a script block) must end with a semicolon in order to separate it from other statements.
<?php echo "Hello World"; print "Hello World"; ?>
If the short PHP delimiter is enabled, which is the default, output can also be printed using the "<?=" open delimiter. This syntax is identical to "<? echo".
<?= "Hello World" ?>
Installing a web server
To view PHP code in a browser the code first has to be parsed on a web server with PHP installed. If you're using Windows, you can download and install a distribution of the popular Apache web server called Indigoampp, which comes pre-installed with PHP, Perl, and MySQL.
After installing and rebooting, point your browser to "localhost" to make sure that the web server is online. The web server should now show the file index.html, which by default is located under "C:\indigoampp\apache-(version)\htdocs". Htdocs is the folder that the web server (Apache) looks for files to serve on your domain.
For more advanced instructions that cover other operating systems refer to the PHP installation manual.
Hello world
Continuing from before, the simple "Hello World" PHP web document should look like this:
<html> <head><title>PHP Tutorial</title></head> <body> <?php echo "Hello World"; ?> </body> </html>
To view this PHP file parsed into HTML move it to the web server's htdocs folder and point your browser to its path, for example "localhost/HelloWorld.php". When you request the PHP web page the script is executed (interpreted) on the server and sent to the browser as only HTML. If you try to view the source code from the browser you cannot see the server-side script that generated the text, only the output as HTML.
Comments and whitespace
As in HTML, whitespace characters are generally ignored in PHP, so you can format the script anyway you want. For writing comments, PHP use the two standard C++ notations for single-line (//) and multi-line (/* */) comments. The Perl comment notation (#) may also be used to make a single-line comment.
<?php // single-line comment # single-line comment /* multi-line comment */ ?>
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

