Introduction to PHP:

  • PHP means PHP Hypertext Preprocessor(P=PHP, H=HYPERTEXT, P=PREPROCESSOR).
  • PHP is a sever side scripting language mainly used for developing web applications. PHP is a programming language that can do all sorts of things: evaluate form data sent from a browser,
    build custom web content to serve the browser, access  to a database, and even send and receive cookies (little packets of data that your browser uses to remember things).
  • Through out the world 82% of the webpages we are developing using PHP.
  • PHP is a open source software, no need to purchase license(for example if we want to use .NET , we have to purchase .NET from Microsoft for 1.4lakhs for each system and works only in windows.)
  • PHP supports all Data Base’s MYSQL, MYSQLI, MYSQL LITE, ORACLE etc.
  • PHP code can be written right into your HTML like in below images,php
  • But our browser knows only HTML. So to convert PHP into HTML we are using WAMP Server. WAMP server is a windows web development environment. It allows you to create web applications with Apache ,PHP and the MySQL database.
  • For Windows we use WAMP Server, for LINUX we use LAMP, and for MAC we use MAMP.
  • XAMP it supports all operating systems.

Installation of  WAMP server:

Open www.wampserver.com/en
wamp
Click on download, then download wampserver 32bit (depending upon your system type)

wamp2
Double click on setup file and follow the instructions to install the pack.
Note: Once if your installation is finished you can observe a folder in
local disk-C (operating system disk) called “wamp”. For example you can observe wamp folder in my system in below picture.
wamp3
Now open that folder you will find a folder with name “www”.

wamp4
we are saving all our coded files in this folder only.
Now double click on wampserver icon on desktop to open wampserver.
Now you can see a wampserver icon on task bar.

wamp5
Its in red color isn’t it? wait until it turns into green.

wamp6

wamp7

To see output:
For seeing output start wampserver. Click on Yes.
Now left click on wampserver icon then click on local host.

wamp8
Now a browser window opens as seen in below picture

wamp9
Type your file path and press enter you can see list of files with ” .php”  extension. select any file to see out put.
wamp10

PHP Syntax:

PHP code is written in between two tags, <?PHP (opening tag) and   ?> (closing tag). whatever you write just write in between those two tags, then only it is treated as PHP programming.
Whenever compiler sees the PHP tags it starts executing the PHP code otherwise it won’t.
In the below picture we can see a basic PHP program.

basic

The above code is a basic PHP code.

Echo:
We use to write programs to output on a screen. For example in C-language we use “Print f ” to display output or our result.

Similarly in PHP we use “echo” to display our result.
Example:If we write like this
<?php
echo “hello”;
?>
PHP will output hello.

Note: Make sure to end each line of php with a semicolon.
semicolon represents end of that line. If you wont give semicolon it throws an error.

PHP can also do maths.
<?php
echo 5 * 7;
?>
here we use echo to display the result of multiplication of 5 and 7.

Variables:
In order to do different operations with the data, we need to save the data. In PHP we use variables to store the data. A variable can store a string or a number and gives it specific case-sensitive name.
Example:
$myname=”prakash”;
$myage=”21″;
But variables have some syntax to follow. There are some rules
1. All variables names in PHP must start with a dollar sign($).
2. First letter of the variable must be alpha bate or underscore.
3. Remaining letters are alphanumeric or ‘_’  .
Few examples
$my name (wrong, no space between letters)
$myname(correct ) or
$myName(correct, use capital letters if we have two words)
$my_name( or separate with underscore)
Note: As we mentioned earlier don’t forget to add semicolon at end of each line.

Comments:
Comments allow you to write notes about the code. Comments are not viewed or acted by browser or users. Its simply in the code to provide information about the code or other feedback from the code developers.
Comments mainly used to :
1.Describe and explain the code.
2.Provide details about when the code was edited or reviewed.
3.Remove sections of the code, but leave them available for later use.
4.chat with other developers working on the code.
Well that’s a lot of uses writing comments. But how to write these comments.
writing comments in HTML, XHTML and XML is very easy.  Simply surround the text as follows
<!– this is comment line–>

In CSS, its little bit different, it goes like this
/* this is comment line*/

and in PHP, we write comments like this
//this is comment line

Comparison operators:
Comparison operators are used to compare two numbers and to perform operations depend upon the result. There are different comparison operators available:
    greater than,
<     less than,
>=  greater than or equal to
<=  less than or equal to
= = equal to
!=   not equal to

Conditionals and control flow:
Control flow is how a program decides to perform one action instead of another.In PHP we can use ‘if ‘ and ‘else’ to do this.
For decision making purpose we are using conditionals statements and comparison operators.

IF statement:  Now lets see how we use comparisons to ask yes or no questions.

Example: Say we want to write a program that asks whether your age is greater than 16 or not. If the answer is yes, we can respond with “Yes you can drive”. We can do this with If statement, like in below example.
if
and the result will be like this

result
An if statement is made up of the If keyword, a condition like in above example, and a pair of curly braces’ { }
If the answer is yes, the code inside the curly braces will run.
What if the condition gets false, we can do something even if the statement gets wrong. We can do this by else statement.
like in below example.

ifelse

The result will be like this.

result2

Switch Statement:

Switch case statements are a substitute for long if statements that compare a variable to several “integral” values  (“integral” values are simply values that can be expressed as an integer, such as the value of a char).  The basic format for using switch case is outlined below.  The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

syntax:
switch (variable) {
case this-value:
code to execute if<variable> == this-value
break;
case that-value:
code to execute if <variable>==that -value
break;
}

Example 1:

<?php
if ($i == 0) {
echo “i equals 0”;
} else if ($i == 1) {
echo “i equals 1”;
} else if ($i == 2) {
echo “i equals 2”;
}

Now instead of if else use switch then the program will be
switch ($i) {
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
}
?>

Example 2:

<?php
switch ($i) {
case “apple”:
echo “i is apple”;
break;
case “bar”:
echo “i is bar”;
break;
case “cake”:
echo “i is cake”;
break;
}
?>

The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements.
PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement.  If you don’t write a break statement at the end of a case’s statement list, PHP will go on executing the statements of the following case.
A special case is the default case. This case matches anything that wasn’t matched by the other cases.
Example with special case:
<?php
switch ($i) {
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
default:
echo “i is not equal to 0, 1 or 2”;
}
?>

The default case is optional, but it is wise to include it as it handles any unexpected cases.
It can be useful to put some kind of output to alert you to the code entering the default case if you don’t expect it to.

WHILE loop:

For repeating a block of content number of times we are using loops. A while loop in programming repeatedly executes a target statement as long as a given condition is true.
syntax:
while(condition) {
statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the loop.

Here, the key point to note is that a while loop might not execute at all.
When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Msys Inc</title>
<style type=”text/css”>
body{
font-size:24px;
font-family: verdana;
background-color:olive;
</style>
</head>
<body>
<?php
$a=1;
while($a <= 10) // here the condition will be checked, if condition is true it will enter into loop (execute)
{
echo “number $a <br>”; //This statement prints value of $a and the below statement increments the $a
$a=$a+1;                              // It will repeat up to $a=10 after that condition will be false and loop will end
}
?>
</body>
</html>

The output will be

result

Example 2:

<!DOCTYPE html>
<html>
<head>
<title>Msys Inc</title>
<style type=”text/css”>
body{
font-size:24px;
font-family:verdana;
background-color:olive;
</style>
</head>
<body>
<?php
$a=1;
while($a <= 6)
{
echo “<h$a>Header tag $a</h$a>”;
$a=$a+1;
}
?>
</body>
</html>

The output will be like this

result2

FOR LOOP:

In computer science a for-loop (or simply for loop)is a programming language control statement for specifying iteration,  which allows code to be executed repeatedly.
For-loops are  typically used when the number of iterations is known before entering the loop. For-loops are the shorthand way to make loops when the number of iterations is known.

Syntax:

for ( init; condition; increment ) {
statement(s);
}

We can see that  a basic for structure has three parts: init (intialization), condition(test), and Increment(update). Each part must be separated by a semicolon (;). The loop continues until the test or condition evaluates to false.
Here is the flow of control in a ‘for’ loop –

1.The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.

2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop.

3. After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

4. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the ‘for’ loop terminates.

Example:forloop

The out put will be

result

Example 2:

forloop2

The output will be

result2.php

Date building functions:

To display date or time in our page or screen we need to use date building functions. For example in the below picture a code was written to display the year.

date

the output will be

result like this the date will be displayed in our page.

We can use the following expressions to display time,date etc.

date2

The out put will be

result2

Arrays: Variables can store numbers or strings. But only able to store one number or one string. Good things we have Arrays.

  • store lists of data,
  • Can store different data types at the same time
  • Are ordered so the position of the each piece of data is fixed.

Example: $books=(“HTML”, “CSS”, “JS”, “PHP”, “MYSQL”, “Ajax”, “WordPress”);

The following program is an example how to define and access data from arrays.

array

The out put will be

result

We can access any of piece of data from array like below

array2

The elements are stored in the array starting from 0,1,2…etc. For example in the above array the book ‘HTML’ is stored in ‘0’ position and ‘css’ is stored in ‘1’ position etc. Now we are accessing third position element, by the above code we will get output as PHP.

To find the length of the array we can write code as below

array3

The output will be 7.

Associative Array:

In associative array we are saving values directly inside the keys. The below program shows how the  data or values can be stored in associative array.

associative

The output will be

result

Variable Building Functions:

Best way to test for a variable’s existence in PHP is to use variable building functions:
Mainly we have 3 variable building functions
1. isset( )
2. empty( )
3. unset( )

isset( ) will return FALSE if testing a variable that has been set to NULL.
Basically, isset() doesn’t check for whether the variable is set at all, but whether it’s set to anything but NULL. If our variable contains any value or data the isset() function will return TRUE otherwise it will return FALSE.

The empty( ) function works same as isset( ) function but in opposite direction.
If variable contains any  nonempty or nonzero value  it will return as FALSE , if varible is having null or 0 value it will return as TRUE.

The following program explains how isset function works

variable

The output will be true (or whatever value we have set). Here the output is hello prakash because the variable $myName contains some data, so isset( ) will result in true.

result

The unset( ) variable completely deletes the data of  variable.

 variable2

The data is deleted so the output will be

variable