<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Geek Files</title>
	
	<link>http://www.sunilb.com</link>
	<description>Blog about Project Management, Tutorial on PHP, MySQL, Java, SEO &amp; Internet Marketing</description>
	<pubDate>Mon, 08 Jun 2009 17:39:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/sunilb/Zgmn" type="application/rss+xml" /><feedburner:emailServiceId>sunilb/Zgmn</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>C Tutorial - Operators in C</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/i1TLJ8t0byw/c-tutorial-operators-in-c</link>
		<comments>http://www.sunilb.com/c-programming/c-tutorial-operators-in-c#comments</comments>
		<pubDate>Sun, 25 May 2008 08:43:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[C Programming]]></category>

		<category><![CDATA[C Tutorials]]></category>

		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/c-tutorial-operators-in-c</guid>
		<description><![CDATA[An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence.

Category [...]]]></description>
			<content:encoded><![CDATA[<p>An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence.</p>
<p><span id="more-89"></span></p>
<h3>Category of operators</h3>
<p><b>Unary Operators</b><br />
A unary operator is an operator, which operates on one operand. </p>
<p><b>Binary</b><br />
A binary operator is an operator, which operates on two operands</p>
<p><b>Ternary</b><br />
A ternary operator is an operator, which operates on three operands.</p>
<h3>Following are the different types of Operators</h3>
<p></b>Arithmetic Operator</b><br />
The arithmetic operator is a binary operator, which requires two operands to perform its operation of arithmetic. Following are the arithmetic operators that are available</p>
<table border="1">
<tr>
<td>Operator</td>
<td>Description</td>
</tr>
<tr>
<td>+</td>
<td>Addition</td>
</tr>
<tr>
<td>-</td>
<td>Subtraction</td>
</tr>
<tr>
<td>/</td>
<td>Division</td>
</tr>
<tr>
<td>*</td>
<td>Multiplication</td>
</tr>
<tr>
<td>%</td>
<td>Modulo or remainder</td>
</tr>
</table>
<p>At the time of using the ‘/’ division operator on two integers, the result will also be an integer. E.g. 100/8 will result in 12 and not 12.50.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Unary Operators</h3>
<p>A unary operator is an operator, which operates on one operand, i.e. it operates on itself.</p>
<p>Following are the unary operators available under C</p>
<p>!&nbsp;&nbsp;&amp;&nbsp;&nbsp;*&nbsp;&nbsp;-&nbsp;&nbsp;+&nbsp;&nbsp;~</p>
<p><b>Increment and Decrement Operators</b></p>
<p>These operators also fall under the broad category or unary operators but are quite distinct than unary minus.</p>
<p>The increment and decrement operators are very useful in C language. They are extensively used in for and while loops. The syntax of these operators is given below.</p>
<p>++<variable-name><br />
<variable-name>++<br />
&#8211;<variable-name><br />
<variable-name>&#8211;</p>
<p>The ++ operator increments the value of the variable by one, whereas the – operator decrements the value of the variable by one.</p>
<p>These operators can be used in either the postfix or prefix notation as follows:</p>
<p>Postfix: a++ or a&#8211;<br />
Prefix: ++a or –a</p>
<p><b>Postfix notation</b></p>
<p>In the postfix notation, the value of the operand is used first and then the operation of increment or decrement takes place, e.g. consider the statements below:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">int</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">;</span>
	a <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
	b <span style="color: #339933;">=</span> a<span style="color: #339933;">++;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“<span style="color: #339933;">%</span>d\n”<span style="color: #339933;">,</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“<span style="color: #339933;">%</span>d\n”<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Program Output</b><br />
10<br />
11</p>
<p><b>Prefix notation</b></p>
<p>In the prefix notation, the operation of increment or decrement takes place first after which the new value of the variable is used.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">int</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">;</span>
	a <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
	b <span style="color: #339933;">=</span> <span style="color: #339933;">++</span>a<span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“<span style="color: #339933;">%</span>d\n”<span style="color: #339933;">,</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“<span style="color: #339933;">%</span>d\n”<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Program Output</b><br />
11<br />
11</p>
<h3>Relational Operators</h3>
<p>Relational operators compare between two operands and return in terms of true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional &#038; looping statements.</p>
<p>Following are the various relational operators</p>
<table border="1">
<tr>
<td>&lt;</td>
<td>Less than</td>
</tr>
<tr>
<td>&lt;=</td>
<td>Less than or equal to</td>
</tr>
<tr>
<td>&gt;</td>
<td>Greater than</td>
</tr>
<tr>
<td>&gt;=</td>
<td>Greater than or equal to</td>
</tr>
</table>
<p>Closely related to the relational operators is the equality operator as follows:</p>
<table border="1">
</tr>
<td>==</td>
<td>Equal to</td>
</tr>
</tr>
<td>!=</td>
<td>Not equal to</td>
</tr>
</table>
<p><b>Example:</b></p>
<p>Suppose we have three integer variables a, b and c with values 1,2 and 3 respectively.</p>
<table border="1">
<tr>
<td><b>Expression</b></td>
<td><b>Returns</b></td>
<td><b>Meaning</b></td>
</tr>
<tr>
<td>A&lt;b</td>
<td>1</td>
<td>True</td>
</tr>
<tr>
<td>(a+b)&gt;=c</td>
<td>1</td>
<td>True</td>
</tr>
<tr>
<td>(b+c)&gt;(a+5)</td>
<td>0</td>
<td>False</td>
</tr>
<tr>
<td>(c!=3)</td>
<td>0</td>
<td>False</td>
</tr>
<tr>
<td>B==2</td>
<td>1</td>
<td>True</td>
</tr>
</table>
<h3>Logical Operators</h3>
<p>A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available in the C language.</p>
<table border="1">
<tr>
<td><b>Operator</b></td>
<td><b>Meaning</b></td>
</tr>
<tr>
<td>&#038;&#038;</td>
<td>Logical AND</td>
</tr>
<tr>
<td>||</td>
<td>Logical OR</td>
</tr>
<tr>
<td>!</td>
<td>Logical NOT</td>
</tr>
</table>
<p>Truth table for Logical AND</p>
<table border="1">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<p>Truth table for Logical OR</p>
<table border="1">
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<p><b>Example</b></p>
<p>Let us take three variables a, b, and c and assume a = 5,b=10 and c=7</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>b <span style="color: #339933;">&amp;&amp;</span> a<span style="color: #339933;">+</span>b<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>c</pre></div></div>

<p>Here a&gt;b will return false hence 0 and a+b&gt;c will return true hence 1. Therefore, looking at the truth table 0 AND 1 will be 0. From this we can understand, that this expression will evaluate to false.</p>
<p><b>Let us look at the same example using Logical OR (||)</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>b <span style="color: #339933;">||</span> a<span style="color: #339933;">+</span>b<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>c</pre></div></div>

<p>Here a&gt;b will return false hence 0 and a+b&gt;c will return true hence 1. Therefore, looking at the truth table 0 OR 1 will be 1. From this we can understand, that this expression will evaluate to true.</p>
<h3>The Logical NOT operator (!)</h3>
<p>The ! (Logical NOT) operator is a unary operator. This operator inverses a logical result. </p>
<p>E.g. if a = 5 and b = 7, then</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> b<span style="color: #009900;">&#41;</span></pre></div></div>

<p>will evaluate to be true. This expression works as follows, a==b evaluates to be false, now because of the ! (NOT) operator, the false is inverted to be true.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Assignment Operator</h3>
<p>An assignment operator (=) is used to assign a constant or a value of one variable to another. </p>
<p><b>Example:</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a <span style="color: #339933;">=</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">;</span>
b <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
interestrate <span style="color: #339933;">=</span> 10.5
result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">/</span>b<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Important Note:</b></p>
<p>Remember that there is a remarkable difference between the equality operator (==) and the assignment operator (=). The equality operator is used to compare the two operands for equality (same value), whereas the assignment operator is used for the purposes of assignment.</p>
<p><b>Multiple assignments:</b></p>
<p>You can use the assignment for multiple assignments as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a <span style="color: #339933;">=</span> b<span style="color: #339933;">=</span> c <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span></pre></div></div>

<p>At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.</p>
<p><b>Arithmetic Assignment Operators</b></p>
<p>Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.</p>
<table border="1">
<b>Operators</b></td>
<td><b>Example</b></td>
<td><b>Expands as</b><br />
<tr>
<td></td>
</tr>
<p>+=</td>
<td>Sum+=3</td>
<td>Sum = sum + 3<br />
<tr>
<td></td>
</tr>
<p>-=</td>
<td>Count-=4</td>
<td>Count -= 4<br />
<tr>
<td></td>
</tr>
<p>*=</td>
<td>Factorial*=num</td>
<td>Factorial = factorial * num<br />
<tr>
<td></td>
</tr>
<p>/=</td>
<td>Num/=10</td>
<td>Num = num /10<br />
<tr>
<td></td>
</tr>
<p>%=</td>
<td>A%=3</td>
<td>A = a % 3<br />
<tr>
<td></td>
</tr>
</table>
<p><b>Conditional Operator</b></p>
<p>A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.</p>
<p>Its general structure is as follows:</p>
<blockquote><p>
	Expression1 ? expression 2 (True Section): expression3 (False Section)
</p></blockquote>
<p><b>Example:</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">a<span style="color: #339933;">=</span>3<span style="color: #339933;">,</span>b<span style="color: #339933;">=</span>5<span style="color: #339933;">,</span>c<span style="color: #339933;">;</span>
&nbsp;
c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>a<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>b<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> a<span style="color: #339933;">+</span>b <span style="color: #339933;">:</span> b<span style="color: #339933;">-</span>a<span style="color: #339933;">;</span></pre></div></div>

<p>The variable c will have the value 2, because when the expression (a&gt;b) is checked, it is evaluated as false. Now because the evaluation is false, the expression b-a is executed and the result is returned to c using the assignment operator.</p>
<h3>Precedence of Operators</h3>
<table border="1">
<tr>
<td><b>Operator</b></td>
<td><b>Associavity</b></td>
</tr>
<tr>
<td>Unary</td>
<td>Right to Left</td>
</tr>
<tr>
<td>Arithmetic Operators</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Relational Operator</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Equality Operator</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Logical Operator</td>
<td>Left to Right</td>
</tr>
<tr>
<td>Conditional Operator</td>
<td>Right to Left</td>
</tr>
<tr>
<td>Assignment Operator</td>
<td>Right to Left</td>
</tr>
<tr>
<td>Comma operator</td>
<td>Right to Left</td>
</tr>
</table>
<h3>Type Conversion</h3>
<p>When an operator’s operands are of different data types, C automatically converts them to the same data type. This can affect the results of mathematical expressions. This is called type conversion. In C type conversion can be done by two ways, viz.</p>
<p><b>Automatic Conversion (Arithmetic Promotion)</b><br />
When a value is converted to a higher type, it is said to be promoted. Let’s look at the specific rules that govern the evaluation of mathematical expressions.</p>
<p><b>Rule 1:</b> chars, shorts, unsigned shorts are automatically promoted to int.<br />
<b>Rule 2:</b> When an operator works with two values of different data types, the lower data type is promoted to a higher data type.</p>
<p><b>Type Casting</b><br />
Type casting means to convert a higher data type to a lower data type. For the purpose of type casting we require to use the type case operator, which lets you manually promote or demote a value. It is a unary operator which appears as the data type name followed by the operand inside a set of parentheses. E.g.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	val <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span>number<span style="color: #339933;">;</span></pre></div></div>

<p>This was about Operators in C, in the next <b>C Tutorial</b> you will learn about input and output in C.</p>
<p>Subscribe now to receive updates when a new <b>C tutorial</b> is released.</p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>


<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/i1TLJ8t0byw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/c-tutorial-operators-in-c/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/c-programming/c-tutorial-operators-in-c</feedburner:origLink></item>
		<item>
		<title>C Tutorial - Constants, Variables and Data Type Modifiers</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/KKZUasmflZw/c-tutorial-constants-variables-and-data-type-modifiers</link>
		<comments>http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers#comments</comments>
		<pubDate>Sun, 25 May 2008 07:46:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[C Programming]]></category>

		<category><![CDATA[C Tutorials]]></category>

		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers</guid>
		<description><![CDATA[Constant
A constant can be defined as “a quantity that does not change during the execution of a program”.

#include &#60;stdio.h&#62;
&#160;
void main&#40;void&#41;
&#123;
        int a = 10;
        printf&#40;“%d”,a&#41;;
&#125;

Program Output
10

Here we are declaring a variable a with its initial value 10. 10 here is an [...]]]></description>
			<content:encoded><![CDATA[<h3>Constant</h3>
<p>A constant can be defined as “a quantity that does not change during the execution of a program”.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> a <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“<span style="color: #339933;">%</span>d”<span style="color: #339933;">,</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Program Output<br />
10</p>
<blockquote><p>
Here we are declaring a variable a with its initial value 10. 10 here is an integer constant and every time you try to execute this program, the output will always be 10.
</p></blockquote>
<p><span id="more-88"></span></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Types of Constants</h3>
<p><b>Integer Constant</b><br />
An integer constant must have atleast one digit and should not have decimal point. It could either be positive or negative.</p>
<p><b>Real Constant</b><br />
A real constant must have atleast one digit and must have a decimal point.</p>
<p><b>Character Constant</b><br />
A character constant should be only one character and must be enclosed in single quotes e.g. ‘A’, ‘b’, etc.</p>
<h3>Variables</h3>
<p>A variable is a name give to the space in the memory for holding data such as integers, characters, floating point numbers, strings, etc. Using a variable name is more convenient than using the memory locations. </p>
<p>There are certain rules for naming variables for which you need to refer to the rules for identifiers.</p>
<p><b>Declaration of variables</b><br />
To declare a variable means to create a memory space for the variable depending on the data type used and associate the memory location with the variable name. In C all the variables should be declared before it can be used. Also you cannot re-declare a variable once it has been already declared.</p>
<p>The general pattern for declaring a variable is as follows:</p>
<p><data-type> <variable-name-1>, <variable-name-2>, <variable-name-n>;</p>
<p>e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">int</span> age<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> age<span style="color: #339933;">,</span> count<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span>j<span style="color: #339933;">;</span>
	<span style="color: #993333;">float</span> amount<span style="color: #339933;">,</span> rateofinterest<span style="color: #339933;">,</span> totalbalance<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> ch<span style="color: #339933;">;</span></pre></div></div>

<p><b>Constant Variables</b></p>
<p>A constant variable is also called a named constant. A named constant is really a variable whose content is read-only and cannot be changed while the program is running. Here is a declaration of a named constant.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">const</span> <span style="color: #993333;">float</span> pi <span style="color: #339933;">=</span> <span style="color:#800080;">3.14</span><span style="color: #339933;">;</span></pre></div></div>

<p>It looks just like a regular variable declaration except that the word const appears before the data type name. const is a qualifier that tells the compiler to make the variable read only. Its value will remain constant throughout the programs execution. </p>
<p>An initialization value must be given when declaring a variable with the const qualifier, or an error will result when the program is compiled. A compiler error will also result if there are any statements in the program that attempt to change the contents of a named constant.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Data type modifiers</h3>
<p>We have so far learned about the fundamental data types, why they are used, their default sizes and values. Now let us understand the need for data type modifiers and the way they can be used.</p>
<p>Let us consider an example of a program, which will accept an age from the user to do some processing. Because the age is represented in numbers, so we will have to use the integer data type int. We all know that even under exceptional case an age of a person cannot exceed more than 150. Now, that we are using an integer data type it occupies 2 Bytes of memory, which would not be required to represent the value 150. Instead the value 150 could easily be saved in an integer of 1 Byte in size, but the default size of an integer is 2 Bytes. So we have a problem here.</p>
<p>Well, not that we can’t solve. To override the default nature of a data type, C has provided us with data type modifiers as follows:</p>
<p><b>signed</b><br />
By default all data types are declared as signed. Signed means that the data type is capable of storing negative values. </p>
<p><b>unsigned</b><br />
To modify a data type’s behavior so that it can only store positive values, we require to use the data type unsigned.</p>
<p>For example, if we were to declare a variable age, we know that an age cannot be represented by negative values and hence, we can modify the default behavior of the int data type as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">       <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> age<span style="color: #339933;">;</span></pre></div></div>

<p>This declaration allows the age variable to store only positive values. An immediate effect is that the range changes from (-32768 to 32767) to  (0 to 65536)</p>
<p><b>long</b><br />
Many times in our programs we would want to store values beyond the storage capacity of the basic data types. In such cases we use the data type modifier long. This doubles the storage capacity of the data type being used. E.g. long int annualsalary will make the storage capacity of variable annualsalary to 4 bytes.</p>
<p>The exception to this is long double, which modifies the size of the double data type to 10 bytes. Please note that in some compilers this has no effect.</p>
<p><b>short</b><br />
If long data type modifier doubles the size, short on the other hand reduces the size of the data type to half. Please refer to the example of age variable to explain the concept of data type modifiers. The same will be achieved by providing the declaration of age as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #993333;">short</span> <span style="color: #993333;">int</span> age<span style="color: #339933;">;</span></pre></div></div>

<p>This declaration above, will provide the variable age with only 1 byte and its data range will be from -128 to 127.</p>
<p>This was about constants, variables and data type modifiers. In the subsequent C Tutorials you will learn about C Operators.</p>
<p>Subscribe now to receive updates when a new <b>C tutorial</b> is released.</p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>


<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/KKZUasmflZw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/c-programming/c-tutorial-constants-variables-and-data-type-modifiers</feedburner:origLink></item>
		<item>
		<title>C Tutorial - Fundamental Data Types</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/ikgPvZgBO2U/c-tutorial-fundamental-data-types</link>
		<comments>http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types#comments</comments>
		<pubDate>Thu, 22 May 2008 04:08:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[C Programming]]></category>

		<category><![CDATA[C Tutorials]]></category>

		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types</guid>
		<description><![CDATA[In this C Tutorial we will learn what Fundamental Data Types are and how to use them.
We develop programs to accept an input from the user and then run the input through a series of processes and produce the output on screen or on the printer, etc. 

INPUT > PROCESS > OUTPUT
From the user’s point [...]]]></description>
			<content:encoded><![CDATA[<p>In this <b>C Tutorial</b> we will learn what Fundamental Data Types are and how to use them.</p>
<p>We develop programs to accept an input from the user and then run the input through a series of processes and produce the output on screen or on the printer, etc. </p>
<p><span id="more-87"></span></p>
<p>INPUT > PROCESS > OUTPUT</p>
<p>From the user’s point of view, the input supplied is data, which requires to be processed. Depending on the input provided by the user i.e. a number, fractional number, a name or an address, etc., the program must be designed to hold all these inputs. C provides the programmer with four basic or fundamental types of container to hold data, which are as follows:</p>
<table border="1">
<tr>
<td>Data Type</td>
<td>Bytes</td>
<td>Default Range</td>
</tr>
<tr>
<td>signed char</td>
<td>1</td>
<td>-128 to 127</td>
</tr>
<tr>
<td>Unsigned char</td>
<td>1</td>
<td>0 to 255</td>
</tr>
<tr>
<td>short signed int</td>
<td>2</td>
<td>-32768 to 32767</td>
</tr>
<tr>
<td>short unsigned int</td>
<td>2</td>
<td>0 to 65535</td>
</tr>
<tr>
<td>long signed int</td>
<td>4</td>
<td>-2147483648 to 2147483647</td>
</tr>
<tr>
<td>long unsigned int</td>
<td>4</td>
<td>0 to 4294967295</td>
</tr>
<tr>
<td>Float</td>
<td>4</td>
<td>-3.4e38 to +3.4e38</td>
</tr>
<tr>
<td>Double</td>
<td>8</td>
<td>-1.7e308 to +1.7e308</td>
</tr>
<tr>
<td>long double</td>
<td>10</td>
<td>-1.7e4932 to +1.7e4932</td>
</tr>
</table>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p><b>Following is the explanation of data types:</b></p>
<p><b>Character data type (char)</b></p>
<p>The character data type is only 1 Byte in size. It is used for only storing characters.</p>
<p><b>Integer Data type (int)</b></p>
<p>The integer data type is used to store whole numbers only. Fractional numbers are not allowed in an int data type.</p>
<p><b>Data type to store fractional numbers</b></p>
<p><b>float</b></p>
<p>The float data type is used to store floating point numbers or fractional numbers. This data type is 4 Bytes in size. Floating point numbers can also be expressed in the scientific notation e.g. 1.7e4 represents the number 1.7 x 10<sup>4</sup>.</p>
<p><b>double</b></p>
<p>The double data type is also used to store floating point numbers or a fractional numbers. This data type is 8 Bytes in size. This data type can hold a large value.</p>
<p>This was about fundamental <b>data types in c</b>. In the subsequent C Tutorials we will learn how to use these data types by creating variables.</p>
<p>Subscribe now to receive updates when a new <b>c tutorial</b> is released.</p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/ikgPvZgBO2U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/c-programming/c-tutorial-fundamental-data-types</feedburner:origLink></item>
		<item>
		<title>Introduction to C Programming</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/LV9IfutTBPs/introduction-to-c-programming</link>
		<comments>http://www.sunilb.com/c-programming/introduction-to-c-programming#comments</comments>
		<pubDate>Sun, 18 May 2008 14:53:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[C Programming]]></category>

		<category><![CDATA[C Tutorials]]></category>

		<category><![CDATA[C Tutorial]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/c-programming/introduction-to-c-programming</guid>
		<description><![CDATA[As a programming language, C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays [...]]]></description>
			<content:encoded><![CDATA[<p>As a programming language, C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.</p>
<p>Of the three languages, C allows the most precise control of input and output. C is also rather more terse than Fortran or Pascal. This can result in short efficient programs, where the programmer has made wise use of C&#8217;s range of powerful operators. It also allows the programmer to produce programs which are impossible to understand.</p>
<p>Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct term) will welcome the ease of use compared with some other languages. Undisciplined use of pointers can lead to errors which are very hard to trace. This course only deals with the simplest applications of pointers.</p>
<p>It is hoped that newcomers will find C a useful and friendly language. Care must be taken in using C. Many of the extra facilities which it offers can lead to extra types of programming error. You will have to learn to deal with these to successfully make the transition to being a C programmer. </p>
<p>The course aims to introduce programmers to the C language. Previous programming experience is assumed, so we can quickly progress to looking at the features of C and their uses. Students with little programming experience will need to do some homework in order to keep up with the lectures.</p>
<p><span id="more-86"></span></p>
<h3>Structure of a C Program</h3>
<p><b>Example</b></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*A simple C example program*/</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>”Hello World”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The output of the program is shown below. This is what appears on the screen when the program runs.</p>
<blockquote><p>Hello World</p></blockquote>
<p>Let’s examine the program line by line. Here’s the first line.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*A simple C example program*/</span></pre></div></div>

<p>Other than the two forward slash marks that begin the line, it looks pretty much like ordinary English. The /* marks the beginning of a comment. The compiler ignores everything from the double slash to the end of the line. That means you can type anything you want on that line and the complier will never complain. To end the comment you must close it with */</p>
<p>Although comments are not required, they are very important to programmers. Real programs are much more complicated than the example above, and comments help explain what’s going on. </p>
<p>The next line looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></pre></div></div>

<p>Because this line starts with a #, it is called a preprocessor directive. The preprocessor reads your program before it is complied and only executes those lines beginning with a # symbol. The word inside the angular brackets <> is a header file name, which is required to do the work of Input / Output. The contents of stdio.h are included at the point where the #include statement appears. The preprocessor directives should not end with a semicolon because preprocessors are messages to the compiler.</p>
<p>The next line reads:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This starts the beginning of a function. A function can be thought of as a group of one or more programming statements that collectively has a name. The name of this function is main, and the set of parentheses enclosing the word void indicate that it is a function. </p>
<p>Although most C programs have more than one function, every C program must have a function called main. It is the starting point of the program. If you are reading someone else’s C program and want to find where it starts, just look for the function called main.</p>
<p>The next line is a</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">     <span style="color: #009900;">&#123;</span></pre></div></div>

<p>This is called the left brace and marks the opening or beginning of a function, in our case main. All statements of the main are enclosed in a set of braces.</p>
<p>After the opening brace you see the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“Hello World”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This statement displays the string Hello World on screen. We will discuss more about the printf()later on in this chapter.</p>
<p>The last line is a</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This marks the end of the function main. Since main is the only function in the program it also marks the end of our program.</p>
<p><b>A special mention of the semicolon</b></p>
<p>A semicolon ; is used to mark the end of a statement in C. This allows the complier to understand that the end of a line has been reached. Usually a common mistake is that programmers omit the semicolon and try to compile the program.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>The C Character Set</h3>
<p>A character denotes any alphabet, digit or special symbol used to represent information. The C character set can be defined as a set of characters permissible under the C language to represent information. Under C, the upper case letters A-Z, the lower case letters a-z, the digits 0-9 and certain special symbols are all valid characters that can be used as building blocks to construct basic program elements.</p>
<p>Following is a table of character sets that are available under C</p>
<p><b>Alphabets</b><br />
A, B, C, ………………… Z<br />
a, b, c, ………………… z </p>
<p><b>Digits</b><br />
0, 1, 2, 3, 4, 5, 6, 7, 8, 9</p>
<p><b>Special Characters or Symbols</b><br />
+ - * &#038; ^ % $ # @ ! ? > < { } [ ] ( ) | \ / “ ‘ : ; . , _ </p>
<h3>Indentifiers in C</h3>
<p>In a C program every word is either classified as an identifier or a keyword.</p>
<p>Identifiers are used to identify or name various program-elements such as variables, symbolic constants, functions, etc.</p>
<p><b>Rules for Identifiers</b></p>
<ol>
<li>An identifier can contain letters or letters in any order except that the first character must begin with a letter.</li>
<li>C is case-sensitive, therefore upper-case identifiers are different from lower-case characters. E.g. name is different from NAME.</li>
<li>An identifier cannot contain any special character. The only exception is the under score character (_), which can also be taken as the first character of an identifier.</li>
<li>An identifier cannot be a keyword.</li>
</ol>
<p><b>Example of valid identifiers</b></p>
<p>name, rate1, rate_2, _myname, NAME</p>
<p><b>Example of invalid identifiers</b></p>
<p>1stname, my-name, rate 2</p>
<h3>Keywords</h3>
<p>Keywords are words, which have special meaning for the C compiler. Keywords cannot be used as identifiers because the programmer would then change the meaning of its implementation. Keywords are also sometimes known as reserved words.</p>
<p>Following is a list of keywords in C</p>
<table border="0">
<tr>
<td>auto</td>
<td>break</td>
<td>case</td>
<td>char</td>
<td>const</td>
</tr>
<tr>
<td>continue</td>
<td>default</td>
<td>do</td>
<td>double</td>
<td>else</td>
</tr>
<tr>
<td>enum</td>
<td>extern</td>
<td>float</td>
<td>for</td>
<td>goto</td>
</tr>
<tr>
<td>if</td>
<td>int</td>
<td>long</td>
<td>register</td>
<td>return</td>
</tr>
<tr>
<td>short</td>
<td>signed</td>
<td>sizeof</td>
<td>static</td>
<td>struct</td>
</tr>
<tr>
<td>switch</td>
<td>typedef</td>
<td>union</td>
<td>unsigned</td>
<td>virtual</td>
</tr>
<tr>
<td>void</td>
<td>volatile</td>
<td>while</td>
</tr>
</table>
<p>In the next series of tutorials, you will learn Data Types, Constants and Data type modifiers.</p>
<p>Subscribe now to receive updates when a new <b>c tutorial</b> is released.</p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/LV9IfutTBPs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/c-programming/introduction-to-c-programming/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/c-programming/introduction-to-c-programming</feedburner:origLink></item>
		<item>
		<title>Writing Website Scrapers in PHP</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/uSx-787D1fY/writing-website-scrapers-in-php</link>
		<comments>http://www.sunilb.com/php/writing-website-scrapers-in-php#comments</comments>
		<pubDate>Tue, 26 Feb 2008 15:35:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[PHP Code Examples]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Tutorial]]></category>

		<category><![CDATA[web programming]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/writing-website-scrapers-in-php</guid>
		<description><![CDATA[This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a powerful string processing capability. This article will teach you the basics of website scraping. The article will further cover a [...]]]></description>
			<content:encoded><![CDATA[<p>This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a powerful string processing capability. This article will teach you the basics of website scraping. The article will further cover a tutorial to find web ranking from Yahoo.com search engine.</p>
<p><span id="more-84"></span></p>
<h3>Steps involved to write a scraping program</h3>
<ol>
<li>Visit the URL</li>
<li>Understand the pattern</li>
<li>Validate the structure of pattern on different URLs</li>
<li>Write the program</li>
<li>Test the program using various input parameters</li>
</ol>
<p>Lets visit each of these steps one bit at a time.</p>
<h3>1. Visit the URL</h3>
<p>For this tutorial, we will extract Yahoo&#8217;s &#8220;Today&#8217;s Top Searches&#8221; section towards the end of their home page (http://www.yahoo.com/).</p>
<h3>2. Understand the pattern</h3>
<p>Before you begin to write a web scraping program, its important to understand the pattern of the data that you wish to extract. View the page source to understand the pattern. Mentioned below.</p>
<p>The string of text that we should parse is given below:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&nbsp;
&lt;div id=&quot;popsearchbd&quot; class=&quot;bd&quot;&gt;
&lt;ol start=1&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Heidi+Klum&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Heidi Klum&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Sarah+Larson&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Sarah Larson&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Oscar+Videos&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Oscar Videos&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Brad+Renfro&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Brad Renfro&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Gary+Busey&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Gary Busey&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;ol start=6&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Barack+Obama&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Barack Obama&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Razzie+Awards&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Razzie Awards&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Raisin+in+the+Sun&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Raisin in the Sun&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Stay+Home+Moms&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Stay Home Moms&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;r/dy/*-http://search.yahoo.com/search?p=Net+Neutrality&amp;cs=bz&amp;fr=fp-buzzmod&quot;&gt;Net Neutrality&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;
&lt;/div&gt;</pre></div></div>

<p>The pattern is that each Search Phrase is enclosed within a &lt;li&gt;&lt;a&gt;&lt;/a&gt;&lt;/li&gt; tag. Therefore, we should parse everything between &lt;a&gt;&lt;/a&gt; of this text piece to get the text.</p>
<p><center><br />
<code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
/* SB - PHP Scraper */
google_ad_slot = "3523700425";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></code><br />
</center></p>
<h3>3. Validate the structure of pattern on different URLs</h3>
<p>If you are writing a script to fetch data that has pagination, you should remember to validate the structure on 3 - 4 pages before you start developing code. The reason behind doing this is that the presentation of the first page could differ in subsequent pages.</p>
<h3>4. Write the program</h3>
<p>You could use any programming language like Java, C#, PHP, PERL, etc. for this processing. I have used PHP for this example.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">//change fso() to f sock open (my blog was causing an error)</span>
<span style="color: #666666; font-style: italic;">//change fwt() to f write (my blog was causing an error)</span>
<span style="color: #666666; font-style: italic;">//change fgs() to f gets (my blog was causing an error)</span>
<span style="color: #666666; font-style: italic;">//change fc() to f close (my blog was causing an error)</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> fso<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;www.yahoo.com&quot;</span><span style="color: #339933;">,</span> 80<span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> 30<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$errstr</span> (<span style="color: #006699; font-weight: bold;">$errno</span>)&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET / HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Host: www.yahoo.com<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	fwt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$str</span> <span style="color: #339933;">.=</span> fgs<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> 1024<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	fc<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;div id=<span style="color: #000099; font-weight: bold;">\&quot;</span>popsearchbd<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;div id=<span style="color: #000099; font-weight: bold;">\&quot;</span>popsearchbd<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;No information available&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fp-buzzmod<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fp-buzzmod<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$temppos</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;/a&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$datalength</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pos</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$temppos</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$temppos</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$datalength</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>5. Test the program using various input parameters</h3>
<p>You should test your program for all the parameters that the web page can take. I have experienced change in layout &#038; data based on the parameters that are passed.</p>
<h3>Notes on processing forms and cookies</h3>
<p>Some pages use form data and cookies to render data. In such cases you should remember to check the Request and Response headers and identify what is necessary to get the results that you want. If the page requires a cookie value, you should then use the cookie information in your Request Headers. Look at the note below that I use to inspect Request and Response headers.</p>
<p><center><br />
<code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
/* SB - PHP Scraper Bottom */
google_ad_slot = "6345077142";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code><br />
</center></p>
<h3>Tool to inspect Request and Response Headers</h3>
<p>I use Live HTTP Headers (a plug in for FireFox) to check for Request and Response Headers. Visit http://livehttpheaders.mozdev.org for more details. To install this plug in visit http://livehttpheaders.mozdev.org/installation.html and click on the &#8216;download it&#8217; link on the latest release. Please read the release notes before installing a particular version.</p>
<p>Please feel free to use the comments section down to share other tools that you guys use to monitor Request and Response Headers.</p>
<h3>Future Maintenance of the program</h3>
<p>From a maintenance perspective you should monitor the page frequently and re-validate the HTML structure. The reason for this is because nothing in this world is constant and so is the website. Design changes could result in a change in the HTML code. I recommend scheduling this activity for at least once a month.</p>
<p>This is all that is there in this tutorial. In the next tutorial, I will guide you step-by-step on how to create a program to check for yahoo.com search engine ranking. Use the subscribe form below to keep yourself updated when the tutorial goes live.</p>
<h5>Would you like to check out some of my <a href="http://www.sunilb.com/php-tutorial"><b>PHP5 Tutorials</b></a> as well?</h5>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>


<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/uSx-787D1fY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/writing-website-scrapers-in-php/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/php/writing-website-scrapers-in-php</feedburner:origLink></item>
		<item>
		<title>Will “Made in India” Software Products Become a Reality?</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/gAHJ8HudrlU/will-made-in-india-software-products-become-a-reality</link>
		<comments>http://www.sunilb.com/general/will-made-in-india-software-products-become-a-reality#comments</comments>
		<pubDate>Wed, 20 Feb 2008 15:04:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Business]]></category>

		<category><![CDATA[General]]></category>

		<category><![CDATA[Management]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/general/will-made-in-india-software-products-become-a-reality</guid>
		<description><![CDATA[A discussion with my friends brought about this topic. The discussion initially started with talks on the condition of Software Exports and the amount of revenue it brings to the Indian Economy as a whole. Though the Software Exports business (by way of consulting projects) has been good over the past so many years and [...]]]></description>
			<content:encoded><![CDATA[<p>A discussion with my friends brought about this topic. The discussion initially started with talks on the condition of Software Exports and the amount of revenue it brings to the Indian Economy as a whole. Though the Software Exports business (by way of consulting projects) has been good over the past so many years and have made billions of dollars for companies like Infosys, TCS, Wipro, etc., the question that we all had was about the future of the Software Exports business.</p>
<p><span id="more-83"></span></p>
<p>We all know that India has been receiving consulting projects because of the talent pool we have and secondly because of economical per hour rates when compared to USA and European countries. But, with time other countries are coping up. Their talent pool and per hour rates are comparable to ours.</p>
<p>Take China for example. To my understanding China is not far behind on grounds of talent pool and per hour rates. We have been slightly over-confident about the fact that such projects will keep coming to India because we are the largest English speaking country (<a href="http://en.wikipedia.org/wiki/List_of_countries_by_English-speaking_population">http://en.wikipedia.org/wiki/List_of_countries_by_English-speaking_population</a>). But for how long? How long will China be left behind? One fine day China will come up to our levels. What will be the competitive edge that we will have then?</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>I think it&#8217;s time that we start building software products and market them. Ultimately a product offers more power of earning because you develop once and then implement/install everywhere. We should definitely start to think in this direction and start building capacity both educational and entrepreneurial. After all why can&#8217;t we have the next best Operating System or an Office Productivity Suite or a Programming Language IDE, etc. etc.</p>
<p>I think India has a lot of scope as regards &#8220;Made in India&#8221; Software products. Think about it.</p>
<p>Please feel free to add your comments and replies.</p>
<p><b>Subscribe to free newsletters:</b></p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/gAHJ8HudrlU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/general/will-made-in-india-software-products-become-a-reality/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/general/will-made-in-india-software-products-become-a-reality</feedburner:origLink></item>
		<item>
		<title>7 habits of highly effective freelance programmers</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/YtVASQtnDio/7-habits-of-highly-effective-freelance-programmers</link>
		<comments>http://www.sunilb.com/programming/7-habits-of-highly-effective-freelance-programmers#comments</comments>
		<pubDate>Mon, 18 Feb 2008 17:45:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[C Programming]]></category>

		<category><![CDATA[Management]]></category>

		<category><![CDATA[PHP Class Examples]]></category>

		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[experiences]]></category>

		<category><![CDATA[freelance]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/programming/7-habits-of-highly-effective-freelance-programmers</guid>
		<description><![CDATA[I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over time with experience (good and bad). Please feel free to leave your experiences and comment on this article.]]></description>
			<content:encoded><![CDATA[<p>I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over time with experience (good and bad). Please feel free to leave your experiences and comment on this article.</p>
<p><span id="more-82"></span></p>
<h3>1. Communicate with your clients frequently</h3>
<p>This I believe is the most important habit that you should inculcate. Keeping the client in the loop ensures them that you are on top of things and that you are working on their project. Think about it! what if you were the client and were paying someone to develop something. Wouldn&#8217;t you want to hear from your vendor frequently?</p>
<p>Issues, slippages, project statuses should be frequently updated to the client (irrespective if your client asks or not). It’s professional ethics!</p>
<h3>2. Document interactions with your client</h3>
<p>Following point 1, you will have a lot of interactions with your client. You should record/document these interactions as a habit. These interactions could either be in the form of a chat message, emails or function specification documents. Maintaining a history of such interactions will help you during the closure of the project and mainly towards User Acceptance Testing (UAT).</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>3. Maintain on-going relations with all parties</h3>
<p>Just because a project is over does not mean that you should not maintain relations with your clients. Greeting your client on birthdays, anniversaries, etc makes them feel good. This exercise is good to build rapport with clients. It always keeps you fresh in their minds. So, the next time they have a project; guess who gets called first.</p>
<p>In addition to above, you should also maintain relations with suppliers or other freelancers who you outsource work to. In the long run it helps build trust and gives you a good bargaining power. Think about it.</p>
<h3>4. Get yourself a professional identity</h3>
<p>So what if you freelance, it doesn&#8217;t mean that you should not carry yourself professionally. Create professional looking logos, business cards, stationery and website (very important).</p>
<p>Get a professional copy writer to help you with the content of your website. You should mention the projects that you have done and the skills that you have. Always have a contact form on your website where prospective clients can contact you (and when you do get contacted, reply immediately).</p>
<h3>5. Seek testimonials from your clients</h3>
<p>Seek testimonials from your clients after closing a project. Get this on email and catalogue it for future reference. Also seek their permission to publish their testimonials on your website. In addition to this, you should consider requesting permission from your clients if its ok to give their contact information for reference checks. Some prospective clients would like to talk to your old clients and gauge the experience they have had with you.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>6. Create reusable libraries</h3>
<p>Creating reusable libraries makes you more profitable. Think about it! If you have been creating email validators on forms for nearly all projects, it makes more sense to create a reusable library and import that library in your current project and make use of it. This way your effort on the project in the long run is reduced and hence you get paid for something that has already been done.</p>
<p>In addition to this, if you discover a bug and fix it, you can apply a patch immediately to all your projects. Saves your time there too and helps all your clients with the bug fix in one go.</p>
<h3>7. Maintain proper accounts</h3>
<p>Many freelancers (including me) have failed at this. We fail at this not because we don&#8217;t know how to handle finances or accounts, but because we are programmers&#8230; we love to write code&#8230; not books. Do you agree with me?.</p>
<p>But the hard reality is that at the end of the financial year we need to pay taxes. To pay taxes properly we need a good accounting system. The choice is up to you, either be disciplined and maintain proper accounts from day one or waste a lot of time towards the end of the financial year doing it.</p>
<p>I recommend that you do it as you go along. I recommend that you dedicate a weekend to file your papers/accounts properly.</p>
<p>Please feel free to post your comments or add to this list your experiences and best practices.</p>
<p>Subscribe to my free newsletter for article updates:</p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

<p><b>Related Posts</b></p>
<ul>
<li><a href="http://www.sunilb.com/programming/12-common-programming-mistakes-to-avoid" alt="12 common programming mistakes to avoid">12 common programming mistakes to avoid</a></li>
<li><a href="http://www.sunilb.com/management/5-skills-every-programmer-must-learn" alt="5 skills every programmer must learn">5 skills every programmer must learn</a></li>
<li><a href="http://www.sunilb.com/general/5-reasons-ever-developer-should-freelance" alt="5 reasons ever developer should freelance">5 reasons ever developer should freelance</a></li>
<li><a href="http://www.sunilb.com/programming/ideas-on-debugging-your-live-website" alt="Ideas on debugging your live website">Ideas on debugging your live website</a></li>
<li><a href="http://www.sunilb.com/programming/5-tips-on-improving-programming-logic" alt="5 Tips on improving Programming Logic">5 Tips on improving Programming Logic</a></li>
<li><a href="http://www.sunilb.com/general/why-should-every-programmer-upgrade-knowledge" alt="Why should every programmer upgrade knowledge">Why should every programmer upgrade knowledge</a></li>
<li><a href="http://www.sunilb.com/general/6-billionaire-college-dropouts" alt="6 billionaire college dropouts">6 billionaire college dropouts</a></li>
</ul>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/YtVASQtnDio" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/programming/7-habits-of-highly-effective-freelance-programmers/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/programming/7-habits-of-highly-effective-freelance-programmers</feedburner:origLink></item>
		<item>
		<title>PHP 5 Tutorial - Handling Exceptions in PHP5</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/Bq5MKa-5Aos/php-5-tutorial-handling-exceptions-in-php5</link>
		<comments>http://www.sunilb.com/php/php-tutorials/php-5-tutorial-handling-exceptions-in-php5#comments</comments>
		<pubDate>Thu, 14 Feb 2008 08:58:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[PHP Class Examples]]></category>

		<category><![CDATA[PHP Code Examples]]></category>

		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<category><![CDATA[PHP5]]></category>

		<category><![CDATA[PHP5 Exceptions]]></category>

		<category><![CDATA[PHP5 OOPS]]></category>

		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php-5-tutorial-handling-exceptions-in-php5</guid>
		<description><![CDATA[In this tutorial we will cover the following:

What is an exception?
The use of a try&#8230;catch block
Anatomy of PHP5 Exception class
Extending the Exception class
A note on unhanded exceptions


What is an exception?
An exception is a logical/system error that occurs during the normal execution of a script. The exception could either be raised by the system or the [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we will cover the following:</p>
<ul>
<li>What is an exception?</li>
<li>The use of a try&#8230;catch block</li>
<li>Anatomy of PHP5 Exception class</li>
<li>Extending the Exception class</li>
<li>A note on unhanded exceptions</li>
</ul>
<p><span id="more-81"></span></p>
<h3>What is an exception?</h3>
<p>An exception is a logical/system error that occurs during the normal execution of a script. The exception could either be raised by the system or the program itself it the exception cannot be handled and the caller script/function needs to be informed about the same.</p>
<h3>The use of a try&#8230;catch block</h3>
<p>PHP5 introduces the try&#8230;catch block to trap exceptions. Look at the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
try <span style="color: #009900;">&#123;</span>
   check<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Message : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Code : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> check<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>some error condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error String&quot;</span><span style="color: #339933;">,</span>Error Code<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example, the method check() is called between the try {} block. The try{} block is the area where you will place your code that could raise an exception. Below the try{} block is the catch() {} block. The catch block expects the Exception type of object as a parameter. Within the catch() {} block you will place your logic to either fix the issue or log the error.</p>
<p>In the function check(), we raise an Exception using the &#8216;throw&#8217; keyword. The statement following &#8216;throw&#8217; is the syntax of creating a new object of Exception type. The exception class accepts two parameters. The left parameter is a string that is the error message and the right parameter is the integer error code that you wish to assign to that error.</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Anatomy of PHP5 Exception class</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Exception <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$code</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$line</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$trace</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> 0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLine<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTrace<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTraceAsString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	final <span style="color: #000000; font-weight: bold;">private</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example, except for __construct and __toString(), no other method can be overridden as all other methods are &#8216;final&#8217;.</p>
<p><b>Related Reading:</b></p>
<ul>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial - Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods" title="PHP 5 Tutorial - Final Class and Methods" target="_blank">PHP 5 Tutorial - Final Class and Methods</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial - Magic Methods - __toString() method</a></li>
</ul>
</blockquote>
<h3>Extending the Exception class</h3>
<p>You can also extend the exception class as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CustomerException <span style="color: #000000; font-weight: bold;">extends</span> Exception <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$t_message</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Exception raised in CustomerException &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$t_message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;with message : &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
&nbsp;
    parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$t_message</span><span style="color: #339933;">,</span> <span style="color: #000088;">$code</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> testException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	throw <span style="color: #000000; font-weight: bold;">new</span> CustomerException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CustomerException has been raised&quot;</span><span style="color: #339933;">,</span>101<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
	testException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>CustomerException <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error Message : &quot;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error Code : &quot;</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output:</strong></p>
<p>Error Message : CustomerException has been raised<br />
Error Code : 101</p>
<p><strong>Related Reading:</strong><br />
<a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5" title="PHP5 Tutorial - Inheritance">PHP5 Tutorial - Inheritance</a></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>A note on unhanded exceptions</h3>
<p>All un-handled exceptions are passed up the function stack order till the time either a try&#8230;catch block is not made available. If a try&#8230;catch block is unavailable it is passed to the PHP core and the program execution stops there.</p>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my free newsletter:</strong></p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> - Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial - Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial - Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial - Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-this-variable-explained" title="PHP5 Tutorial - $this variable explained">PHP5 Tutorial - $this variable explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-instanceof-operator-explained" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial - instanceOf Operator Explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-defining-class-constants" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial - Defining Class Constants</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5" title="PHP5 Tutorial - Inheritance">PHP5 Tutorial - Inheritance</a></li>
<li><a href="http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials - Abstract Class and Interface</a></li>
<li><a href="http://www.sunilb.com/php/php5-oops-tutorials-polymorphism" title="PHP5 OOPS Tutorials - Polymorphism">PHP5 OOPS Tutorials - Polymorphism</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods" title="PHP 5 Tutorials - Static Data Members and Methods">PHP 5 Tutorials - Static Data Members and Methods</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods" title="PHP 5 Tutorial - Final Class and Methods" target="_blank">PHP 5 Tutorial - Final Class and Methods</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial - Magic Methods - __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial - Magic Methods - __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial - Magic Methods - __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial - Magic Methods - __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial - Magic Methods - __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial - Magic Methods - __clone() method</a></li>
</ol>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/Bq5MKa-5Aos" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php-tutorials/php-5-tutorial-handling-exceptions-in-php5/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/php/php-tutorials/php-5-tutorial-handling-exceptions-in-php5</feedburner:origLink></item>
		<item>
		<title>PHP 5 Tutorial - Final Class and Methods</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/l_Tpqh3seJ0/php-5-tutorial-final-class-and-methods</link>
		<comments>http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods#comments</comments>
		<pubDate>Thu, 14 Feb 2008 06:41:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[PHP Class Examples]]></category>

		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<category><![CDATA[PHP5]]></category>

		<category><![CDATA[PHP5 OOPS]]></category>

		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods</guid>
		<description><![CDATA[In this PHP tutorial we will understand the following:

Meaning of Final Class
Meaning of Final Method
When to declare a class as final
When to declare a method as final


Meaning of Final Class
A final class is a class that cannot be extended. To declare a class as final, you need to prefix the &#8216;class&#8217; keyword with &#8216;final&#8217;. Example [...]]]></description>
			<content:encoded><![CDATA[<p>In this PHP tutorial we will understand the following:</p>
<ul>
<li>Meaning of Final Class</li>
<li>Meaning of Final Method</li>
<li>When to declare a class as final</li>
<li>When to declare a method as final</li>
</ul>
<p><span id="more-80"></span></p>
<h3>Meaning of Final Class</h3>
<p>A final class is a class that cannot be extended. To declare a class as final, you need to prefix the &#8216;class&#8217; keyword with &#8216;final&#8217;. Example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
final <span style="color: #000000; font-weight: bold;">class</span> BaseClass <span style="color: #009900;">&#123;</span>   
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;BaseClass method called&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//this will cause Compile error</span>
<span style="color: #000000; font-weight: bold;">class</span> DerivedClass <span style="color: #000000; font-weight: bold;">extends</span> BaseClass <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;DerivedClass method called&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DerivedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>
In the above example, BaseClass is declared as final and hence cannot be extended (inherited). DerivedClass tries to extend from BaseClass and hence the compiler will throw a compile error.
</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Meaning of Final Method</h3>
<p>A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the &#8216;final&#8217; keyword. Example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BaseClass <span style="color: #009900;">&#123;</span>   
   final <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;BaseClass method called&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DerivedClass <span style="color: #000000; font-weight: bold;">extends</span> BaseClass <span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//this will cause Compile error</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> myMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;DerivedClass method called&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DerivedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<blockquote><p>
In the above example, DerivedClass extends from BaseClass. BaseClass has the method myMethod() declared as final and this cannot be overridden. In this case the compiler causes a compile error.
</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>When to declare a class as final</h3>
<p>You should declare a class as final when you think that you implementation of that class should not change in the derived class. You should do this mainly for Utility classes where you don&#8217;t want the behavior/implementation of your class to change.</p>
<h3>When to declare a method as final</h3>
<p>You should declare a class method as final when you think that the method you develop contains necessary functionality to support your application and any modification or change to the functionality can cause unexpected errors/bugs.</p>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my free newsletter:</strong></p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> - Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial - Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial - Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial - Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-this-variable-explained" title="PHP5 Tutorial - $this variable explained">PHP5 Tutorial - $this variable explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-instanceof-operator-explained" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial - instanceOf Operator Explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-defining-class-constants" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial - Defining Class Constants</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5" title="PHP5 Tutorial - Inheritance">PHP5 Tutorial - Inheritance</a></li>
<li><a href="http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials - Abstract Class and Interface</a></li>
<li><a href="http://www.sunilb.com/php/php5-oops-tutorials-polymorphism" title="PHP5 OOPS Tutorials - Polymorphism">PHP5 OOPS Tutorials - Polymorphism</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods" title="PHP 5 Tutorials - Static Data Members and Methods">PHP 5 Tutorials - Static Data Members and Methods</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial - Magic Methods - __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial - Magic Methods - __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial - Magic Methods - __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial - Magic Methods - __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial - Magic Methods - __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial - Magic Methods - __clone() method</a></li>
</ol>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/l_Tpqh3seJ0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/php/php-tutorials/php-5-tutorial-final-class-and-methods</feedburner:origLink></item>
		<item>
		<title>PHP 5 Tutorials - Static Data Members and Methods</title>
		<link>http://feedproxy.google.com/~r/sunilb/Zgmn/~3/6lV0ORPO5uk/php-5-tutorials-static-data-members-and-methods</link>
		<comments>http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods#comments</comments>
		<pubDate>Tue, 12 Feb 2008 09:47:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[PHP Class Examples]]></category>

		<category><![CDATA[PHP Tutorials]]></category>

		<category><![CDATA[PHP5 OOPS Tutorials]]></category>

		<category><![CDATA[PHP5]]></category>

		<category><![CDATA[PHP5 OOPS]]></category>

		<category><![CDATA[PHP5 Tutorials]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods</guid>
		<description><![CDATA[In this tutorial you will learn all about static data members and methods

Meaning of static data members
Meaning of static methods
Defining static data members in PHP5
Defining static methods in PHP5
Accessing static data members in PHP5
Accessing static methods in PHP5
Rules to keep in mind for static methods


Meaning of static data members
A data member that is commonly available [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you will learn all about static data members and methods</p>
<ul>
<li>Meaning of static data members</li>
<li>Meaning of static methods</li>
<li>Defining static data members in PHP5</li>
<li>Defining static methods in PHP5</li>
<li>Accessing static data members in PHP5</li>
<li>Accessing static methods in PHP5</li>
<li>Rules to keep in mind for static methods</li>
</ul>
<p><span id="more-79"></span></p>
<h3>Meaning of static data members</h3>
<p>A data member that is commonly available to all objects of a class is called a static member. Unlike regular data members, static members share the memory space between all objects of the same class.</p>
<h3>Meaning of static methods</h3>
<p>A static method is a class method that can be called without creating an instance of a class. Such methods are useful when creating utility classes.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Defining static data members in PHP5</h3>
<p>To define a static member in PHP5 you need to prefix the class member name with the keyword &#8217;static&#8217;. Look at the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$first_name</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// regular member</span>
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$instance_count</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//static data member</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example $instance_count is declared as a static data member</p></blockquote>
<h3>Defining static methods in PHP5</h3>
<p>To define a static data methods in PHP5 you need to prefix the class method name with the keyword &#8217;static&#8217;. Look at the example below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInstanceCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<blockquote><p>In the above example getInstanceCount is declared as a static method</p></blockquote>
<h3>Accessing static data members in PHP5</h3>
<p>A static member data can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class</p>
<p>Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$instance_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//static data member</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">--;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInstanceCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output:</strong><br />
2</p>
<blockquote><p>In the above example, $instance_count is a static data member. Every time a new object is created the constructor is executed and the $instance_count variable is incremented by one. To echo the value contained in $instance_count variable, we use the :: (scope resolution) operator.</p></blockquote>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<h3>Accessing static method in PHP5</h3>
<p>A static method can be accessed using the name of the class along with the scope resolution operator (::) i.e. you don’t need to create an instance of that class. However, you can also access it with an instance variable.</p>
<p>Look at the example below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #009900;">&#123;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$instance_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//static data member</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">--;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFirstName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//body of method</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	static <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getInstanceCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> Customer<span style="color: #339933;">::</span><span style="color: #000088;">$instance_count</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$c1</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> Customer<span style="color: #339933;">::</span><span style="color: #004000;">getInstanceCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//this is using the scope resolution operator</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInstanceCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//this is using the instance variable</span></pre></div></div>

<p><strong>Output:</strong><br />
2<br />
2</p>
<h3>Rules to keep in mind for static methods</h3>
<ul>
<li>A static method can only access static data members</li>
<li>A static method does not have access to the $this variable</li>
</ul>
<p>Please feel free to write back for any clarification.</p>
<p><strong>Subscribe to my newsletter:</strong></p>
<form method="post" action=""><p>Your email:<br /><input type="text" name="email" value="Enter email address..." size="20" onfocus="if (this.value == 'Enter email address...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter email address...';}" /></p><p><input type="submit" name="subscribe" value="Subscribe" />&nbsp;<input type="submit" name="unsubscribe" value="Unsubscribe" /></p></form>

</ol>
<p><strong>Related Posts on <b>PHP5 Tutorial</b> - Object Oriented Programming (OOPS)</strong></p>
<ol>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-create-a-php5-class" title="PHP5 Tutorial - Learn to create a PHP5 Class" target="_blank">PHP5 Tutorial - Learn to create a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-learn-to-creating-a-php5-class-object" title="PHP5 Tutorial - Learn to Create a PHP5 Class Object" target="_blank">PHP5 Tutorial - Learn to Create a PHP5 Class Object</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-attributes-of-a-php5-class" title="PHP5 Tutorial - Defining Attributes of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Attributes of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-defining-methods-of-a-php5-class" title="PHP5 Tutorial - Defining Methods of a PHP5 Class" target="_blank">PHP5 Tutorial - Defining Methods of a PHP5 Class</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-creating-a-php5-constructor-__construct" title="PHP5 Tutorial - Creating a PHP5 Constructor __construct()" target="_blank">PHP5 Tutorial - Creating a PHP5 Constructor __construct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-creating-a-php5-destructor-__destruct" title="PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()" target="_blank">PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-oops-php5-class-access-specifiers-public-private-and-protected" title="PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected" target="_blank">PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-this-variable-explained" title="PHP5 Tutorial - $this variable explained">PHP5 Tutorial - $this variable explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-instanceof-operator-explained" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial - instanceOf Operator Explained</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-defining-class-constants" title="PHP5 Tutorial - instanceOf Operator Explained">PHP5 Tutorial - Defining Class Constants</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-inheritance-in-php5" title="PHP5 Tutorial - Inheritance">PHP5 Tutorial - Inheritance</a></li>
<li><a href="http://www.sunilb.com/php/php5-tutorials-abstract-class-and-interface" title="PHP5 Tutorials - Abstract Class and Interface">PHP5 Tutorials - Abstract Class and Interface</a></li>
<li><a href="http://www.sunilb.com/php/php5-oops-tutorials-polymorphism" title="PHP5 OOPS Tutorials - Polymorphism">PHP5 OOPS Tutorials - Polymorphism</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-tutorial-magic-methods-__tostring-method" title="PHP5 Tutorial - Magic Methods - __toString() method" target="_blank">PHP5 Tutorial - Magic Methods - __toString() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-__get-and-__set" title="PHP5 Tutorial __get() and __set()" target="_blank">PHP5 Tutorial - Magic Methods - __get() and __set()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__isset-and-__unset" title="PHP5 Tutorial - Magic Methods - __isset() and __unset()" target="_blank">PHP5 Tutorial - Magic Methods - __isset() and __unset()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__call-method" title="PHP5 Tutorial - Magic Methods - __call() method" target="_blank">PHP5 Tutorial - Magic Methods - __call() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__autoload-method" title="PHP5 Tutorial - Magic Methods - __autoload() method" target="_blank">PHP5 Tutorial - Magic Methods - __autoload() method</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__sleep-and-__wakeup" title="PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()" target="_blank">PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()</a></li>
<li><a href="http://www.sunilb.com/php/php-tutorials/php5-oops-tutorial-magic-methods-__clone-method" title="PHP5 Tutorial - Magic Methods - __clone() method" target="_blank">PHP5 Tutorial - Magic Methods - __clone() method</a></li>
</ol>

<img src="http://feeds.feedburner.com/~r/sunilb/Zgmn/~4/6lV0ORPO5uk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods/feed</wfw:commentRss>
		<feedburner:origLink>http://www.sunilb.com/php/php-tutorials/php-5-tutorials-static-data-members-and-methods</feedburner:origLink></item>
	</channel>
</rss>
