spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


Basics of PHP

Are we ready?

Assuming that your installation went smooth and you are now ready to begin, let's start with some basic understanding of PHP files.

You must understand and remember few things about creating PHP files. And these are:-

  1. Every PHP file will have an extension of .php or optionally .php3 You have an option to change these default file extensions using the web server's configuration file(s).
  2. A .php file should have all the PHP code inside <? (opening tag) and ?> (closing tag). The opening tag can be <?php as well. It will then be treated as php language code and parsed accordingly. This is useful when you want to mix PHP code with HTML.
  3. PHP can be embedded inside HTML, but in a slightly different fashion. In between the HTML tags where ever you want to make use of PHP code, you will have to use <? php code ?> and this will be parsed by PHP.
  4. All statements must end with a ;(semicolon). This is very important and this semicolon tells the PHP parser to treat this line as a single statement. Without this you will see error messages when parsing the code.

Your first PHP code

Why not display some output onto the screen? It is in fact much easier to accomplish with PHP. All you have to do is use PHP language construct called echo and we are done. Here's how..

Open a blank new file in your editor or notepad and write the below mentioned lines in the file. Then save the file with a .php extension. If you are using notepad as editor then be careful about the file extension. In Windows OS the file extensions are often hidden, so you must ensure that file saved has a .php extension. Ok, take a look at the code below..

<? echo "This is my first line!"?>
 Or, you can write it using other declaration tag as well, which is
<?php echo "This is my first line!"php?>

Now save the file and upload it to the correct location in the web server and call it. The url could be http://localhost/filename.php (just an example, replace 'localhost' and filename.php with actual.)

Great! What do you see? We expect to see an output which will look like..

This is my first line!

If that is the exact output on your browser, then it is a success!!! We are on a good start. If you did not see this output, then perhaps PHP was not installed/configured properly. Note that if the PHP file is prompted to be downloaded in your browser, then PHP is not installed/configured properly as the file with .php extension is not being parsed. Please refer to your installation procedure.

Case Sensitivity

All the keywords and built-in constructs are case in-sensitive. Which means, there is no difference between echo and ECHO

Handling Statements

All statements are separated using a semicolon. It's only a compound statement such as if loop or a conditional loop which does not need a semicolon before and after the closing bracket. For example:

<?php
if (a=b) {
echo 
"a and b are equal"  // Semicolon is not needed here
}                     // Semicolon is not needed here
?>

Commenting the code

Code require commenting for a many reasons. The commented code is not parsed by PHP and is a good way of adding comments about statements, code and functions. All lines starting with // will be treated as a comment by PHP. One can also using # to comment the code. Example:

<?php
// Function to echo string passed as an argument
function echo_string($a) {
echo 
$a   // The value of variable a will be printed on screen
}
?>

Now you know that the text in first line and third line after // are treated as comments.



Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: