<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web and designers &#124; Complete resource platform for web designers and developers &#187; PHP</title>
	<atom:link href="http://www.webanddesigners.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.webanddesigners.com</link>
	<description>Web blog to help web designers and developers</description>
	<lastBuildDate>Wed, 08 Sep 2010 13:14:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction to PHP &#8211; Part10</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part10</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part10#comments</comments>
		<pubDate>Wed, 16 Dec 2009 11:37:01 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[begineers]]></category>
		<category><![CDATA[php begineers]]></category>
		<category><![CDATA[PHP class]]></category>
		<category><![CDATA[PHP Object]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=1014</guid>
		<description><![CDATA[Classes and Objects:
The programming paradigm we have learned so far is called Procedural Paradigm. In Procedural programming paradigm, programmer writes a program as collections of independently behaving procedures and makes use of procedure call to access those procedures. As we already discussed in Part8, how procedural programming is a better choice than a simple unstructured [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part9' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part9'>Introduction to PHP &#8211; Part9</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Classes and Objects:</strong></h2>
<p>The programming paradigm we have learned so far is called Procedural Paradigm. In Procedural programming paradigm, programmer writes a program as collections of independently behaving procedures and makes use of procedure call to access those procedures. As we already discussed in Part8, how procedural programming is a better choice than a simple unstructured programming. It has been a long time, programmers have started thinking about new paradigm called Object oriented paradigm (OOP) which provides a better way to develop big and complex system than procedural programming. We don’t want to confuse readers by putting too complex terms and advantages of OOP at this stage, we will discuss when required.<span id="more-1014"></span></p>
<p>In OOP, a class is a language construct that is used as a template or blue print to create instances of class called objects. This tutorial explains step by step to make the things clear. </p>
<p><strong>Creating Class</strong></p>
<p><strong>Example#1</strong></p>
<p>The example below creates a class called Person. It has two instance variables; name and address and few methods. The instance variables are not accessible outside the class when they are defined as private; we need methods to access them. But public variable can be accessed outside the class. All instance variables should be kept private and there should be some methods to access those variables. Here, we have SET and GET methods which are be public. All methods should be public; otherwise they cannot be accessed outside the class.</p>
<p>After the end of class, we have created a instance “p” of class “Person”. “p” is called object of class “Person”. Note that, “p” object binds instance variables and methods together, that property of OOP is known as <em>encapsulation</em>.</p>
<pre class="brush: php;">
&lt;?php
class Person
{
// Properties or Instance variables
private $name;
private $address;

//Methods or behaviours
//set methods are used to set the instance variables
              public function setName($aName)
             {
	         $this-&gt;name= $aName;
    	}
	public function setAddress($aAddress)
	{
       		$this-&gt;address= $aAddress;
    	}
	//get methods are used to get the instance variables
	public function getName()
	{
      		 return $this-&gt;name;
    	}
	public function getAddress()
	{
        		return $this-&gt;address;
    	}
}// end of class

//create a object of Person class
$p = new Person();
//Set name
$p-&gt;setName(&quot;Gary Tom&quot;);
//Set address
$p-&gt;setAddress(&quot;Unit 10 Nelson Bay, NSW 2030 Australia&quot;);
//Print name
echo $p-&gt;getName();
//Print Address
echo $p-&gt;getAddress();
?&gt;
</pre>
<p>The output of the above code will be<br />
Gary Tom Unit 10 Nelson Bay, NSW 2030 Australia</p>
<p><strong>Constructor and Destructor</strong></p>
<p>When we create an object constructor method is called, hence it is useful to initialize the instance variables before they are ready to use. Similarly, destructor method is called when we destroy the objects.</p>
<p><strong>Example#2</strong></p>
<p>The code we have in this example produces the same output as in example#1, but has been implemented in a different way. Here we initialize the instance variables using constructors.</p>
<pre class="brush: php;">
&lt;?php
class Person
{
    	// Properties or Instance variables
   	 private $name;
	private $address;

	//Default Constructor
	public function __construct()
	{
       		$this-&gt;name= &quot;Gary Tom&quot;;
	   	$this-&gt;address= &quot;Unit 10 Nelson Bay, NSW 2030 Australia&quot;;
   	}
      	//Default Destructor
   	function __destruct()
   	{
     		//This portion of the code will be executed when
	 	// we destroy an object explicitly.
   	}

  	//get methods are used to get the instance variables
	public function getName()
	{
       		return $this-&gt;name;
    	}
	public function getAddress()
	{
        		return $this-&gt;address;
    	}
}// end of class

//create a object of Person class
$p = new Person();
//Print name
echo $p-&gt;getName();
//Print Address
echo $p-&gt;getAddress();
?&gt;
</pre>
<p>This code will also produces the same output as in example#1, that is<br />
Gary Tom Unit 10 Nelson Bay, NSW 2030 Australia</p>
<p><strong>Inheritance</strong></p>
<p>As the name suggests, inheritance is a principle by which child class can inherit the methods form its parent class and the methods performs their original functionality.</p>
<p><strong>Example#3</strong></p>
<p>In this example, we extend a class called <em>Student</em> from the class <em>Person</em>. The subclass <em>Student</em> inherits <em>printMe</em> methods and all instance variables from the parent class <em>Person</em>. The <em>printMe</em> method of <em>Person</em> class has been rewritten with the same name in <em>Student</em> class. If a method with same name exists in parent and child class, the child class object has two version of the same module. In this situation the child class method overrides the parent class method, it known as method overriding.  When we call <em>printMe</em> method of <em>Student</em> object, it calls <em>printMe</em> of <em>Student</em> class not the <em>printMe</em> of <em>Person</em> class. Remember that all data members of Person class are available in Student class.</p>
<pre class="brush: php;">
&lt;?php
class Person
{
    	// Properties or Instance variables
    	private $name;
	private $address;

   	//Function to print Instance variables
   	public function printMe()
   	{
   		echo $this-&gt;name.&quot;, &quot;.$this-&gt;address;
   	}
}// end of Person class

//Student class, inherits from Person class
class Student extends Person
{
	//Instance variables, remember that name and address is inherited from Person
	private $program;

	//Default Constructor
	public function __construct()
	{
       		$this-&gt;name= &quot;Gary Tom&quot;;
	   	$this-&gt;address= &quot;Unit 10 Nelson Bay NSW 2030 Australia&quot;;
	   	$this-&gt;program= &quot;Masters in IT&quot;;
   	}

   	public function printMe()
   	{
   		echo $this-&gt;name.&quot;, &quot;.$this-&gt;address.&quot;, &quot;.$this-&gt;program;
   	}
}// end of Student class

//create a object of Person class
$s = new Student();
//Print s
$s-&gt;printMe();
?&gt;
</pre>
<p>The output of the above code will be<br />
Gary Tom, Unit 10 Nelson Bay NSW 2030 Australia, Masters in IT</p>
<p>This tutorial tries to describe Classes and Objects in short, for a complete reference please visit http://www.php.net/manual/en/language.oop5.php</p>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part9' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part9'>Introduction to PHP &#8211; Part9</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part10/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part9</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part9</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part9#comments</comments>
		<pubDate>Sun, 06 Dec 2009 07:03:05 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[GET]]></category>
		<category><![CDATA[get method]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[post method]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=983</guid>
		<description><![CDATA[Forms:
A form is an area where user can input data. The place in the form where user input data is called form element. A form element can be a text field, text area field, drop-down list, radio buttons and checkbox. In this tutorial we will apply the knowledge we have gained so far and use [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Forms:</strong></h2>
<p>A form is an area where user can input data. The place in the form where user input data is called form element. A form element can be a text field, text area field, drop-down list, radio buttons and checkbox. In this tutorial we will apply the knowledge we have gained so far and use it with HTML forms. The best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. That means the data entered in HTML forms can be manipulated using PHP scripts.<span id="more-983"></span></p>
<p><strong>Creating form</strong></p>
<p>A form is created using &lt;form&gt; tag. To learn more about HTML please refer to HTML tutorials available in net. Let us start with a simple HTML form that we use for user feedback.</p>
<p><strong>Example#1</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;form &gt;
 &lt;p&gt;Name : &lt;input type=&quot;text&quot; name=&quot;name&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Email : &lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Subject : &lt;input type=&quot;text&quot; name=&quot;subject&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Message : &lt;br/&gt;
 &lt;textarea rows=&quot;10&quot; cols=&quot;30&quot;&gt; &lt;/textarea&gt;
 &lt;p&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
</pre>
<p>The output of the above HTML code will be<br />
<img class="alignnone" src="http://www.webanddesigners.com/wp-content/uploads/2009/12/Form.jpg" alt="" width="286" height="374" /></p>
<p><strong>Collecting information from HTML form using POST method</strong></p>
<p>As we already mentioned, the best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. After filling the form, when we pressed <em>Submit Query</em> button, everything we filled in the form will be available in PHP script.</p>
<p><strong>Example#2</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;form action=&quot;action.php&quot; method=&quot;post&quot;&gt;
 &lt;p&gt;Name : &lt;input type=&quot;text&quot; name=&quot;name&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Email : &lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Subject : &lt;input type=&quot;text&quot; name=&quot;subject&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Message : &lt;br/&gt;
 &lt;textarea rows=&quot;10&quot; cols=&quot;30&quot;&gt; &lt;/textarea&gt;
 &lt;p&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
</pre>
<p>We can notice that we added <em>action</em> and <em>method</em> in the above code, which tells the server what <em>action</em> is to be taken and what <em>method</em> is to be used. In this example, when we click on <em>Submit Query</em> button the page <em>action.php</em> is called and the <em>method</em> used is <em>post</em>. In order to access the form elements we should write <em>action.php</em> like this. Note that the file name should be <em>action.php</em> and saved in the same directory.</p>
<p><strong>action.php</strong></p>
<pre class="brush: php;">
&lt;?php
echo $_POST[&quot;name&quot;].&quot;&lt;br/&gt;&quot;;
echo $_POST[&quot;email&quot;].&quot;&lt;br/&gt;&quot;;
echo $_POST[&quot;subject&quot;].&quot;&lt;br/&gt;&quot;;
echo $_POST[&quot;message&quot;];
?&gt;
</pre>
<p>When we fill the form and click the submit button, <em>action.php</em> will be called, the URL will look like <a href="http://webanddesigners.com/action.php">http://webanddesigners.com/action.php</a>. Information filled in the form is now available in action.php page. <em>post</em> method use $_POST array to save the form information, the name of the form fields will be the key in the $_POST array.</p>
<p><strong>Collecting information from HTML form using GET method</strong></p>
<p>The following code is exactly same as code in example#2, except that the method used here is GET.</p>
<p><strong>Example#3</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;form action=&quot;action.php&quot; method=&quot;get&quot;&gt;
 &lt;p&gt;Name : &lt;input type=&quot;text&quot; name=&quot;name&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Email : &lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Subject : &lt;input type=&quot;text&quot; name=&quot;subject&quot; /&gt;&lt;/p&gt;
 &lt;p&gt;Message : &lt;br/&gt;
 &lt;textarea rows=&quot;10&quot; cols=&quot;30&quot;&gt; &lt;/textarea&gt;
 &lt;p&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
</pre>
<p>action.php will look like as the following code. Note that when GET method is used the data elements are available in $_GET array. The data elements are accessed in the similar fashion.</p>
<p><strong>action.php</strong></p>
<pre class="brush: php;">
&lt;?php
echo $_GET[&quot;name&quot;].&quot;&lt;br/&gt;&quot;;
echo $_GET[&quot;email&quot;].&quot;&lt;br/&gt;&quot;;
echo $_GET[&quot;subject&quot;].&quot;&lt;br/&gt;&quot;;
echo $_GET[&quot;message&quot;];
?&gt;
</pre>
<p><strong>Comparison of GET and POST method</strong></p>
<p>When we use GET method, the information sent is visible. It will display the variable and value in browser’s address bar. The address bar will look like <a href="http://webanddesigners.com/action.php?name=John&amp;email=john@webanddesigner.com&amp;subject=hi&amp;message=This+is+the+test+message+using+get+method">http://webanddesigners.com/action.php?name=John&amp;email=john@webanddesigner.com&amp;subject=hi&amp;message=This+is+the+test+message+using+get+method</a>. There is a limitation in the amount of data send using GET method, maximum of 100 characters can be send. One best thing about GET method is that, the page can be bookmarked; this is because all the variables and data are available in URL.</p>
<p>When we use POST method the information sent is completely invisible. In addition, there is no restriction on the amount of information to be sent. Up to 8mb of data can be sent using POST method. However, because data and variables are not available in URL, it is not possible to bookmark the page. The URL will look like <a href="http://webanddesigners.com/action.php">http://webanddesigners.com/action.php</a></p>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part9/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part8</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part8</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part8#comments</comments>
		<pubDate>Wed, 02 Dec 2009 11:41:23 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[php function]]></category>
		<category><![CDATA[php functions]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=930</guid>
		<description><![CDATA[Function:
A function is a subprogram that performs a specific task when called by the main program. It is always a good practice to divide a huge program into a number of subprograms until elementary functions are reached. More clearly, a huge program should be divided into number of functions, and a function precisely should do [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part10' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part10'>Introduction to PHP &#8211; Part10</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part4' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part4'>Introduction to PHP &#8211; Part4</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Function:</strong></h2>
<p>A function is a subprogram that performs a specific task when called by the main program. It is always a good practice to divide a huge program into a number of subprograms until elementary functions are reached. More clearly, a huge program should be divided into number of functions, and a function precisely should do one job. Suppose, we have a big program that reads data from user at the beginning, save data to a database and displays the saved data at the end. <span id="more-930"></span>We can divide this program into three subprograms or functions. The first function reads data from user, the second function writes data to the database and finally third function writes data to monitor.</p>
<p>There are number of advantages of implementing a huge program into a number of subprograms. The first advantage is we can reuse the existing code. During a single execution of a program, a function can be called several times from different places and even from another function. The second advantage is it is easy to understand the program logic, debugging and testing is easier.</p>
<p>Functions can be categorised in two broad topics, built-in functions and user defined functions. Built-in functions are the language constructs that comes with PHP. There are hundreds of built-in functions available in PHP for our use. User-defined functions are the functions created by programmers. In this tutorial we will discuss user-defined functions in detail.</p>
<p><strong>Creating function</strong></p>
<p>Let us start with a simple function that calculates sum of two numbers.</p>
<p><strong>Example#1</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
/*
This function calculates the sum of two numbers
*/
function sum()
{
            //declare and initialize variables
            $x=20;
            $y=30;
            $z=$x+$y;
            //print the result
            echo 'The value of $x is '.$x. ' and value of $y is '. $y .' The result of  $x + $y is '.$z;
}
//Execution of this program starts from here, and calls the function sum, that means code inside the function sum will be executed
sum();
?&gt;
&lt;/body&gt;
</pre>
<p> <br />
<strong>Function with parameter</strong></p>
<p>Though we can call the function sum in example#1 as many times as we want, it produces the same result. Let us modify the above code to achieve better result.</p>
<p><strong>Example#2</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
/*
This function calculates the sum of two numbers
*/
function sum($x,$y)
{
            //Calculate result
            $result=$x+$y;
            //print the result
            echo $result;
}
//Execution starts from here
$x=20;
$y=30;
sum($x,$y);

//We can call the function using different value
$x=200;
$y=300;
sum($x,$y);
?&gt;
&lt;/body&gt;
</pre>
<p>The code in both examples provides the same functionality but the variables in example#1 are hardcoded inside the function itself and hence, can provide the functionality of adding the hardcoded values. If we have to add other numbers we have to write another function. The code in example#2 solves this problem by passing parameters and provides more functionality. We can add any two numbers any times by just calling the function with parameters. The function <strong>sum</strong> in example#2 takes two parameters. The parameters are enclosed in parenthesis and separated by comma.</p>
<p><strong>Function with parameter and return value</strong></p>
<p>A function can also return value. The return statement is optional. A function can return any type including arrays and objects. After a function returns a value, its execution is passed back to the line from which it was called. Let us modify the code of example#2 to see how a function can return value.</p>
<p><strong>Example#3</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
/*
This function calculates and returns the sum of two numbers
*/
function sum($x,$y)
{
            //Calculate result
            $result=$x+$y;
            return $result;
}
//Execution starts from here
$x=200;
$y=300;
//calculate and print the result
echo &quot;The sum is : &quot;. sum($x,$y);
?&gt;
&lt;/body&gt;
</pre>
<p>For a complete reference of functions please visit <a href="http://www.php.net/manual/en/language.functions.php">http://www.php.net/manual/en/language.functions.php</a></p>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part10' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part10'>Introduction to PHP &#8211; Part10</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part4' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part4'>Introduction to PHP &#8211; Part4</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part8/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part7</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part-7</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part-7#comments</comments>
		<pubDate>Mon, 23 Nov 2009 13:02:56 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[PHP Array]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=862</guid>
		<description><![CDATA[Array:
Array is a systematic collection of data of similar data types that can be accessed by numeric index. Array is one of the most important and oldest data structures that are used to implement other data structures like strings, maps, vector etc. An array in PHP is in fact an ordered map type that associates [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part10' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part10'>Introduction to PHP &#8211; Part10</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part2' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part2'>Introduction to PHP &#8211; Part2</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Array:</strong></h2>
<p>Array is a systematic collection of data of similar data types that can be accessed by numeric index. Array is one of the most important and oldest data structures that are used to implement other data structures like strings, maps, vector etc. An array in PHP is in fact an ordered map type that associates values to keys. We suggest readers to not get confused with map type here, simply understand PHP array as a data type that can be accessed using keys. <span id="more-862"></span></p>
<p>In a situation where it is almost impossible to create a number of variables, a single array can solve the problem. For example, we need to store marks of 100 students having Roll no 1 to 100. Instead of creating 100 variables student1, student2, &#8230;&#8230;., student100 we can create an array of size 100. After we created array, the Roll no can be used as index. More clearly, mark of student with Roll no 1 can be a stored and accessed using index 1, i.e. student[1].</p>
<p><strong>Creating Array</strong></p>
<p><strong>Syntax</strong></p>
<p>array() is used to create an array. We can specify any number of parameters of key =&gt; value pairs separated by comma.</p>
<pre>array(  <em>key1</em> =&gt;  <em>value1</em>, key2 =&gt;value 2,..............keyn =&gt; valuen)</pre>
<p>Key can only be number or string, but value can be any type, it can be an array, map, vector, object or any other PHP supported type.</p>
<p><strong>Example#1</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
/*
Output: 59 69 90 84 73 79 33 66 93 54
Average marks: 70
Marks of Roll No 5: 73
Program description: This program creates an array called student with 10 elements
and stores marks obtained by each student. The code inside the foreach loop print the marks obtained
by each student and adds to the totalMarks. After foreach loop the Average marks is calculated.
*/
$student  = array(1 =&gt; 59, 2 =&gt; 69, 3 =&gt; 90, 4 =&gt; 84, 5 =&gt; 73, 6 =&gt; 79, 7 =&gt; 33, 8 =&gt; 66, 9 =&gt; 93, 10 =&gt; 54);
/*
This array can also be defined as
$student = array(59, 69, 90, 84, 73, 79, 33, 66, 93, 54);
If you want to start index from a specific number for example say 100, you can write like
$student = array(100 =&gt; 59, 69, 90, 84, 73, 79, 33, 66, 93, 54);
*/
$totalMarks=0;
foreach($student as $marks)
{
         echo $marks.&quot; &quot;;
         $totalMarks+=$marks;
}
echo &quot;&lt;br/&gt; Average marks: &quot;. $totalMarks/10;

/*
It is also possible to access the array element using the numeric index,
for example to print the marks of student with rollno 5 can be printed as
*/
echo &quot;&lt;br/&gt; Marks of Roll No 5 : &quot;. $student[5];
?&gt;
&lt;/body&gt;
</pre>
<p>The array we used in example has a numeric index or key. We can also implement array with string key, this array is known as associative array very similar to map data structure.</p>
<p><strong>Example#2</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
/*
Output: 59 69 90 84 73 79 33 66 93 54
Average marks: 70
Marks of Sam: 69
Program description: This program creates an array called student with 10 elements
and stores marks obtained by each student. The code inside the foreach loop print the marks obtained
by each student and adds to the totalMarks. After foreach loop the Average marks is calculated.
*/

$student  = array(&quot;Hary&quot; =&gt; 59, &quot;Sam&quot; =&gt; 69, &quot;Joe&quot; =&gt; 90, &quot;Kevin&quot; =&gt; 84, &quot;Kelly&quot; =&gt; 73, &quot;Steve&quot; =&gt; 79,
 &quot;Craig&quot; =&gt; 33, &quot;Jodi&quot; =&gt; 66, &quot;John&quot;=&gt; 93, &quot;Rose&quot; =&gt; 54);
$totalMarks=0;
foreach($student as $marks)
{
            echo $marks.&quot; &quot;;
            $totalMarks+=$marks;
}
echo &quot;&lt;br/&gt; Average marks: &quot;. $totalMarks/10;

/*
It is also possible to access the array element using the string key,
for example to print the marks of &quot;Sam&quot; can be printed as
*/
echo &quot;&lt;br/&gt; Marks of Sam : &quot;. $student[&quot;Sam&quot;];
?&gt;
&lt;/body&gt;
</pre>
<p><strong>Modifying array</strong></p>
<p>To modify an existing array, we can assign values explicitly to the array. Remember that, if the specified array does not exist yet, a new array will be created. The following example shows how to modify an existing array.</p>
<p><strong>Example#3</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
/*
This program creates an array called countries with two elements
*/
$countries  = array(1 =&gt; &quot;Australia&quot;, 2 =&gt; &quot;New Zealand&quot;);
// To add a country called Japan, we need to assigh it explicitly as
$countries[3] = &quot;Japan&quot;;
// We can aslo add a element without specifying the key, in this situation
// the key of new element will he the key of last element + 1
$countries[] = &quot;United Kingdom&quot;;
 
//To simply print an array we can use the function print_r($array) as follows
// Output: Array ( [1] =&gt; Australia [2] =&gt; New Zealand [3] =&gt; Japan [4] =&gt; United Kingdom )
print_r($countries);

/* Deleting element from array:
Output:  Array ( [1] =&gt; Australia [3] =&gt; Japan [4] =&gt; United Kingdom )
Remember that the key of Japan and United Kingdom does not get changed
*/
unset($countries[2]);
print_r($countries);

// This will delete the whole array
unset($countries);
//If we try to print the array, a message like the following will be displayed
//Undefined variable: countries in C:\wamp\www\WebTest\index.php on line 29
print_r($countries);
?&gt;
&lt;/body&gt;
</pre>
<p>For a complete reference of array please visit <a href="http://www.php.net/manual/en/language.types.array.php">http://www.php.net/manual/en/language.types.array.php</a> and for complete reference on array functions please visit <a href="http://www.w3schools.com/PHP/php_ref_array.asp">http://www.w3schools.com/PHP/php_ref_array.asp</a></p>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part10' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part10'>Introduction to PHP &#8211; Part10</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part2' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part2'>Introduction to PHP &#8211; Part2</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part-7/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part6</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part-6</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part-6#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:25:43 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[PHP String]]></category>
		<category><![CDATA[php strings]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=833</guid>
		<description><![CDATA[Strings:
A string is an array of characters. This tutorial demonstrates the use of string to manipulate text. A string can be represented in one of the following four ways; single quoted, double quoted, heredoc syntax and nowdoc syntax. Before describing each string in detail, it would be appropriate to discuss about escape sequence. Escape sequence [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part2' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part2'>Introduction to PHP &#8211; Part2</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Strings:</strong></h2>
<p>A string is an array of characters. This tutorial demonstrates the use of string to manipulate text. A string can be represented in one of the following four ways; single quoted, double quoted, heredoc syntax and nowdoc syntax. Before describing each string in detail, it would be appropriate to discuss about escape sequence. Escape sequence are used to format the output text and other output options. <span id="more-833"></span>The table below presents the escape sequences for special characters.</p>
<table style="border:1px solid #CCC" border="0" cellspacing="0" cellpadding="4">
<tbody>
<tr>
<td width="170" valign="top"><strong>Sequence</strong></td>
<td width="161" valign="top"><strong>Explanation</strong></td>
</tr>
<tr>
<td width="170" valign="top">\n</td>
<td width="161" valign="top">Linefeed</td>
</tr>
<tr>
<td width="170" valign="top">\r</td>
<td width="161" valign="top">Carriage return</td>
</tr>
<tr>
<td width="170" valign="top">\t</td>
<td width="161" valign="top">Horizontal tab</td>
</tr>
<tr>
<td width="170" valign="top">\v</td>
<td width="161" valign="top">Vertical tab</td>
</tr>
<tr>
<td width="170" valign="top">\f</td>
<td width="161" valign="top">Form feed</td>
</tr>
<tr>
<td width="170" valign="top">\\</td>
<td width="161" valign="top">Backslash</td>
</tr>
<tr>
<td width="170" valign="top">\$</td>
<td width="161" valign="top">Dollar sign</td>
</tr>
<tr>
<td width="170" valign="top">\”</td>
<td width="161" valign="top">Double quote</td>
</tr>
</tbody>
</table>
<p>There are many functions available in PHP to manipulate strings. For a complete list of reference on functions please visit the link <a href="http://www.w3schools.com/PHP/php_ref_string.asp">http://www.w3schools.com/PHP/php_ref_string.asp</a></p>
<p><strong>Single quoted</strong></p>
<p>A string can be represented in single quote. In PHP a single quote string can be expanded in more than one line. Remember that, if we attempt to escape the character other than the above table, it will print the backslash too.</p>
<p><strong>Example#1</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
//Output: Hello World
$myString = 'Hello World';
echo $myString;
//or we can simply write
echo 'Hello World';

//To print string enclosed in double quotation, simply enclose it with single quotation
//Output: &quot;Hello World&quot;
$myString = '&quot;Hello World&quot;';
echo $myString;
 
//To print string that contains single quotation, simply use escape sequence &quot;\&quot; in front of single quote
//Output: &quot;Hello World, let's begin with more complex examples&quot;
$myString = '&quot;Hello World, let\'s begin with more complex examples&quot;';
echo $myString;
?&gt;
&lt;/body&gt;
</pre>
<p><strong>Double quoted</strong><br />
PHP recognized all of the above escape sequences if it is enclosed in double quotes. Remember that, as in single quoted string, if we attempt to escape the character other than the above table, it will print the backslash too.</p>
<p><strong>Example #2</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
//Output: Hellow World
//This is similar to the example presented in Example#1, except that it is enclosed by double quote
$myString = &quot;Hello World&quot;;
echo $myString;
//or we can simply write
echo &quot;Hello World&quot;;
 
//Similar to this example, \v \t \r can be implemented
//Output: Hellow World
//This will print in a new line
$myString = &quot;Hello World \n This will be printed in a new line&quot;;
echo $myString;

//This example demonstrate how variables can be expanded using double quote string
//Output: The value of $x is 10 and value of $y is 20. The result of  $x + $y is 30
$x=10;
$y=20;
$z=$x+$y;
//In the following statement, concatenation operator &quot;.&quot; is used to concatenate two strings. The variable $x in single quote is not expanded where it is expanded in double quote
echo 'The value of $x is '.&quot;$x&quot;. ' and value of $y is '. &quot;$20&quot; .'. The result of  $x + $y is '.&quot;30&quot;;
?&gt;
&lt;/body&gt;
</pre>
<p><strong>heredoc</strong></p>
<p>heredoc syntax &lt;&lt;&lt; can also be used to represent string. heredoc text behaves like double quote string, except that it is not enclosed with double quote, that means that escape sequence can be used in heredoc in the similar manner. The variables are also expanded in heredoc text.</p>
<p><strong>Example#3</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
//Output: Hellow World, This is your heredoc example, and this can be expanded as necessary
//As shown in the code, after &lt;&lt;&lt; an identifier (here ID) should be provided and then a newline should begin.
//Then the string comes and at last the same identifier is used to close the quotation.
$myString = &lt;&lt;&lt;ID
Hellow World
This is your heredoc example,
and this can be expanded as necessary
ID;
echo $myString;
?&gt;
&lt;/body&gt;
</pre>
<p><strong>nowdoc</strong></p>
<p>heredoc text behaves like double quote string while nowdoc text behaves like single quote string. The main difference is that  escape sequence cannot be used in nowdoc.</p>
<p><strong>Example#4</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
//Output: Hellow World, This is your nowdoc example, and this can be expanded as necessary
//To differenciate between heredoc and nowdoc, identifier is enclosed in single quote in nowdoc as shown
$myString  = &lt;&lt;&lt;'ID'
Hellow World
This is your nowdoc example,
and this can be expanded as necessary
ID; */
echo $myString;
?&gt;
&lt;/body&gt;
</pre>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part2' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part2'>Introduction to PHP &#8211; Part2</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part-6/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part 5</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part-5</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part-5#comments</comments>
		<pubDate>Mon, 16 Nov 2009 01:42:00 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=776</guid>
		<description><![CDATA[Loops
A loop is a programming construct that facilitates programmer to execute some code number of times or a specified condition is true. For instance, we need to read a file stored in computer and print it in screen. Suppose the function available to read a file reads one line at a time and the file [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part4' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part4'>Introduction to PHP &#8211; Part4</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Loops</strong></h2>
<p>A loop is a programming construct that facilitates programmer to execute some code number of times or a specified condition is true. For instance, we need to read a file stored in computer and print it in screen. Suppose the function available to read a file reads one line at a time and the file contains many lines, we have to write the same line of code many times to read one line at a time from the file.  By using loops we can execute the same code number of times called iterations to read the whole file, one line at a time until there is no line left in the file. <span id="more-776"></span>A loop always needs to have a terminating condition, otherwise it will loop infinitely, and we need to kill the whole process to break the loop explicitly. In the above example, the terminating condition might be end of file. Basically, there are two types of loop in programming <strong>while</strong> and <strong>for</strong>.</p>
<p>All the examples presented in this tutorial prints multiplication table of 2 using different loops. The output of all examples looks like:</p>
<p>Multiplication Table of 2<br />
2 X 1 = 2<br />
2 X 2 = 4<br />
2 X 3 = 6<br />
2 X 4 = 8<br />
2 X 5 = 10<br />
2 X 6 = 12<br />
2 X 7 = 14<br />
2 X 8 = 16<br />
2 X 9 = 18<br />
2 X 10 = 20</p>
<p><strong>while loop</strong></p>
<p><strong>while</strong> loop examines the condition first and then execute the code if the condition is true. After the statements inside the loop are executed, the condition is again checked and the loop continues until the specified condition is false.  </p>
<p>Syntax:</p>
<p>while (<em>condition</em>)<br />
{<br />
<em>          code block;</em><br />
}</p>
<p><strong>Example#1</strong></p>
<p>In this example, we created a loop that starts with x=1 and continue as long as x is less than or equal to 10. In each loop iteration, x is increased by 1. The final value of x will be 11.</p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
$x=1;
echo &quot;Multiplication Table of 2 &lt;/br&gt;&quot;;
while($x&lt;=10)
{
  	echo &quot;2 X &quot; . $x . &quot; = &quot;. 2*$x. &quot;&lt;br /&gt;&quot;;
  	$x++;
}
?&gt;
&lt;/body&gt;
</pre>
<p><strong>do while loop</strong></p>
<p><strong>do while</strong> loop execute the code first and then examines the condition. After the statements inside the loop are executed, the condition is checked and the loop continues until the specified condition is false. </p>
<p>Syntax:</p>
<p>do<br />
{<br />
<em>          code block;</em><br />
} while (<em>condition</em>);</p>
<p><strong>Example#2</strong></p>
<p>In this example, the  loop starts with x=1 and execute the first loop iteration without checking the condition. The condition is then checked when x becomes 2 and continue as long as x is less than or equal to 10. In each loop iteration, x is increased by 1. The final value of x will be 10.</p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
$x=1;
echo &quot;Multiplication Table of 2 &lt;/br&gt;&quot;;
do
{
  	echo &quot;2 X &quot; . $x . &quot; = &quot;. 2*$x. &quot;&lt;br /&gt;&quot;;
  	$x++;
} while($x&lt;=10)

?&gt;
&lt;/body&gt;
</pre>
<p> <br />
<strong>for loop:</strong></p>
<p>A <strong>for</strong> loop contains three parts, <strong>initialization</strong>, <strong>condition</strong> and <strong>increment</strong>. <strong>Initialization</strong> is used to initialize a counter for the number of loop. This expression is executed before each loop iteration. Condition expression is a terminating condition, which is checked every time the loop is executed. If the conditional expression returns false, the loop terminates. Increment is used to increment the counter. After each loop iteration, this expression is executed.  <strong> </strong></p>
<p>Syntax:</p>
<p>for(initialization; condition; increment)<br />
{<br />
<em>          code block;</em><br />
}</p>
<p><strong>Example#3</strong></p>
<p>This example demonstrate a for loop which starts from 1 to 10. Before each loop iteration, the condition is checked to see if the value of x is less than or equall to 10. In each loop iteration, x is increased by 1. Can you check, what will be the final value of x?</p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
echo &quot;Multiplication Table of 2 &lt;/br&gt;&quot;;
for($x=1; $x&lt;=10; $x++)
{
  	echo &quot;2 X &quot; . $x . &quot; = &quot;. 2*$x. &quot;&lt;br /&gt;&quot;;
}
?&gt;
&lt;/body&gt;
</pre>
<p><strong>foreach loop:</strong></p>
<p>Array is a systematic collection of data of similar data types. We will discuss array in detail in later tutorials. <strong>foreach</strong> loop is used to loop throughout the array</p>
<p>Syntax:</p>
<p>foreach($array as $value)<br />
{<br />
<em>          code block;</em><br />
}</p>
<p><strong>Example#4</strong></p>
<p>This example creates a array containing 10 numbers from 1 to 10. The loop is used to access the elements of the array. In each iteration, the current array element is assigned to the variable $value.</p>
<pre class="brush: php;">
&lt;html&gt;
&lt;body&gt;
&lt;?php
$numberArray = array(1,2,3,4,5,6,7,8,9,10);
echo &quot;Multiplication Table of 2 &lt;/br&gt;&quot;;
foreach($numberArray as $x)
{
  echo &quot;2 X &quot; . $x . &quot; = &quot;. 2*$x. &quot;&lt;br /&gt;&quot;;
}
?&gt;
&lt;/body&gt;
</pre>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part4' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part4'>Introduction to PHP &#8211; Part4</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-7' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part7'>Introduction to PHP &#8211; Part7</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part-5/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part4</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part4</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part4#comments</comments>
		<pubDate>Wed, 11 Nov 2009 04:19:01 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=719</guid>
		<description><![CDATA[Control Statements
A program is a collection of modules working together to perform certain task. The modules can be thought independently which always produces the same set of output for a given set of input. Control statements control the execution of a program by executing those modules once at a time in a meaningful order,  known [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-6' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part6'>Introduction to PHP &#8211; Part6</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Control Statements</strong></h2>
<p>A program is a collection of modules working together to perform certain task. The modules can be thought independently which always produces the same set of output for a given set of input. Control statements control the execution of a program by executing those modules once at a time in a meaningful order,  known as flow of control. Basically, there are two types of control statements, branching statements and loops. In this tutorial, we will discuss branching statements in detail. <span id="more-719"></span></p>
<p><strong>Branching statements</strong></p>
<p>As the name suggest, the control of execution is passed to some code block among many code blocks depending upon the conditional statement. There are two branching statements available in PHP, Conditional Statement and Switch.</p>
<p><strong>Conditional Statements</strong></p>
<p>A conditional statement is used to decide which code block will be executed. Let us take an example of examining a number and printing it in word form, the conditional statement can be one of the following, depending upon the context.</p>
<p><strong>if statement</strong>:  This statement is used to execute some code if a specified condition is true and does not care if the condition is false. In a situation where we need to print &#8220;One&#8221; if the value of x is 1, the code looks like</p>
<p><strong>Example#1</strong></p>
<pre class="brush: php;">
$x=1;
if ($x==1)
       echo &amp;quot;One&amp;quot;;
</pre>
<p>The output of example#1 will be “One”.</p>
<p><strong>if&#8230;else statement</strong>: This statement is used to execute some code if a specified condition is true and another code if the condition is false. Let us modify the situation of example#1 by adding a statement that is executed if  the value of x is not 1, assume that it is an error.</p>
<p><strong>Example#2</strong></p>
<pre class="brush: php;">
$x=0;
if ($x==1)
       echo &amp;quot;One&amp;quot;;
else
       echo &amp;quot;Error...&amp;quot;;
</pre>
<p> </p>
<p> The output of example#2 will be &#8220;Error&#8230;”.</p>
<p><strong>if&#8230;elseif&#8230;.else statement</strong>: This statement is used to select one of several blocks of code to be executed. Let us suppose another situation where we need to print &#8220;One&#8221; if the value of x is 1, &#8220;Two&#8221; if the value of x is 2, &#8220;Three&#8221; if the value x is 3 and error in other cases; the code looks like</p>
<p><strong>Example#3</strong></p>
<pre class="brush: php;">
$x=3;
if ($x==1)
       echo &amp;quot;One&amp;quot;;
else if($x==2)
       echo &amp;quot;Two&amp;quot;;
else if($x==3)
       echo &amp;quot;Three&amp;quot;;
else
       echo &amp;quot;Error...&amp;quot;;
</pre>
<p>The output of example#3 will be “Three”.</p>
<p><strong>Switch</strong></p>
<p>Switch is functionally similar to<strong> if&#8230;elseif&#8230;else</strong> statement. In Switch, the conditional expressions in<strong> if&#8230;elseif</strong> statements are replaced by <strong>case</strong> and <strong>else</strong> is replaced by <strong>default</strong>. One more thing to understand in <strong>Switch</strong> is <strong>break</strong>. In example#4, the execution begins from case 1, the value of x is 2 and hence, it will not enter into case 1. It will enter into case 2 and will display the word “Two” and then switch execution will be break by using <strong>break</strong> statement. But if we don’t use <strong>break</strong> statement it will print “TwoThreeError&#8230;”. That means after a certain condition is satisfied, if we don’t break the execution, the <strong>Switch</strong> will continue executing the other cases even though the condition is not satisfied.</p>
<p><strong>Example#4</strong></p>
<pre class="brush: php;">
$x=2;
switch($x)
{
case 1:
       echo &amp;quot;One&amp;quot;;
       break;
case 2:
       echo &amp;quot;Two&amp;quot;;
       break;
case 3:
       echo &amp;quot;Three&amp;quot;;
       break;
default:
       echo &amp;quot;Error...&amp;quot;;
}
</pre>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-6' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part6'>Introduction to PHP &#8211; Part6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part4/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP – Part 3</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part-3</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part-3#comments</comments>
		<pubDate>Sat, 07 Nov 2009 09:10:03 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=683</guid>
		<description><![CDATA[Operators
An operator is a type of function which acts on operands often called inputs and produce results. In PHP, there are three types of operators. Increment operator ++ and decrement operator &#8212; belongs to the first operator group called unary operator. Unary operator always acts on single operand. The second group called the binary operator [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part2' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part2'>Introduction to PHP &#8211; Part2</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2><strong>Operators</strong></h2>
<p>An operator is a type of function which acts on operands often called inputs and produce results. In PHP, there are three types of operators. Increment operator ++ and decrement operator &#8212; belongs to the first operator group called unary operator. Unary operator always acts on single operand. The second group called the binary operator acts on more than two operands. .<span id="more-683"></span>The group is the ternary operator : ? :. This operator is used to select between two expressions depending upon the third one. These operators can be classified in the following ways based on the type of functionality they provides.</p>
<p><strong>Arithmetic Operators</strong></p>
<table style="border: 1px solid #ccc" border="0" cellspacing="0" cellpadding="5" width="90%">
<tbody>
<tr>
<td width="15%"><strong>Operator</strong></td>
<td width="21%"><strong>Operation</strong></td>
<td width="29%"><strong>Example</strong></td>
<td width="33%"><strong>Result</strong></td>
</tr>
<tr>
<td valign="top">++</td>
<td width="21%" valign="top">Increment</td>
<td width="29%" valign="top">x=6<br />
x++Here, x is called operand</td>
<td width="33%" valign="top">x=7</td>
</tr>
<tr>
<td valign="top">&#8211;</td>
<td width="21%" valign="top">Decrement</td>
<td width="29%" valign="top">x=2<br />
x&#8211;</td>
<td width="33%" valign="top">x=1</td>
</tr>
<tr>
<td valign="top">+</td>
<td width="21%" valign="top">Addition</td>
<td width="29%" valign="top">1+1</td>
<td width="33%" valign="top">2</td>
</tr>
<tr>
<td valign="top">-</td>
<td width="21%" valign="top">Subtraction</td>
<td width="29%" valign="top">2-1</td>
<td width="33%" valign="top">1</td>
</tr>
<tr>
<td valign="top">*</td>
<td width="21%" valign="top">Multiplication</td>
<td width="29%" valign="top">2*3</td>
<td width="33%" valign="top">6</td>
</tr>
<tr>
<td valign="top">/</td>
<td width="21%" valign="top">Division</td>
<td width="29%" valign="top">3/2</td>
<td width="33%" valign="top">1.5</td>
</tr>
<tr>
<td valign="top">%</td>
<td width="21%" valign="top">Modulus</td>
<td width="29%" valign="top">3%2<br />
5%3<br />
6%2</td>
<td width="33%" valign="top">1<br />
2<br />
0 We can notice that result is division remainder</td>
</tr>
</tbody>
</table>
<p><strong>Assignment Operators</strong></p>
<table style="border: 1px solid #ccc" border="0" cellspacing="0" cellpadding="5" width="90%">
<tbody>
<tr>
<td width="16%"><strong>Operator</strong></td>
<td width="17%"><strong>Example </strong></td>
<td width="66%"><strong>Assuming x=10 and y=5</strong></td>
</tr>
<tr>
<td valign="top">=</td>
<td width="17%" valign="top">x=y</td>
<td width="66%" valign="top">x will be 5</td>
</tr>
<tr>
<td valign="top">+=</td>
<td width="17%" valign="top">x+=y</td>
<td width="66%" valign="top">Equivalent to x=x+y, hence x will be 15</td>
</tr>
<tr>
<td valign="top">-=</td>
<td width="17%" valign="top">x-=y</td>
<td width="66%" valign="top">Equivalent to x=x-y, hence x will be 5</td>
</tr>
<tr>
<td valign="top">*=</td>
<td width="17%" valign="top">x*=y</td>
<td width="66%" valign="top">Equivalent to x=x*y, hence x will be 50</td>
</tr>
<tr>
<td valign="top">/=</td>
<td width="17%" valign="top">x/=y</td>
<td width="66%" valign="top">Equivalent to x=x/y, hence x will be 2</td>
</tr>
<tr>
<td valign="top">.=</td>
<td width="17%" valign="top">x.=y</td>
<td width="66%" valign="top">Equivalent to x=x.y, hence x will be 105. More clearly, if x=23 and y =11, then x will be 2311</td>
</tr>
<tr>
<td valign="top">%=</td>
<td width="17%" valign="top">x%=y</td>
<td width="66%" valign="top">Equivalent to x=x%y, hence x will be 0</td>
</tr>
</tbody>
</table>
<p><strong>Comparison Operators</strong></p>
<table style="border: 1px solid #ccc" border="0" cellspacing="0" cellpadding="5" width="90%">
<tbody>
<tr>
<td width="16%"><strong>Operator</strong></td>
<td width="27%"><strong>Operation</strong></td>
<td width="26%"><strong>Returns TRUE if</strong></td>
<td width="29%"><strong>Returns FALSE if</strong></td>
</tr>
<tr>
<td valign="top">==</td>
<td width="27%" valign="top">is equal to</td>
<td width="26%" valign="top">2==2</td>
<td width="29%" valign="top">1==2</td>
</tr>
<tr>
<td valign="top">!=</td>
<td width="27%" valign="top">is not equal</td>
<td width="26%" valign="top">1!=2</td>
<td width="29%" valign="top">2!=2</td>
</tr>
<tr>
<td valign="top">&gt; </td>
<td width="27%" valign="top">is greater than</td>
<td width="26%" valign="top">2&gt;1</td>
<td width="29%" valign="top">1&gt;2</td>
</tr>
<tr>
<td valign="top">&lt; </td>
<td width="27%" valign="top">is less than</td>
<td width="26%" valign="top">1&lt;2</td>
<td width="29%" valign="top">2&lt;1</td>
</tr>
<tr>
<td valign="top">&gt;=</td>
<td width="27%" valign="top">is greater than or equal to</td>
<td width="26%" valign="top">2&gt;=1, 2&gt;=2</td>
<td width="29%" valign="top">1&gt;=2</td>
</tr>
<tr>
<td valign="top">&lt;=</td>
<td width="27%" valign="top">is less than or equal to</td>
<td width="26%" valign="top">1&lt;=2, 2&lt;=2</td>
<td width="29%" valign="top">2&lt;=1</td>
</tr>
</tbody>
</table>
<p><strong>Logical Operators</strong></p>
<table style="border: 1px solid #ccc" border="0" cellspacing="0" cellpadding="5" width="90%">
<tbody>
<tr>
<td width="15%"><strong>Operator</strong></td>
<td width="28%"><strong>Operation</strong></td>
<td width="26%"><strong>Returns TRUE if</strong></td>
<td width="29%"><strong>Returns FALSE if</strong></td>
</tr>
<tr>
<td width="15%" valign="top">&amp;&amp;</td>
<td width="28%" valign="top">ANDReturns true only if the both expressions are true</td>
<td width="26%" valign="top">(2==2 &amp;&amp; 1&lt;2)</td>
<td width="29%" valign="top">(2==2 &amp;&amp; 1&gt;2)</td>
</tr>
<tr>
<td width="15%" valign="top">||</td>
<td width="28%" valign="top">ORReturns true if one of the expressions is true</td>
<td width="26%" valign="top">(2==2 || 1&gt;2)</td>
<td width="29%" valign="top">(2!=2 ||1&gt;2)</td>
</tr>
<tr>
<td width="15%" valign="top">!</td>
<td width="28%" valign="top">NOTComplement the result of an expression</td>
<td width="26%" valign="top">!(2==3)</td>
<td width="29%" valign="top">!(2==2)</td>
</tr>
</tbody>
</table>
<p><strong>Operators Precedence and Associativity</strong></p>
<p>The precedence of an operator specifies which operation is carried first. For example, the result of 2+5*2 is 12 not 14. Here multiplication is evaluated first followed by addition, hence, we say that operator “*” has higher precedence than operator “+”. In some cases parenthesis is used to force the precedence. For instance, the result from same expression with parenthesis like (2+5)*2 will produce result 14.</p>
<p>Associativity decides the order of evaluation of an expression if the operators have equal precedence. If a expression is evaluated from right we called right associativity and for left we say left associativity. The following table shows the precedence of operators having highest precedence. Operators having same associativity are listed in the same line. </p>
<table border="0" cellpadding="0">
<thead>
<tr>
<td><strong>Operators</strong></td>
</tr>
</thead>
<tbody>
<tr>
<td>++ &#8211;</td>
</tr>
<tr>
<td>!</td>
</tr>
<tr>
<td>* / %</td>
</tr>
<tr>
<td>+ -</td>
</tr>
<tr>
<td>&lt;&lt; &gt;&gt;</td>
</tr>
<tr>
<td>&lt; &lt;= &gt; &gt;=</td>
</tr>
</tbody>
</table>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part8' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part8'>Introduction to PHP &#8211; Part8</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part2' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part2'>Introduction to PHP &#8211; Part2</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part-3/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part2</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part2</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part2#comments</comments>
		<pubDate>Thu, 05 Nov 2009 13:10:29 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=647</guid>
		<description><![CDATA[Data Types:
PHP supports eight primitive types; four scalar types two compound and two special types. In this section we will discuss scalar types; boolean, integer, float and string. Other four array, object, resource and null will be discussed in the later sections.
Boolean:
 To define boolean variable we can specify TRUE or FALSE, both are case insensitive. [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-6' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part6'>Introduction to PHP &#8211; Part6</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>Data Types:</strong></p>
<p>PHP supports eight primitive types; four scalar types two compound and two special types. In this section we will discuss scalar types; boolean, integer, float and string. Other four array, object, resource and null will be discussed in the later sections.</p>
<p><strong>Boolean:</strong></p>
<p> To define boolean variable we can specify TRUE or FALSE, both are case insensitive. The size of the Boolean variable is one byte.<span id="more-647"></span></p>
<p><strong>Example# 1:</strong></p>
<pre class="brush: php;">
$flag = True; // assign the value TRUE to $flag
Echo $flag; // Output will be 1, which means TRUE
</pre>
<p><strong>Integer: </strong></p>
<p>According to PHP manual an integer is a number of set Z = {&#8230;, -2, -1, 0, 1, 2, &#8230;}. In general integer is a positive or negative number without decimal point. Integer can be specified in decimal, hexadecimal or octal notation. The sign (+ or -) is optional. If the integer is precede by number 0(zero), it is octal notation and 0X is hexadecimal notation. The size of integer is platform dependent and maximum size is four bytes. If a number greater than integer range is encountered, PHP interpret it as float.</p>
<p><strong>Example# 2 </strong></p>
<pre class="brush: php;">
$decimal = 4532; // positive decimal number
$negatige = -320; // negative decimal number
$Octal = 770; // octal number (equivalent to 504 decimal)
$Hex = 0x3F; // hexadecimal number (equivalent to 63 decimal)
</pre>
<p>Note that there is no integer division is available in PHP, the result is always float.</p>
<p><strong>Float:</strong></p>
<p>Floating point numbers are the real number with decimal point. They are also known as doubles.  The size of float is platform dependent, the maximum size is eight bytes with a precision of roughly 14 decimal digits.</p>
<p><strong>Example# 3</strong></p>
<pre class="brush: php;">
$x = 5.362; // floating point number
$y = 2.3e2; // can also be expressed in e notation
$z = 2E-10; // another form of float representation
</pre>
<p><strong>String: </strong></p>
<p>A string variable is used to store array of characters or character strings. For instance, if we need to store characters “ABCD” in a variable, string is required. A string can also contain numbers, space and special characters and should enclose by quote. PHP supports a string of any size, the limit depends upon the memory of the computer on which PHP is running.</p>
<p>The following example creates a string variable called $myString, assign string “Hello World” to the string and finally prints $myString to the monitor.</p>
<p>Hello World</p>
<p><strong>Example# 4</strong></p>
<pre class="brush: php;">
$myString=&amp;amp;amp;quot;Hello World&amp;amp;amp;quot;;
echo $ myString;
</pre>
<p>The output of the above code will be</p>
<p>Hello World</p>
<p>A string can be specified in four ways.</p>
<ul>
<li>Single quoted</li>
<li>Double quoted</li>
<li>heredoc syntax</li>
<li>now doc syntax</li>
</ul>
<p>We will discuss string in detail in following tutorials.</p>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part-6' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part6'>Introduction to PHP &#8211; Part6</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-1' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 1'>Introduction to PHP &#8211; Part 1</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part2/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Introduction to PHP &#8211; Part 1</title>
		<link>http://www.webanddesigners.com/introduction-to-php-part-1</link>
		<comments>http://www.webanddesigners.com/introduction-to-php-part-1#comments</comments>
		<pubDate>Tue, 03 Nov 2009 13:41:49 +0000</pubDate>
		<dc:creator>Bhoj R Dhakal</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginners]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.webanddesigners.com/?p=613</guid>
		<description><![CDATA[Introduction:
PHP is a widely used open source server side scripting language that is used to create dynamic and interactive web pages. PHP can be directly embedded into the HTML code and hence, is perfect for any web development. The syntax of PHP is very similar to Perl and C. PHP works on Apache web server [...]


Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part9' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part9'>Introduction to PHP &#8211; Part9</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-6' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part6'>Introduction to PHP &#8211; Part6</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Introduction:</h2>
<p>PHP is a widely used open source server side scripting language that is used to create dynamic and interactive web pages. PHP can be directly embedded into the HTML code and hence, is perfect for any web development. The syntax of PHP is very similar to Perl and C. PHP works on Apache web server and also supports Microsoft Internet Information Server.</p>
<h3>Installing PHP:</h3>
<p>If you want to use PHP for website and web applications you need three things; PHP itself, a web server and web browser. To design and develop web pages you need a web development application. In addition, if you want to create dynamic web pages, you might also need database functionalities.</p>
<p>I suggest you to install WampServer and Macromedia Dreamweaver. WampServer is a Windows web development environment that comes with Apache webserver, PHP and MySQL database. You can download WampServer for free using this link http://www.wampserver.com/en/download.php. For more information on how to install WampServer visit this link, <a href="http://www.wampserver.com/en/presentation.php" target="_blank">http://www.wampserver.com/en/presentation.php</a>.</p>
<h3>Basic Syntax:</h3>
<p>PHP block starts with <strong> </strong> and can be placed anywhere in HTML code. Each line of code ends with semicolon. Semicolon is used to separate one set of instructions from another.  The example below is a simple PHP script which sends the message “Hello World” to the browser.</p>
<p>Create a file and save it with name example1.php and paste the following code. This code is extremely simple, contains HTML tags and a block of PHP script. The highlighted portion of the code is PHP code and is embedded into HTML. The whole file needs to be saved with .php extension, if the file has other than .php extension like .html, the PHP script will not execute.</p>
<p>The file can be accessed using browser with web server&#8217;s URL, ending with the /example1.php file reference. For example if you are developing locally, the URL will look like <a href="#">http://localhost/example1.php</a>.</p>
<p><strong>Example# 1:</strong></p>
<pre class="brush: php;">

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;First Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
  echo &quot;Hello World&quot;;
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>For comments,  // or # is used for single line comment and /* and */ is used for multi line comments. Now amend the file as follows. The output will remains the same, why?.</p>
<p><strong>Example# 2:</strong></p>
<pre class="brush: php;">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Example with comments&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
//Single line comment or
#Single line comment
echo&quot;Hello World&quot;;
/*
    Multi line comment
    Line 1
    Line 2 ...
*/
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><br/></p>
<h3>Defining Variables:</h3>
<p>Normally, most of the programming languages are strongly typed and require the data type while declaring variables but PHP does not need to declare data type before being set. PHP automatically declare variable and the type of variable is determined at the run time depending on the context in which that variable is used. In PHP, a variable is identified by placing a $ in front of variable name. Variable name are case sensitive.<br/><br/><br />
<strong>Example# 3:</strong></p>
<pre class="brush: php;">
&lt;?php
    $x = 10;
    $y=20;
	$z = $x + $y; // $z=$X+$Y does not produce result 30, because variable names are case sensitive.
    echo &quot;The result is: &quot;. $z; // Output will be 30
    $name ='John';
    $Name= &quot;Mary&quot;;
    echo &quot;$name, $Name&quot;; // The output will be John, Mary
?&gt;</pre>
<p>In the above example $ sign before x, y and z specifies that they are variables. The types of the variables are determined when they are being assigned. 10 determine the type of variable x, 20 determine the type of y and the result of x+y determines the type of z. We will discuss more about the types, assignment and predefined variables in the following tutorials.<br />
<br/></p>
<h3>Naming rules:</h3>
<ul>
<li>A variable name must begin with a letter or an underscore &#8220;-&#8221;.</li>
<li>A variable name can only contain characters, numbers and underscore (a-z, A-Z, 0-9 and _).</li>
<li>A variable name should not contain any special characters and space. </li>
</ul>


<p>Related posts:<ol><li><a href='http://www.webanddesigners.com/introduction-to-php-part9' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part9'>Introduction to PHP &#8211; Part9</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-5' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part 5'>Introduction to PHP &#8211; Part 5</a></li>
<li><a href='http://www.webanddesigners.com/introduction-to-php-part-6' rel='bookmark' title='Permanent Link: Introduction to PHP &#8211; Part6'>Introduction to PHP &#8211; Part6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.webanddesigners.com/introduction-to-php-part-1/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>
