Owner: Learning Computer Programming URL:http://learning-computer-programming.blogspot.com Join Date: Tue, 12 Jun 2007 04:07:38 -0500 Rating:0 Site Description: I post articles, tips, tricks, techniques, algorithms etc. related to C++ Programming. Many of the things that I discuss also applies to other programming languages. Site statistics:Click here
Some Changes to Come... 2008-04-17 01:23:31 The name of this blog, as you know, is Learning Computer Programming but since
long (actually start) we’ve only been discussing about two programming
languages only (C & C++). I thought it’d be better if I discuss about
other programming languages too, and since I’m deeply caught up with PHP
as for now, we’ll be starting off with it.
This of course doesn’t mean that we’ll leave C++ or the topic of
this blog will shift to PHP rather it means that the discussion here will not
be limited to just one language.
This also doesn’t mean you’ll be seeing posts on all sorts of programming
languages, as I said for the time being I’ll be posting articles on PHP
and Web Programming.
Introduction to PHP Part III 2008-04-18 06:49:01 Please read the post Introduction
to PHP, How to Insert Dynamic Content on WebPages, if you haven’t
already.
Below is the code snippet from the previous post.
<html>
<body>
Date: <?php echo date("H:i A, jS F Y"); ?>
</body>
</html>
I’d like to state the following regarding the above code:
PHP code starts with a <?php and ends with a ?> tag. Though PHP supports
some other tags (depending on the configuration) which signify the same
but these are the most widely used and are guaranteed to work on most servers.
You can have any number of <?php …?> blocks in a file.
In PHP too, like C++ comments are written using // (single line) and /*…*/
(multi
Introduction to PHP, How to Insert Dynamic Content on WebPages 2008-04-18 01:33:58
In the last article Introduction
To PHP, The Web Programming Language, we talked about what
PHP is and what it is used for. There we talked about the Dynamic
insertion
of date & time on web pages. Well, in this post we’re going to ACTUALLY
create page that displays current date & time.
We’ll also briefly talk about how PHP pages are handled by the server
and the browser (client).
Let’s get started!
Suppose we have the following:
<html>
<body>
Date: 06:00 AM, 18th April 2008
</body>
</html>
Above is a simple HTML code (some tags are intentionally left-out)
No matter when and how many times you run it, you’d see the same output
(date and time), since it is a static HTML page.
Now have a look at the foll Read more:Content
Writing and Running PHP Scripts 2008-04-19 04:51:01 We’ve been talking about PHP for 2 days now; we even wrote a simple PHP
script and understood its working. Now comes a big question, “How do we
write and run PHP scripts?”. Do we need some special software to write
PHP code? How do we run these scripts?
Let’s know the answers!
Writing PHP scripts
You don’t need anything special; really, plain old Notepad would do just
fine. PHP is an interpreted language hence code remains as it is (not compiled)
thus any plain text editor is fine. Although syntax highlighted text editors
would be better.
If you are using a word processor instead, be sure to save the files as plain
text files and not in the proprietary formats.
Notepad, as I said may be used but I don’t recommend using it, instead Read more:Scripts
Installing Apache and PHP on your PC 2008-04-20 08:35:00 So you want to install web server on your own computer and have downloaded
Apache
and PHP. Please read
Installing
Web Server on your PC first, if in doubt.
Installing Apache web server
Installing Apache is a no-brainer. It is just as you install any other software.
Run the set-up, feed the information in each step and click finish to install.
It is a 4-5 step installation and wouldn’t take much time to get installed.
Be sure not install it as a Windows Sevice (default option).
After installation, fire-up your favorite browser. Type in
or (both refer top the local server) in the
address bar and press enter. You’ll see a welcome page stating that your
installation was successful. If it doesn’t show up, go to Start->Programs->Apache
HTT
Installing Web Server on your PC 2008-04-20 01:21:35 Why install web server on your own PC?
Running C++ programs or finding error (syntax) is as easy as clicking on the
Compile button but that is not the case with PHP. You have to first upload the
script file to the server to run it or to find any errors. As with anybody,
peoples make lots of syntax mistakes in the beginning when they learn a new
language. So sending back and forth scripts from your PC to the server every
time you forget a ‘;’ here or a ‘,’ there via FTP is
not something many would like to do.
If you have a server set-up on your own PC then it’s as simple as copy
and paste, there would be no need of FTP or anything of that sort. Write the
script, put it in the special folder (created by the web server) and there you
a
Conditional Statements if...else in PHP 2008-04-21 11:29:01 Look at the following code:
<?php
$t=(int) date("G");
if($t<12)
echo "Good Morning!";
else if($t<17)
echo "Good Afternoon!";
else if($t<21)
echo "Good Evening!";
else
echo "Good Night!";
?>
If you are a reader of this blog (or know C++!) then the code would look familiar.
As is obvious, that is a PHP code but looks similar to C++.
You see, the if and else statements are all similar to that in C++.
Talking about the code, it is pretty straightforward, we are taking the current
time, type casting it to an integer in a variable $t (type casting is also done
in the similar manner in C++). Then we are printing different messages according
to what the current time is.
That being clear, le
Configuring Apache Web Server and PHP 2008-04-21 05:37:45 [Update: You may download the MSI
package of PHP instead of the ZIP one which would do all the set-up and
configuration itself and you wouldn’t have to do the following by yourself]
This is the crucial part guys, first I start off with giving the locations
of the configuration files of Apache
and PHP.
Apache: [X:\…]\Apache Software Foundation\Apache2.2\conf\httpd.conf
PHP: [Y:]\PHP\php.ini-dist [renamed to php.ini later]
Now let’s start guys:
1. Start by backing up both the configuration files, you’d
need them in case you mess up the files.
2 .Rename the php.ini-dist file to php.ini
3. Look for a line like below
doc_root
and make it look like
doc_root =[X:\…]\Apache Software Foundation\Apache2.2\htdocs
be sure to change ’[X:\ Read more:Configuring
, Web Server
Taking User Inputs to Create Personalized Pages 2008-04-23 09:17:01 You might have probably seen this type of URLs:
It of course is a dynamic web page. As a matter of fact the string after the
‘?’, the data, is what makes it dynamic. The page is dynamic because
most pages which take data this way, create the page according to the data passed.
In this post we’re going to see how pages take data, we’ll also
create a simple dynamic page which can be personalized to display user’s
name.
First, let’s understand what is in the URL
index.php is a PHP page and everything after the ‘?’ is the data
being passed, which is
name=noname&age=18
here name and age are the two variables having the values noname and 18 respectively
which are being passed. Each variable is separated by a ‘& Read more:Taking
, Personalized
, Pages
Variables, Type Casting and Constants in PHP 2008-04-23 02:15:36 Let’s start by looking at the fundamental difference between variables
in PHP and in C++:
Variables in PHP need not be declared before using
Variables can hold any type of values due to the fact that variable are
not declared of any type, you can store any value in any variable no matter
which type of value it is currently holding.
So, in the previous post (Conditional
Statements if...else in PHP) when we needed a variable to
hold the integer (hour of the day) we just wrote
$t=(int) date(“G”);
and suppose we now want to have a string to be stored in $t variable, we’d
just have to write
$t=”PHP”;
You see, in the first statement it was an integer variable but now it’s
a string. Therefore we can con Read more:Casting
An Example of User Authentication System in PHP 2008-04-24 12:05:01 In this post we’re going to create a very simple user authentication
system in PHP. It’d be like the one’s you see while logging in to
various sites/services (emails, forums, social networking sites etc)
User authentication is a way for sites to know who you are among the other
registered users and showing you relevant content (may be confidential). For
example it’s only you ho is authorized to see your emails because you
only know your authentication information.
In this post we’re going to create two files, a HTML page which will
collect the username and password in a form. These information will then be
send to a PHP script, which will verify and show the required information.
Below is the PHP code:
<?php
//define some constants Read more:Example
, Authentication
, System
Taking User Inputs to Create Personalized Pages II 2008-04-24 05:04:04 HTML Forms are a method of sending information from a page to a script. Forms
in HTML have the following form:
<form name="form1" id="form1" method="get" action="script.php">
</form>
here,
name=”form1” is the name of the form, may be anything
id=”form1” is usually same as the name of the form
method=”get” is the method by which sending of data will take
place. It can also be “post”.
action=”script.php” is the name of the script to which the data
is to be passed to.
A form can have many child elements such as input box, password box, check
box etc. a form almost always has a submit button which on clicking sends the
data entered in various elem Read more:Taking
, Personalized
, Pages
How does CMS Create Dynamic Pages 2008-04-26 05:22:59 Content Management System (CMS) gives you an easier way to manage sites. It
gives you a nice front-end to write, publish and manage content while hiding
the technical details like FTP, HTML and other coding work. Most of the CMSs
have a web based front-end that means you can manage the whole site from within
the browser. Some examples of popular CMS are Joomla, Wordpress etc.
One major characteristic of CMS is the fact that it creates the whole site
dynamically, it means most (if not all) of the pages that the site has, are
created dynamically. Conventional way of creating site was to create different
HTML pages for each article (page) the site has. Contrary to that most CMS don’t
create different files for pages. They store the content in the Database and
Read more:Dynamic
, Pages
How does CMS Create Dynamic Pages II 2008-04-26 05:18:07 This is the continuation of the last post How
does CMS Create DynamicPages
.
If you run the script given on that post (save it with the name “cms.php”),
you’d see a site like below:
As you can see, it’s a simple five page site of which all of the five
pages are available (only 3 are shown in the image though).
Isn’t it amazing for just one PHP script to create a five page site!
If you look at the code and try to understand, you see that the script is designed
to show the Homepage when no data is passed. It creates different pages from
the data in the arrays when the respective page is asked for, by passing p=0
to p=4 to the script.
We can create 10, 20, 100 or even a 1000 page site like this just from one
script. In fact most CMS do tha
An Example of User Authentication System in PHP II 2008-04-25 01:46:05 This is a short follow-up of the last post An
Example
of User AuthenticationSystem
in PHP. In this post we’ll
talk about the two methods of from sending GET and POST and how thy affect the
way data sending
From the previous posts example, when we provided the username and password
and clicked on submit, we saw something like this:
If you look at the address bar, you can see the data (username and password)
being sent. Now, that’s not a good thing, if we are using a password box
to hide the password being entered then what its use is if it can be seen this
way!
The good thing is that with very few modifications, the data passed can be
made invisible (not to appear on the address bar). How? By using POST method
of data sending for the HTML form.
It
Properties of String in PHP 2008-04-28 01:26:10 1. String
s in PHP can either be enclose in single quotes (‘)
or double quotes (“).
$str=’Strings in PHP’;
$str2=”PHP”;
both of the above are valid strings in PHP.
2. Two strings can be concatenated or joined together with
the help of the “.” Dot operator.
$str=”I Like “.”PHP”;
$str2=$str.” a lot!”;
Now, $str will have the string “I Like PHP a lot!”.
3. Guess what the following code will print
$str=”String”;
echo “This is $str”;
I bet many of you thought that it’d print “This is $str”.
But actually it is going to print “This is String” because variables
inside (“) double quotes are evaluated in Read more:Properties
Designing a Simple Order Form Application in PHP 2008-04-28 01:19:31
Ok guys, for this post we’re going to create a simple yet
complete order form page. Order
forms are used on many sites to take customers
order online. Order forms should have the capability to take orders from visitors
regarding what items they want to purchase and store the information for further
processing.
For this post’s example, we are going to create an order form for a Book
Seller. The form will be designed to take order of five different items (books).
Our order form application should be able to take order of five different items
in any separate quantities tht user wants, it should also ask for shipping address
and name of the customer. It should then store the information provided in a
file along with the date and time order was placed. Th Read more:Designing
, Simple
, Application
How File Processing is done in PHP? 2008-04-27 01:32:25 In the previous post’s (Designing
a Simple Order Form Application in PHP) example we implemented the
file I/O for storing order information without discussing about it.
For those of you who were eager to know more about PHP File I/O read along,
this post has it.
File processing requires the following steps:
Opening a file
Doing the operation
Closing the file
Opening a file
First of all we have to open the file before any operation (reading/writing)
can be done.
If you remember the previous post Designing
a Simple Order Form Application in PHP, we had this line
//open file
$fp=fopen("orders.txt","a");
There we’re opening a file named “orders.txt” in the “append”
file mode. File modes tell PHP what we want t Read more:Processing
String Manipulation functions in PHP 2008-04-29 08:43:37 In the previous post Properties
of String
in PHP, we were discussing about the different properties
of strings in PHP. String manipulation as you know, is an important part of
web programming. PHP being a web programming language thus provides good set
of string manipulation functions. In this post we’re going to discuss
some of those which arte frequently needed.
1. trim() function
Prototype: string trim (string str);
This function strips white spaces from the start and end of the string supplied
returning the resulting string..
When we have to take user input via form, it’d be a good idea to “trim”
the variables as extra white spaces sometimes creep in.
$name=trim($_GET['name'];
2. explode() function
Prototype: array&nbs Read more:Manipulation
Designing a Simple “Quote of the Day” Script in PHP 2008-04-30 01:52:40 “Quote
of
the Day” is quite an old feature, people put on their sites. And of course
people like it; I mean who doesn’t like to know the sayings of great persons!
In this post we’re going to design a complete quote of the day system
to place on our web site.
The title is a bit misleading actually, because most of these types of scripts
show a new quote (randomly) each time the page is requested and not once a day.
So if you incorporate this script on our web site, you’d see a different
quote each time you reload the page. You may easily change it to “actually”
display different quotes for each day only but people like that random one!
The working of the quote scrip is pretty simple. We first store a good number
of quote i Read more:Designing
, Simple
Arrays and Array Processing in PHP 2008-04-30 01:45:35 Arrays are a set of variables of the same data type. If you’ve ever programmed
before you should be knowing about it. but PHP takes arrays to the next level
by providing so many useful in-built features to operate on them, let’s
see some of them.
Types of Arrays
You should be familiar with the following type of array:
$ar[0]=1;
$ar[1]=10;
It is an example of numerically indexed array since numerical values are used
as indices to reference different elements of the array.
One other type of array that PHP supports is those having string indices which
sometimes may make indexing more meaningful.
$temp['jan']=21;
$temp['feb']=23;
$temp['mar']=24;
This type of array is also called Associative Arrays.
As is obvious, the array above should most Read more:Array
, Processing
Creating a Simple ‘Contact Us’ Form in PHP 2008-05-03 05:04:52
If you’ve a website, giving your visitors a way to contact you is very
important. The easiest way for you would be to just give your email address
but for the visitors that surely won’t be an easy method. A more sophisticated
method would rather be a ‘Contact Us’ form, yeah just like those
you see on many websites.
A contact form generally asks the visitors name, email address and message
they would like to send to the webmaster of the website.
In this post, we are going to create a simple contact form, which you can even
integrate on your own site.
For the ‘Contact Us’ form, we need to create two different files.
First one will have the HTML form and some text boxes to collect the information
form the visitor, second, the PHP sc Read more:Creating
, Simple
String Manipulation Functions in PHP II 2008-05-05 04:20:06
PHP has got wide range of in-built functions for manipulating strings, in the
post StringManipulation
functions in PHP we discussed some of them which are frequently
used/needed.
Here we’ll be discussing about the rest of the functions, but again not
all of them.
implode() or join() function
Prototype: string implode(string separator, array arr);
This function does the opposite of explode() function. Explode divides a string
into array of strings on a separator, this one joins an array of strings with
a separator to form a string. join() and implode() functions are identical.
$str="PHP is a web programming language.";
$ar=explode(' ',$str);
//now $ar contains each word of the str Read more:Functions
Creating a ‘Contact Us’ Form (E-Mail Version) 2008-05-04 01:23:07
This is a short follow-up of the last post Creating
a Simple ‘Contact Us’ Form in PHP.
In the last post we discussed how we can create a simple contact form for our
website. That surely will give visitors an easy way to contact you. But I guess
many of you would be thinking that it’d have been better if contact form
could just send emails. If you are one of them, keep reading!
To be true guys, PHP gives us a dead easy way of sending emails from scripts,
thanks to the mail() function. mail() function in its simplest form has the
following prototype:
bool mail(string to, string subject, string msg);
Here,
to- email address, mail is to be sent to
subject- subject of the email
msg- body of the email
For our contact form, we
Creating a Simple Visitor Counter in PHP 2008-05-06 02:41:41 Visitor
counter is an easy way to track number of visitors coming to a web
site, while sophisticated trackers can give detailed statistics of visitors,
a simple visitor counter like the one we’re going tot create, would only
track the number of times a web page (or site) has been requested.
A simple visitor counter which only tracks number of pageloads is very easy
to create. It is obvious that the number of pageloads has to be preserved across
different runs so we need to store that data in a Database or file. To ease
things we’ll use a file.
Every time the page (with the script) is requested the value (number of pageloads)
will be read from and incremented in the file. The initial value in the file
should be 0.
Here is the code:
<?php
//functio Read more:Creating
, Simple
, Counter
All About User-Defined Functions in PHP 2008-05-07 05:24:02
So far we’ve been using PHPs built-in functions to do our tasks; we also
declared and used user-defined function in the post Creating
a Simple Visitor Counter in PHP. However, we didn’t discuss much
about them. For those of you who are curious to know more about user-defined
functions or those who’d doubts, I’ve written this post. Read along
to know more about user-defined functions.
Declaring functions
In PHP functions are declared using the keyword ‘function’ as below:
function func-name(arg-list)
{
…
}
Here ‘arg-list’ may be empty if you don’t want the function
to take any arguments.
If you remember from the post Creating
a Simple Visitor Counter in PHP, we’d sta Read more:Functions
Sorting Arrays in PHP…Some Sorting Functions 2008-05-07 05:05:18
Oh, so you want to learn how to sort arrays in PHP. But, did I tell you that
PHP has inbuilt sorting functions. Well, I’ve now! So this post will not
be anything but discussion on those sorting functions. Let’s look at them
without wasting any more time.
sort() Function
This function, as the name suggest can be used to sort single dimensional arrays
in ascending order. PHP is intelligent enough to also sort string arrays very
well besides numerical arrays.
$ar=array(1,11,38,65,2,99);
sort($ar);
//gives array(6) { [0]=> int(1) [1]=> int(11) [2]=>
// int(38) [3]=> int(56) [4]=> int(65) [5]=> int(99) }
The optional second a Read more:Sorting
, Functions
Other Ways of Initializing Arrays in PHP 2008-05-08 05:11:21
We had discussed about Arrays in PHP in the post
Arrays and Array Processing in PHP. We learnt how we can
simply initialize an array by giving values to different indices or using the
‘array’ construct. Besides those methods there are a few others
that you should know, which is the topic of this post.
Creating Sequential Arrays
There are times when you want to store sequence or pattern of values in an
array. For that task range() function would prove to be very helpful. Suppose
if we want to have an array having ten elements from 1 to 10. We can use the
following tedious method:
$ar[0]=1;
$ar[1]=2;
$ar[2]=3;
…
…
$ar[9]=10;
or the range function like:
$ar=range(1,10);
the above line of code creates an arrays same as the
What is Session Control/Variables? 2008-05-10 02:20:57
In the post An
Example of User Authentication System in PHP we created a simple authorization
system which could show a personalized page when the user enters correct username
and passwords. But since HTTP is a stateless protocol (it can’t figure
out if two subsequent requests come from the same user) we cannot preserve the
state (logged in) on ant consecutive clicks. All it means is that after logging
in into that script, if the user clicks on some link, there is no way we can
preserve the logged in state (know that a logged in user is requesting a page).
Therefore we cannot, that way, personalize the whole site for the logged in
user.
So, only logging in someone is not all, we’ve tpo preserve that state
across the whole session. For this PHP giv Read more:Control
Implementing User Authorization Using Session Control 2008-05-10 02:20:53
You may want to read An
Example of User Authentication System in PHP, What
is Session Control
/Variables? before reading this post.
In this post we are going to create a ‘secret’ site whose pages
will only be accessible after logging in using the correct username and password.
We will be using our knowledge of Session
Control/Variables since we want authorization for the whole site and
not a single page.
The site that we are going to create will have three ‘secret’ pages
plus one-one page for logging in and homepage.
Let’s start by having a look at the login page code:
<html>
<head>
<title>My Web Site | Login</title>
</head>
<body>
<h1>My Web Site</h1>
<h2&g