who.am.i's PHP Tutorial (001) - Hello World
Here†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¾¢‚¬Å¡‚¢s a simple Hello World style PHP example file.
Copy and paste the following code into a file called [font=Courier New:c7033afa0d]helloworld.php[/font:c7033afa0d] and open it in your browser:
CODE
1 <html>
2 <head>
3 <title>Hello World Example</title>
4 </head>
5 <body>
6 Hello World from HTML!<br>
7 <?php
8
9 // Print our line from PHP
10 echo †™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™¢¢¬‚¹¢‚¬¦¢¢¬…œHello World from PHP!†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¾¢‚¬Å¡‚¢;
11
12 ?>
13 </body>
14 </html>
OK, let†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¾¢‚¬Å¡‚¢s dissect our code. First, lines 1-6 are simply normal HTML tags and parameters, which define our header and body blocks, and spits out the words †™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™¢¢¬‚¦‚¢¢¢‚¬Å¡‚¬¦¢‚¬Å“Hello World from HTML!†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬? in our HTML page.
On line 7, we have our PHP code block start, which tells the PHP interpreter that we are ready to start parsing the code below, until we reach the ?> tag, where we†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¾¢‚¬Å¡‚¢ll swap back to HTML. PHP always ignores white-space between strings, so you can, in theory, put as many new lines, or tabs or spaces as you want. It is, however good practise, to indent and space you code out correctly.
Line 9 simply contains a PHP comment, identified by the two forward slashes. This type of comment can start anywhere on a line, and everything else until a new line character will be part of that comment. A multi-line comment will be covered in the following chapters. Line 10 contains an instruction to display the line between the quotes. Note that strings can be defined using either single quotes(†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™¢¢¬‚¹¢‚¬¦¢¢¬…œ) or double quotes (†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™¢¢¬‚¦‚¢¢¢‚¬Å¡‚¬¦¢‚¬Å“) †™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™‚¢‚¢¢¢¬…¡‚¬¢‚¬¦¢¢¬…œ character arrays are always defined using single quotes. The line ends with a semi-colon (/wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" />. Note that semi-colons are only required at the end of a line if there is at least 1 statement after it, then each statement requires a semi-colon. It is good practise to use semicolons wherever possible.
So, if we run our script in your web-browser by going to http://path/to/your/web/server/helloworld.php, you will receive the following output:
[font=Times New Roman:c7033afa0d]Hello from HTML!
Hello from PHP![/font:c7033afa0d]
Cool huh..? No, not really†™ ¢‚¬„¢¢‚¬Å¡‚¢†™‚¢‚¢¢¢‚¬Å¡‚¬¦‚¡¢‚¬Å¡‚¬†™¢¢¬…¡¢‚¬Å¡‚¦ /:D" style="vertical-align:middle" emoid=":D" border="0" alt=":D" />
Don't understand something? Please reply and ask..!
[Tutorial] who.am.i's PHP Tutorial (001) - Hello World
-
- Site Admin
- Posts: 2411
- Joined: Sat Nov 06, 2004 4:43 am
- Location: Sydney, Australia
- Contact: