Learning PHP…

August 23, 2008 at 11:06 am (Programming, Uncategorized)

PHP (recursive acronym for “PHP: Hypertext Preprocessor”) is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Example 1: Hello World (sample1.php)

echo "Hi, I'm a PHP script!";

PHP can be used on all major operating systems, including Linux, many Unix variants (including HP-UX, Solaris and OpenBSD), Microsoft Windows, Mac OS X, RISC OS, and probably others. PHP has also support for most of the web servers today. This includes Apache, Microsoft Internet Information Server, Personal Web Server, Netscape and iPlanet servers, Oreilly Website Pro server, Caudium, Xitami, OmniHTTPd, and many others. For the majority of the servers PHP has a module, for the others supporting the CGI standard, PHP can work as a CGI processor.v

Variables and Operators

$var = “Hello”; //string “hello”

$var = 3; // integer 3

$var2 = $var1; // Assigns a value of ‘PHP’ to $var2

echo $var1; // Outputs ‘PHP’


A string is series of characters. In PHP, a character is the same as a byte, that is, there are exactly 256 different characters possible.

echo “Hello World”;



An array is a special kind of variable that contains multiple values. The simplest way to create an array in PHP is to use the built-in array function:

$myarray = array(‘one’, 2, ‘3’);

$arrayvar = array(‘apple’, ‘banana’, ‘orange’);

echo $arrayvar[0]; // Outputs ‘apple’

echo $arrayvar[1]; // Outputs ‘banana’

echo $arrayvar[2]; // Outputs ‘orange’


The most basic, and most often-used, control structure is the if-else statement.

Here’s what it looks like:

1. If condition

Ex: if($username==”admin” and password==”admin”) { echo “Hello admin! Welcome!”; }

else { echo “Invalid username or password”; }

2. While condition


$count = 1;

while ($count <= 10) {

echo “$count “;



3. For Loop

Ex: for($i = 0; $i <10; $i++)

{ echo $i; }




<table width =”50″ border=”1“>


for($y=1; $y<=10; $y++)

{ echo “<tr><td><center><h1>$y </h1></center></td></tr>”; }






One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element in a form will automatically be available to your PHP scripts.


<form action=”welcome.php” method=”get”>

<label>First Name: <input type=”text” name=”firstname” />

</label><br />

<label>Last Name: <input type=”text” name=”lastname” />

</label><br />

<input type=”submit” value=”GO” />




$firstname = $_POST[‘firstname’];

$lastname = $_POST[‘lastname’];

echo “Welcome to my Website, $firstname $lastname!”;



Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site.

A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.



$_SESSION[‘username’] = $name;

To destroy a session you created:

Example: session_destroy();


The <input type="file"> form element lets a user upload the entire contents of a file to your server. When a form that includes a file element is submitted, the PHP interpreter provides access to the uploaded file through the $_FILES auto-global array.

$_FILES[‘upload-name’][‘name’]: The name of the file as uploaded from the client to the server.

$_FILES[‘upload-name’][‘type’]: The MIME type of the uploaded file. Whether this variable is assigned depends on the browser capabilities.

$_FILES[‘upload-name’][‘size’]: The byte size of the uploaded file.

$_FILES[‘upload-name’][‘tmp_name’]: Once uploaded, the file will be assigned a temporary name before it is moved to its final location.

$_FILES[‘upload-name’][‘error’]: An upload status code. Despite the name, this variable will be populated even in the case of success. There are five possible values:


1. $connect = mysql_connect(address, username, password); // address is the ip address or host name of the computer. username and password of the same username and password of your mysql server. returns true if the connection is established.

2. mysql_select_db($databasename,$connect); // databasename name of the database, $connect is the name of the connection your established see no.1.

3. mysql_query($query[, connection_id]); // $query is the sql command you want to execute;

4. mysql_close([connection id]) // To close the connection in database server.


$link = mysql_connect(‘localhost’, ‘root’, ”);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
echo ‘Connected successfully’;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: