Tag Archives: Technical Expertise

PHP Control Structures

Introduction to Control Structures

When you start writing programs in PHP, you will realize the importance of using control structures. They are use to control how a structure will be executed, adding useful functionality to the PHP code and making it more flexible.

Condition and Statements

Control structure are usually based on condition and therefore always have condition associated with them .They check the status of these conditions ,which can be either true or false .A statement can be used with a control structure to tell the program what to do depending on the result of the condition specified. Consider the fallowing example to understand how a condition differs from a statement. If Rishi promises that he won’t neglect his studies, buy a Tab for him. This sentence can be broken intotwo parts, a condition and a statement. The first part of sentence, If Rishi promises that he won’t neglect his studies, is a condition. The status of the condition will decide whether Rishi  will get the Tab or not. If the condition is true , Rishi will get the Tab.

AS the name suggest, conditioal statement use to check for condition and execute code on the basis of the value returned. This section discuss if, else, elseif and switch conditional statements.

The if statement

We discussed the if statement briefly in the previous chapter; this section contains a detailedexplanation of it .The if statement is one of the most commonly use statement in PHP or any programming language.

Allows conditional execution.

The if statement can check for a particular condition and also perform a specific action depending on whether the condition is true or false.

In capable of containing multiple code blocks. You can set more than one operation to be performed depending on the result of the condition specified in the PHP code.

Can also be nested within an existing if statement. You can use another If statement within an exciting one to check another condition within an exciting condition.

$a = 5;
$b = 7;
$c = $a+$b;
  if ($c > 9){ 
    echo "The sum is greater than 9.";

The else statement

The else statement as discussed in the previous chapter is always used with the if statement. When you use the if statement you check condition and specify a statement to be executed when the condition is true. The else statement is used to specified a statement in case the result of the condition is false. The uses of else statement is similar to that of the if statement. The else statement is also used to check the alternative block of code.

$a = 10;
$b = 5;
$c = $a + $b;
 if ($c == 20) {
   echo "The sum is equal to 20.";
     echo "The sum is not equal to 20.";

The elseif statement

The elseif statement, as the name suggest, is a combination of the else and if  statements. you can check for multiple conditions by using the elseif statement. you can also have multiple elseif statement under an if statement. An else statement is executed only when the previous if statementreturns a false value. You can write both else if or elseif, with space and without space both are correct.

$playing = "Chess";
if ($playing == "Cricket") {
  echo "Rishi is playing cricket.";
}else if ($playing == "Chess"){
  echo "Rishi is playing Chess.";
  echo "Rishi is not playing.";

The switch statement

The switch statement can be used in placement of the if statement. Similar to the if statement, the switch statement is also used to change the flow of a program depending on the evolution of an expression. Although the if statement is also used to achieve the same result, there are certain differences between the if and switch statement:

– The else if statement can be used with the if statement to evaluate multiple conditions and expressions. In contrast switch evaluates only one expressions at a time and executes the corresponding code specified for the expressions.

– The result generated as part of the if statement returns a Boolean value, which is either true or false, but in the case of switch ,the result of a condition can be evaluated against any number of values.

$game = "Chess";
    case "Cricket";
       echo "Rishi is playing Cricket";
    case "Football";
       echo "Rishi is paying Foot Ball";
    case "Chess";
       echo "Rishi is playing Chess";

In this code notice that
– Multiple case statement have been used with respect to the switch statement.
– Each case statement ends with a colon.
– A break statement is include in each case statement.

The output of the code is: Rishi is playing Chess

PHP Increment/decrement Operator

Incrementing and Decrementing Operators

There are two types of incrementing and decrementing operators, post incrementing/decrementing operators and pre incrementing /decrementing operators. Post incrementing operators are placed after a variable name, and pre incrementing /decrementing operators are placed before the name.

Operator Usage Output
Post incrementing $a++; Returns the value of $a and then adds 1 to the value
Post decrementing $a–; (2 minus signs) Returns the value of $a and then subtract 1 from the value
Pre incrementing ++$a; Adds 1 to the value of $a and then returns the new value
Pre decrementing –$a; (2 minus signs) Subtract 1 from the value of $a and returns the new value

Consider the fallowing code for a better understanding of incrementing and decrementing operators. This code shows how the post incrementing operators works.

Post Incrementing Operator
$a = 8;
$b = $a++;
echo $b; //Returns the value 8.
echo $a; //Returns the value 9.
Post Decrementing Operator
$a = 10;
$b = $a--;
echo $b; // Returns the value 5.
echo $a; // Returns the value 4.
Pre Incrementing Operator
$a = 8;
$b = ++$a;
echo $b; // Returns the value 9.
echo $a; // Returns the value 9.
Pre Decrementing Operator
$a = 10;
$b = --$a;
echo $b; // Returns the value 9.
echo $a; // returns the value 9.

String Operator

When you start writing PHP code, you will realize the importance of string operators. PHP uses twokinds of string operators, concatenation (.) the dot sign and concatenating assignment (.=) the dot and equal to sign.The concatenation operator is used when you want to combine the left and right argument between which the operator is placed. The concatenating assignment operator is a combination of the concatenation operators and the assignment operator.

Concatenation Operator
   $a = "My name is ";
   $b .= "Rishi";
   echo $b;

The output of the code is: My name is Rishi.

Concatenating Assignment operator
  $a = "My name is";r/>  $a.= "Rishi";
  echo $a;

The output of the code is: My name is Rishi.

Logical Operators

The Logical operators are used in PHP to determine the status of condition . When you use if…else or while statements, logical operator execute code based on the status of a condition .In this section, we will discuss only two logical operators:

The and operate (&&) is used when a statement has two condition to be checked. If both the condition are true , the statement is true,.

The or operator ( || ) is also used when a statement has two condition is true, the statement is true.

  $a = 10;
  $b = 15;
  $c = $a + $b;
  if( ($a > 5) && $c < 20 ){
    echo "Value of c is less than 20";
  }else {
    echo "Value of c is more than 20"; 

The Output of the above code is: Value of c is more than 20

HTML Forms

HTML forms are used to send information to server. You can use input elements like text fields, radio buttons, check box, dropdown etc. to capture information. You can use multiple forms in a single HTML page, but you can’t use one form inside another form.

<form action="contact.php"></form>

Form and the Attribute:

Action: This attribute is used to send the information to the specified page. In the following page the value of all input filels will send to contact.php page.

<form action="contact.php"></form>

Autocomplete: As you start type in a input fields your browser history shows the previously enteredvalue, to avoid that you can set autocomplete off.

<form action="contact.php" autocomplete="off"></form>

Method: Method is use to set the send method of the form. It can either be GET or POST. To know more please click here.

<form action="contact.php" method="post"></form>

Name: Name attribute is used to assign a name to the form. It may required in JavaScript.

<form action="contact.php" method="post" name="myForm"></form>

HTML Forms Input Element

The Input elements are used to capture the user information.

<form action="contact.php" method="post" name="myForm">
<input type="text" />

Input and the Attribute:

Type: This attribute is use to define what type of element you want to display. It can be button, checkbox, file, hidden, image, password, radio, reset, submit or text.

<form action="contact.php" method="post" name="myForm">
<input type="text" />

Name: This is used to assign a name to the input fiel. Plese use unique name to each element in a form.

<form action="contact.php" method="post" name="myForm">
<input type="text" />

Value: This attribute is use to set the default value of an element.

<form action="contact.php" method="post" name="myForm">
<input type="text" value="Enter Name" />

You can also set the height, width of an text input field. And also assign calss and id to any input field.

HTML Video

Play Video in HTML Page

You can add audio or video to your html page. Either you can add video from www.youtube.com or you can add you own video by uploading the same to your own web server. But it is a bit tricky to show videos in your web page, and it is a little complicated to make it works in all browser.

Browser Supported Video Formats:

File Format
.swf or .flv Flash
.mov QuickTime
.rm or .ram Real Video
.avi AVI
.wmv WMV
.mpg or .mpeg MPEG
.mp4 Mpeg-4

Play Flash File in HTML Page

<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" 
id="obj1" border="0" width="160" height="160">
<param name="movie" value="myfile.swf">
<param name="quality" value="High">
<param name="wmode" value="transparent">
<embed src="myfile.swf" 
name="obj1" width="160" height="160" 
quality="High" wmode="transparent"></object>

Play QuickTime in HTML Page

<embed src="mymovie.mov" width="320" height="252" 
autoplay="true" controller="true" loop="false" 

Play RealVideo in HTML Page

<object id="id1" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" 
height="240" width="320">
<param name="controls" value="imagewindow">
<param name="autostart" value="true">
<param name="src" value="myvideo.rm">
<embed height="240" width="320" controls="imagewindow" src="myvideo.rm" 
type="audio/x-pn-realaudio-plugin" autostart=true>

Play MPEG Video in HTML Page

<embed src="myfile.mpeg" autostart="false" />

Play Youtube Video in HTML Page

<iframe width="420" height="315" 
frameborder="0" allowfullscreen></iframe>
<object width="420" height="315">
<param name="movie" 
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/p9KjdYJ_gcc?version=3&amp;hl=en_US" 
type="application/x-shockwave-flash" width="420" height="315" 
allowscriptaccess="always" allowfullscreen="true"></embed>


Web Application Development

“Internationally, IWCn has leading expertise in Usability and User Interface Design.”

IWCn has been working in the field of custom software development since 2009. Our expert team has taken part in dozens of application development projects. We are experienced in developing advanced systems with complex business logic dealing with large amounts of data and transactions. We are able to supply you with an innovative, trustworthy software solution to complement your most complicated business ideas.