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.

<?php
$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.

<?php
$a = 10;
$b = 5;
$c = $a + $b;
 if ($c == 20) {
   echo "The sum is equal to 20.";
 else{
     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.

<?php
$playing = "Chess";
if ($playing == "Cricket") {
  echo "Rishi is playing cricket.";
}else if ($playing == "Chess"){
  echo "Rishi is playing Chess.";
}else{
  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.

<?php
$game = "Chess";
switch($game){
    case "Cricket";
       echo "Rishi is playing Cricket";
       break;
    case "Football";
       echo "Rishi is paying Foot Ball";
       break;
    case "Chess";
       echo "Rishi is playing Chess";
       break;
}
?>

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
<?php
$a = 8;
$b = $a++;
echo $b; //Returns the value 8.
echo $a; //Returns the value 9.
?>
Post Decrementing Operator
<?php
$a = 10;
$b = $a--;
echo $b; // Returns the value 5.
echo $a; // Returns the value 4.
?>
Pre Incrementing Operator
<?php
$a = 8;
$b = ++$a;
echo $b; // Returns the value 9.
echo $a; // Returns the value 9.
?>
Pre Decrementing Operator
<?php
$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
<?php
   $a = "My name is ";
   $b .= "Rishi";
   echo $b;

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

Concatenating Assignment operator
<?php
  $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.

<?php
  $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" />
</form>

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" />
</form>

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" />
</form>

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" />
</form>

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" 
type="application/x-shockwave-flash" 
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" 
pluginspage="http://www.apple.com/quicktime/">

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>
</object>

Play MPEG Video in HTML Page

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

Play Youtube Video in HTML Page

<iframe width="420" height="315" 
src="http://www.youtube.com/embed/p9KjdYJ_gcc" 
frameborder="0" allowfullscreen></iframe>
<object width="420" height="315">
<param name="movie" 
value="http://www.youtube.com/v/p9KjdYJ_gcc?version=3&amp;hl=en_US">
</param>
<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>
</object>

 

Web Application Development

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

EXPERIENCE AND RESEARCH
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.