 |
 |
|
|
| |
| |
| |
|
Statistics |
| Unique Visitors: 866 |
| Total Unique Visitors: 1664234 |
| Visitors Out: 3082 |
| Total Visitors Out: 4061 |
|
|
|
| |
|
|
| |
|
| Some Useful Array Manipulation Functions in PHP |
| 2008-05-09 02:09:47 |
Please read Arrays
and Array Processing in PHP for an introduction to arrays in PHP. You
may also want to read
Sorting
Arrays in PHP…Some Sorting Functions and Other
Ways of Initializing Arrays in PHP.
shuffle() Function
This function can shuffle or randomly arrange the elements of an arrays. It
takes an arrays as argument and manipulates on the original array passed.
$ar=array(1,2,3,4,5,6,7);
shuffle($ar);
//$ar=array(7) { [0]=> int(5) [1]=>
//int(4) [2]=> int(3) [3]=> int(1)
//[4]=> int(6) [5]=> int(7) [6]=> int(2) }
//will be different for each run
As you can see the elements of the array ‘$ar’ passed have been
re-ordered randomly by the shuffle() function.
This function can be useful when we’d like to have a random element from
an array and the order of the elements is not impo...
|
| |
|
| Designing a Simple ‘Shout Box’ Script in PHP |
| 2008-05-09 01:57:28 |
Shout Boxes are quite a popular widget that people put on their web site or
blog. It gives peoples a very easy way to interact with each other.
In case you don’t know what shout box is, you can think of it as a chat
script that you can put on a part of an existing web page. People can leave
their messages (usually short) there and can interact with each other and with
the webmaster. One special feature is that anybody can leave their message without
any registrations. That’s why it’s the perfect way to interact with
your visitors and let the visitors interact with each other.
Designing a simple shout box script is actually not difficult. In this post
we’re going to design one.
A shout box usually needs the user to fill name (optional), URL (optional)
and message to be posted. When an URL is provided ‘name’ becomes
a hyperlink.
As is obvious, the messages that people post must be saved somewhere. Again
Database is the be...
|
| |
|
| 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 one above.
This function can also be use with the optional third argument to set the step
between values.
Eg.
$ar=range(1,10,2);
//$ar={1,3,5,7,9}
range() function also works with character,...
|
| |
|
| 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 stated that no matter whether
the function returns a value or not, no return type has to be specified for
the function (unlike C/C++).
a. Functions having parameters
function myfunc($a)
...
|
| |
|
| 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 argument if specified can be SORT_REGULAR (default), SORT_NUMERIC
or SORT_STRING. It can be used to explicitly tell the sort function the way
you want the arrays to be sorted. Most commonly numer...
|
| |
|
| 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
//function
function showCounter()
{
//open the file in read & write mode
$fp=fopen("counter.txt","r...
|
| |
|
| String Manipulation Functions in PHP |
| 2008-05-05 04:21:59 |
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 explode (string separator, string input, int limit);
The argument “limit” is optional.
This function splits the string “input” on a specified ...
|
| |
|
| 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 String
Manipulation 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 strings as
//different elements
$str2=implode(' ',$ar);
//again $str2 is joined using ' ' (spaces)
//now $str=$str2
strstr() function
Prototy...
|
| |
|
| Designing a Simple “Quote of the Day” Script in PHP |
| 2008-05-04 01:25:42 |
“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 in an array (here we’ll just be storing a handful as an example).
Generate a random number from 0 to NUM_ELEMENTS_IN_ARRAY-1 and just show the
quote from the array with that index.
Here is ...
|
| |
|
| Creating a Simple ‘Contact Us’ Form in PHP |
| 2008-05-04 01:25:42 |
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 script to receive those information and write
it to a file for the webmaster to see.
For this project, we’re writing the information sent by the user to a
file which you should be checking e...
|
| |
|
| 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’ll have to give our own (or whoever the webmaster
is) email for the ‘to’ argument (since we should get the messages
sent form the form), ‘subject’ can be anything ...
|
| |
|
| 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 probably be having temperatures
of different months. Had it been numerically indexed, it wouldn’t have
made that much sense. So you can use these types of arrays to make indexing
and re...
|
| |
|
| Properties of String in PHP |
| 2008-04-28 01:26:10 |
1. Strings 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 PHP. If we had used (‘)
single quotes for the string like:
$str=”String”;
echo ‘This is $str’;
It’d have printed “This is $str” as varia...
|
| |
|
| 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. The application should also
be able to take any number of orders and store them all linearly for further
human processing.
For this, we need a front end of a HTML form to which the user would inte...
|
| |
|
| 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 to do with the file.
Some of the commonly used files modes along with what they mean is listed below:
r
For reading only, reading begins from the start of the file
r+
Reading ...
|
| |
|
| 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
create the pages from the Database. That means the content don’t reside
in pages or files but rather in the Database.
In this post we’re going to create a simple system that will help y...
|
| |
|
| 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 Dynamic Pages.
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 that.
Do remember however that the array storing the content was just to depict the
database and real sites would store content in database.
Did you notice the line $page=$_GET[‘p’];
As ...
|
| |
|
| An Example of User Authentication System in PHP |
| 2008-04-25 02:05:31 |
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
define("USERNAME", "goodjoe");
define("PASSWORD", "123456");
define("REALNAME", "Joe Burns");
//have the data being passed
$u...
|
| |
|
| 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 Authentication System 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 can be done like below:
<html>
<head>
<title>Simple Uesr Authentication System</title>
</head>
<body>
<form name="form1" method="po...
|
| |
|
| Taking User Inputs to Create Personalized Pages |
| 2008-04-25 01:36:34 |
You might probably have seen this type of URLs:
http://somepage.com/index.php?name=noname&age=18
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
http://somepage.com/index.php?name=noname&age=18
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 ‘&’
sign.
That’s it; now let’s see how we can catch these variables from
...
|
| |
|
| 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"http://learning-computer-programming.blogspot.com/2008/04/taking-user-inputs-to-create.html">Taking
User Inputs to Create Personalized Pages, so we need not work on that,
here we’ll only create a HTML page having a form which will send the data
to that script. For this purpose, we need an input box for the user to enter
their name and obviously the submit button, as the form elements.
Look at the following HTML code:
<html>
<head>
<title>Enter Your Name</title>
</head>
<body>
<h2>Enter your name </h2>
<form name="form1"http://learning-computer-programming.blogspot.com/2008/04/taking-user-inputs-to-create.html">
Taking User Inputs to Create Personalized Pages) in the same directory
on a server and you’re ready to go.
Open the HTML page ...
|
| |
|
| 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 conclude that the type of a variable is determined
by the value currently assigned to it.
PHP has the following types of data:
Integer
Float
String
Boolean
Array
Object
Since PHP takes c...
|
| |
|
| 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, let’s us look at the working of date() function a bit.
We’ve previously used date function in the post Writing
and Running PHP Scripts. As I explained, it takes a string as an argument...
|
| |
|
| 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:\…]\’ with the location you installed
Apache server in. Save the file.
4. Open the folder you installed PHP in. There you’ll
see lots of DLL files, select them all and click copy. ...
|
| |
|
| Installing Apache and PHP on your PC |
| 2008-04-21 05:25:27 |
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 http://localhost
or http://127.0.0.1 (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
HTTP Sever->Control Apache Sever->START.
Installing PHP
Installation of PHP is too, straightforward but to integrate and configure
it to work with Apache is a bit...
|
| |
|
| 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
are.
This is by far the main reason that makes people install web servers on their
home PC.
What does installing a web server do?
Nothing, in the sense that your PC runs the way it used to.
It insta...
|
| |
|
| 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
use code editors like Programmers
Notepad, Notepad++(really
great, supports numerous third-party plug-ins)that have syntax highlighting
feature. That would make you easily understand the c...
|
| |
|
| Introduction to PHP Part III |
| 2008-04-19 04:48:08 |
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-line) symbols. Besides these one more symbol i.e. ‘#’
symbol can also be used for single line comments.
[Update: Added this point] Each statement of PHP ends with a semic...
|
| |
|
| 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 following
<html>
<body>
Date: <?php echo date("H:i A, jS F Y"); ?>
</body>
</html>
This is a HTML document having embedded PHP code.
As I said PHP can...
|
| |
|
| Introduction To PHP, The Web Programming Language |
| 2008-04-17 09:24:00 |
PHP is a recursive acronym for PHP Hypertext Preprocessor, though it originally
stood for Personal Home Page. It is designed specifically for the web, hence
a web programming language.
PHP is a server-side scripting language which can be either embedded into HTML
documents or used alone. Since PHP is a interpreted language, when someone requests
a page containing PHP, the code is interpreted on the server and the output
(often HTML) is returned to the client web browser.
As you might have guessed, PHP can help you generate different outputs depending
on the conditions and generate different pages depending on conditions programmed,
hence able to create what wee call “Dynamic Pages”.
For example, suppose we want to put the current date and time on our web page,
HTML alone won’t do any good. So a nice option would be to use PHP (in
fact, any sever-sided programming language would be fine. It could also be done
with JavaScript but due to it ...
|
| |
|
| 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 Microcontrollers |
| 2008-04-06 01:42:31 |
This is a guest post by Avinash over at Extreme
Electronics. Check out his website to learn more about electronics
and microcontroller programming.
Computer programming is exciting! Isn’t it. It gives you the felling
of power in your hands, power to create and innovate. You can create solution
for many everyday problems. Many programs you create are simple doesn’t
requires much RAMs only few hundred bytes for variables, array etc. And the
input outputs are simple. For example a “calculator” application.
Your computer has resources many times more than what is required to run that
app.
The thing I want to say is that you can easily make a small computer at very
cheap cost (less than Rs 300/$6.00). For any specific purpose and make it run
your programs. You can make any dream device by using variety of hardware like
simple led, seven segment display, switches, sensors to advance Text/graphics
LCD modules, PC interface et...
|
| |
|
| How Bitwise Operators are Used, an Example Program |
| 2008-03-15 01:53:27 |
Well, one-by-one we’ve discussed each of the Bitwise
Operator. Starting from Operation
on Bits and Bitwise Operators, we moved on to Right/Left
Bit Shift Operators then discussed Decimal
Number to Binary Conversion Program. and at last One's
Complement and XOR Operators. After having so much theoretical it’s
time now for a nice Example Program, which is the topic of today’s post.
The code here is basically to show how these bitwise operator are used rather
than what they are used for.
// Example Program to demonstrate how
// One's Complement (~) and XOR (^)
// Opeartors are used.
#include<stdio.h>
// prototype
void showbits(short int);
// defined
void showbits(short int dec_num)
{
short int loop, bit, and_mask;
for(loop=15; loop>=0; loop--)
{
and_mask=1<<loop;
bit=dec_num&and_mask;
if(bit==0) printf("0");
else printf("1");
}
}
void main()...
|
| |
|
| One's Complement and XOR Operators |
| 2008-03-15 01:45:55 |
Talking about Bit
Operators we are left with two of them, which we’ll be discussing
in this article.
One’s Complement Operator (~)
It takes and works only on one operand. On taking one’s complement of
any variable, the 0s are changed to 1 and vice-versa from the bit structure
(binary representation) of that variable. The following example will make it
easier to understand:
Suppose we have a short int a
short int a = 16;
its binary representation will be
0000000000010000 (decimal 16)
on taking one’s complement like below
res = ~a;
res will contain
1111111111101111 (decimal 65519)
It can be used as a part of algorithm to encrypt data.
XOR (eXclusive OR) (^)
It is derived from the OR
Operator and takes two operands to work on. It compares bits like the
OR
bitwise operator but exclusively for OR cases.
Following will clarify what it does:
short int a = 46265, its binary form
1011010010111001
another short int b = 46734, binary
101101101000...
|
| |
|
| Decimal Number to Binary Conversion Program |
| 2008-03-14 02:01:51 |
Please read Operation
on Bits and Bitwise Operators and Right/Left
Bit Shift Operators if you haven’t already. This post is based
on those articles.
We’ll be using the following operators and the respective properties
for decimal to binary conversion:
AND (&) Operator from the article Operation
on Bits and Bitwise Operators: Its property to be able to check
whether a particular bit is ON (1) or OFF (0).
Left Bit Shift Operator (<<) from the article Right/Left
Bit Shift Operators: Its property to shift bits (of byte(s)) to
desired number of places to the left.
After making you guys familiar with the two things above, the program will
be easier to understand.
// Example Program to convert decimal number
// to binary equivalent.
// --------
// Function: We have defined a function 'showbits()'
// It shows the bit structure of the
// Short Int(2 Bytes) passed as argument
#inc...
|
| |
|
| Right/Left Bit Shift Operators |
| 2008-03-13 07:54:48 |
This is the continuation of the article Operation
on Bits and Bitwise Operators. If you haven’t read that, it is
strongly recommended that you do, before proceeding with this article.
Bit shifting, as the name signifies, does shifting of bits in byte(s). There
are basically two ways, in which bits (of a byte) can be shifted, either to
the right, or to the left. Thus we have two types of bit shifting operator.
If you think logically, its pretty clear that for bit shifting in a byte, we
need to have two data. We need the byte(s) to shift bits on and the number of
bits to be shifted. Guess what, the two operators need these to data as operands!
Right Bit Shifting Operator (>>)
Syntax: res = var >> num;
This would shift all bits in the variable var, num places
to the right which would get stored to the variable res. So
for example if var has the following bit structure:
var = 00110101 (decimal 53)
And we do the following operation:
res = var &...
|
| |
|
| Let Us Grow Our Community |
| 2008-03-12 07:32:52 |
Click
Here to Submit Your Article
I guess many sites/blogs don’t talk about it too often but I am ;-)
These are the number of articles that I’ve posted for the respective
months, it’s clear that for 4-5 months I’ve not been able to post
much. Although there is a thing about quality over quantity but I don’t
think it makes as an excuse. Does it?
Amazingly though, the number of visitors and page views have increased
months after month.
In the span of about 9 months with 112 articles, we have somewhat formed
our own community.
I know that many of our visitors are good programmers and have the ability
to share their knowledge and expertise.
If you feel like you have/know something that you’d like to share with
thousands of other peoples on our community, then please submit your articles
to us. We’ll post it here and give you the platform.
Along with the articles, a short note about the author (including author’s
website and e-mail address) will also be published. So it...
|
| |
|
| Electronics Hobby Shop Launched |
| 2008-03-11 05:46:31 |
You
may skip this post if you’re not a resident of India
Microcontrollers such as AVR, PIC from Atmel and Microchip are fast becoming
the choice of hobbyist for their projects. Now instead of many conventional
ICs hobbyists are rather using a single MCU (MicroController Unit) for their
projects due to many advantages they have.
In the recent years MCUs have become cheap too. Now you may easily get
a fully functional MCU from Atmel at under Rs. 70!
Sadly that is only one dimension of its popularity if you don’t reside
in those big cities, leaving some big cities MCUs are not that easily available
let alone the remote areas. There are many resellers but they either deal in
large quantities or at high prices both not being suitable for average hobbyists.
Seeing this, my brother has come up with a new Online
Electronics Hobby Shop for average hobbyists. He has planned
to make available everything (Development Boards, MCUs, LCD Displays, Programmers,
...
|
| |
|
| Operation on Bits and Bitwise Operators |
| 2008-01-16 00:46:19 |
OK guys, this is my first post in the New Year 2008, I thought of posting it
earlier but at last I didn’t. It’s already been so long since I
posted so let’s keep everything aside and talk just about what we have
for today. ;-)
I was sitting the other day thinking about what to write for a post here. Suddenly
I realized that we have discussed operations
on matrices, arrays,
and what not but we haven’t had the chance to talk anything about the
most fundamental thing a computer understands. Yeah, Operation on Bits.
Bits can have only two values either ON (1) or OFF (0). In this article, we’ll
be discussing about the different operations which can be performed on bits.
One thing to note here is, we don’t perform these operation on single
bits but rather on a group of bits (byte(s)). So even though Bitwise operators
operate on bits its almost always a part of a group (byte, which makes up each
data type), it means we can do bitwise operations on any data type.
BTW, the operators that perform operation on bits are called Bitwise Operator
and such operations are known as Bitwise Operations
The six bitwise operators are listed below:
&
AND
|
OR
^
XOR
>>
Right shift
<<
Left shift
~
One’s complement
For this post we’ll only be discussing &(AND) and | (OR) operators
leaving the rest for future posts ;-)
Bitwise AND (&) Operator: First thing, it’s nothing to do with the
Logical (&&) operator, both are different.
Now, if you know something about Logic Gates then you might already know about
this. For the rest of us, it does an AND mask on bits.
So, suppose if we have two separate bytes having binary values as 00100000
and 00100001 then doing AND oper...
|
| |
|
| Inline Functions and their Uses |
| 2007-12-11 03:14:59 |
It’s a good practice to divide the program into several functions such
that parts of the program don’t get repeated a lot and to make the code
easily understandable.
We all know that calling and returning from a function generates some overhead.
The overhead is sometimes to such an extent that it makes significant effect
on the overall speed of certain complex and function-oriented programs. In most
cases, we have only a few functions that have extensive use and make significant
impact on the performance of the whole program.
Not using functions is not an option, using function-like macros is an option,
but there is a better solution, to use Inline Functions.
Yes, like it sounds, inline functions are expanded at the place of calling
rather than being “really called” thus reducing the overhead. It
means wherever we call an inline function, compiler will expand the code there
and no actual calling will be done.
Member functions of classes are generally made inline as in many cases these
functions are short but any other function can be inline too. Functions are
made inline by preceding its definition with the keyword “inline”
as shown in the example below:
// precede the definition
// of function with "inline"
// it's optional to do so for
// the prototype
inline ret-type func-name(arg-list...)
{
...
...
...
}
Member functions (class) can be made inline as below:
class myclass
{
private:
...
...
public:
ret-type func-name(arg-list...);
...
...
};
inline ret-type myclass::func-name(arg-list...)
{
...
...
}
Or simply as
class myclass
{
private:
...
...
public:
inline ret-type myclass::func-name(arg-list...)
{
...
...
}
...
...
};
Short member functions are usually made inline as above.
Please mote one thing though, inline is a request to the compiler a...
|
| |
|
| |
 |
|
| |
| |
|
 |