| What is PHP?? |
| PHP is a HTML-embedded, server-side scripting language that has gained popularity in part due to it's easy-to-learn syntax that borrows from many common languages such as Java, C, and Perl. PHP is also extremely popular due to the ease with which it can be integrated with MySQL to create dynamic, database-driven websites. |
| How do I get started with PHP?? |
|
Getting started with PHP is as simple as downloading the installer and choosing the type of web server
you are running. Once you have set this up, testing that PHP is working properly requires only a simple test file.
Try this code: <?     phpinfo(); ?> and put it in a file called test.php This file will display all the settings relating to your installation of PHP! The<? and ?> denote where the PHP script code begins and where it ends. |
| So what is the syntax like?? |
|
The syntax of PHP looks very similar to what you would see if you have programmed
in languages like C or Java, but there are many small differences present. Whereas Java and C are
closer to being "strongly-typed" languages because of the way they handle variables and object types, PHP is
a "weakly-typed" language. This feature gives the language flexibility, but as a programmer, it is also necessary to
pay more attention to variables and types because the compiler will NOT complain
about type errors! So the following code would be completely valid(although it could cause problems in
your scripts if a variable gets assigned incorrectly!):    $var = "Hello";    $var = 1;    echo $var; As you can see, the variable $var is first assigned to a string, then immediately changed to an Int! But what is that echo command?? Echo is the output/print command for PHP. A common use for this command is to output HTML code from within a script so that the webpage content can be chosen dynamically. In PHP, there are functions that are like "methods" in Java or C. One main difference again relates to variables, in that the return type of functions is not defined. So, a method could(technically) return different types when run multiple times!! Obviously this situation can create problems, but it also gives the programmer a great deal of flexibility. |
| So how does PHP interact with databases?? |
|
Database integration/interaction is one of the most attractive features of PHP because
it is built into the language very well and is extremely easy to implement. With MySQL, for example, a connection can be established with several short lines of code: <?     @ $db_connection = mysql_pconnect("servername", "username", "password");     $query = "SELECT something FROM table WHERE conditions";     $result = mysql_query($query); ?> And you have a connection to a MySQL database and a simple query structure! So as you can see from the code above, the first line initializes the connection to the database. This connection is often created as a persistent connection and the variable, in this case $db, is usually instantiated as a global variable so that it can be referenced from different scripts. If you have taken a database systems course, you probably already understand the benefits and drawbacks of using persistent connections, but if you haven't, you should look into these factors before choosing a connection type. So you might be asking yourself what the point is of having a variable associated with the database connection. The answer is that it allows the programmer to put code in the rest of the scripts that check if the database connection is still open before trying to access the database. So, if the connection is no longer valid, a new connection can be established. |
| So what advanced features does PHP have?? |
|
PHP has many advanced features that can be extremely useful when building any type of dynamic website.
One of the most useful, in my opinion, is the support for sessions and cookie handling. Cookies and sessions allow the
website to keep track of a particular user so that the site can be customized for that user each time he or she accesses
a page on the site. PHP makes sessions extremely easy with the session_start() command. This command
goes at the beginning of the script on each page. PHP will check to see if the session
is already registered, before trying to start a new session each time, but if you want to be able to customize the page
content based on the session status, you can use the session_is_registered() function that
returns a boolean(true or false) value reflecting the session status. So here's some sample code: <?    session_start();    $db = mysql_pconnect("localhost", "username", "password");    if(isset($uid) && isset($password)){      $query = "select * from user where email='$uid' and pwd='$password'";      $result = mysql_query($query);      $num = @mysql_num_rows($result);      if($num >0){         $valid_user = $uid;         session_register("valid_user");      }    } ?> So let's go through this code briefly to see exactly what it does. First it tries to start a new session. If a session is already started and the variables $uid and $password are already registered(meaning the user logged in previously), then the user should be in the user database. So we build a query that looks for a user in the database with a given username and password. If the user is found, noted by this code showing that there is an entry in the database with this information $num = @mysql_num_rows($result); if($num > 0){ then we can use that information to register the particular user's session with the session_register() command. |
| Some additional resources: |
|
    This Site offers a good Tutorial: Zend.com PHP Tutorial     The Official PHP Site:         
|
I hope that this brief overview of some of the features of PHP has been helpful. Good luck and enjoy using PHP!! |