Learn-Php.docx

shrubberystatuesqueData Management

Dec 1, 2012 (4 years and 7 months ago)

443 views

Learn Php

Php Intro.:


What is PHP?



Known as

H
ypertext
P
reprocessor




S
erver
-
side scripting language, like ASP



Its

scripts are executed on the server

side.



The
databases

which supports by php are (
MySQL, Informix, Oracle, Sybase, Solid,
PostgreSQL,
Generic ODBC, etc.)



This is also known as

open source software



The files which contains php code saved as
.php

extension.(for eg. If our file name is
“siddharth” then I save it with .php extension “
siddharth.php
” )



Php

file also contains the html code. But always saved as the
.php

extension.



We can import the php code in any HTML document any where it run success fully.




But keep in mind that file will always saved as the .php extension if it contains even a
little bit
of php code.

PHP Install:

Follow the tutorial given on the link below

and know

how to install PHP5:
http://www.php.net/manual/en/install.php

Download PHP
:

Download
link is

here:
http://www.php.net/downloads.php

Download MySQL Database

Download MySQL
link is

here:
http://www.mysql.com/downloads/

Download Apache Server

Dow
nload Apache
link is

here:
http://httpd.apache.org/download.cgi

PHP Syntax

A PHP
code

block

of code we

always starts with
<?php

and ends with
?>
. A PHP code of

block can be
placed anywhere in the document.

As I said earlier.


Example

<html>

<body>

<?php


echo "
Dulcet Solutions
";

?>


</body>

</html>

Note:
echo

is used to print something on screen as in
c language

we use
print
.

Now the Comments in PHP


In PHP, we
use // to make a single
-
line comment or /* and */ to make a large comment block.

We cannot declare a comment inside a comment.( for eg.
/*first comment/*second comment*/
*/

this is wrong

)

<html>

<body>


<?php

//
This is a
single
comment


/*

This is

a
large
comment

block

*/

?>


</body>

</html>

Variables in PHP

All variables in PHP start with a $ sign symbol.

Declaration of php variable
:

$var_name = value;

Never forget

$ sign at the beginning of the variable.
If it is not given, then
In that case it
will not work.

Example

<?php

$txt="
Dulcet Solutions”; (string variable)

$x=16

?>



PHP Operators

This section lists the different operators used in PHP.

Arithmetic Operators

Operator

Description

Example

Result

+

Addition

x=2

x+2

4

-

Subtraction

x=2

5
-
x

3

*

Multiplication

x=4

x*5

20

/

Division

15/5

5/2

3

2.5

%

Modulus (division remainder)

5%2

10%8

10%2

1

2

0

++

Increment

x=5

x++

x=6

--

Decrement

x=5

x
--

x=4

Assignment Operators

Operator

Example

Is The Same As

=

x=y

x=y

+=

x+=y

x=x+y

-
=

x
-
=y

x=x
-
y

*=

x*=y

x=x*y

/=

x/=y

x=x/y

.=

x.=y

x=x.y

%=

x%=y

x=x%y

Comparison Operators

Operator

Description

Example

==

is equal to

5==8 returns false

!=

is not equal

5!=8 returns true

<>

is not equal

5<>8 returns true

>

is greater than

5>8 returns false

<

is less than

5<8 returns true

>=

is greater than or equal to

5>=8 returns false

<=

is less than or equal to

5<=8 returns true

Logical Operators

Operator

Description

Example

&&

and

x=6

y=3

(x < 10 && y > 1) returns true

||

or

x=6

y=3

(x==5 || y==5)
returns false

!

not

x=6

y=3

!(x==y) returns true




Conditional Statements

In this lesson We are going to introduce Conditional steatment in PHP. conditional statement is a
statement performed if true or false.

<h4>
In PHP we have the following conditional statements:</h4>

<ul>


<li> if statement
-

use this statement to execute some code only if a specified condition is
true</li>


<li> if...else statement
-

use this statement to execute some code if a condition is t
rue and
another code if the condition is false</li>


<li> if...elseif....else statement
-

use this statement to select one of several blocks of code to be
executed</li>


<li> switch statement
-

use this statement to select one of many blocks of code to

be
executed</li>

</ul>

if Statement

Use the if statement to execute some code only if a specified condition is true.

Syntax

if (condition) code to be executed if condition is true;

<?php

$k=10”

if ($k<=10)

{

echo "Have a nice weekend!";

}


?>

No
tice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true

if...else Statement

Use the if....else statement to execute some code if a condition is true and

another code if a condition is false.

Example:

if (condition)


code to be executed if condition is true;

else


code to be executed if condition is false;

<?php

$k=10;

if ($k<=10)

{

echo "value is ten";

}


else

{

echo "value is more than ten";

}


?>

if...else if....else Statement

Use the if....else if...else statement to select one of several blocks of

code to be executed.

Example

<?php

$k=10;

if ($k<10)

{

echo "value is less than ten";

}

else if(k>10)

{

echo "value is more than ten";

}

else {

echo "value is equal to ten";

?>

PHP
Switch Statement

The <em>switch</em> statement is similar to a series of IF statements on the same expression.
Sometimes you may want to compare the same variable (or expression) with many different values,
and execute a different piece of code depen
ding on which value it equals to. Use the switch statement
to select one of many blocks of code to be executed.

Example

<code><?php

switch ($i) {

case "apple":

echo "i is apple";

break;

case "bar":

echo "i is bar";

break;

case "cake":

echo "i is cake";

break;

}

?> </code>






PHP Loops

In PHP, loops are used when we want to repeat a block of code. There are following looping statements
in PHP


1) While Loop
-

loops through a block of code while a specified condition is true


2) do...while
-


loops through a block of code once, and then repeats the loop as long as a specified
condition is true 3)for
-

loops through a block of code a specified number of times


4) for each
-

loops through a block of code for each element in an array

T
he while Loop

The function of the while loop is to do a task over and over as long as the specified conditional
statement is <em>true</em>. This logical check is the same as the one that appears in a PHP if
statement to determine if it is <em>true</em> or

<em>false.</em>

Example:

<?php

$x = 1;

while (x < 5)

{

echo $x;

x++;

}

?>


Output:

1

2

3

4

The do...while Statement

A do
-
while loop <em>always</em> executes its block of code at least once. This is because the
conditional statement is not checked until
<em>after</em> the contained code has been executed.


Example:

<?php

$x = 0;

do {

echo "Hi this is example of Do While Loop";

} while ($x > 1);

?>


Output:

hi this is example of do while loop.

The for Loop

A FOR loop is used when we want to execute a
block of code for certain number of times.

Syntax

for (initialize counter; condition until counter is reached; increment counter)

{

//execute block of code

}

1)In the first statement, we initialize a counter variable to an number value.

2)In the second sta
tement, we set a condition (a max/min number value) until the counter is reached.

3) In the third statement, we set a value by how much we want the counter variable to incremented by.


Example

<?php

<pre>


for($i=0; $i<=3; $i=$i+1)


{



echo $i."hi ";


}

?></pre>


Output: 1 hi


2 hi


3 hi


The foreach Loop

If you have an associative array that you want to iterate through it is better choice to use foreach
loop.<em>Foreach</em> works only on arrays, and will issue an error when you try to use it on

a
variable with a different data type or an uninitialized variable.

Example:

<?php

$a=array("one","two","three");

foreach ($a as $value)

{

echo $value ;

}

?>


Output:

one

two

three

PHP Functions

A function is just a name we give to a block of code
that can be executed whenever we need it.. A
function will be executed by a call to the function. You may call a function from anywhere within a page.
Using functions you can save your lot of time.

How to Create PHP Function

Example

<?php function Test( )

// test is name of function.

{

echo "this is my first function";

}

Test( ); // call to the function


?>


Output: this is my first function.

PHP Functions with parameters

To add more functionality to a function, we can pass parameters in functions.

A parameter is just like a

Variable. Parameters are specified after the function name, inside the parentheses. You can add integer
as well as string as parameter.

<?php

function Test($num1,$num2,$num3)

{

$sum= $num1 +$num2;

echo $sum;

echo $num2;

}

Test(4
,5,8); // call to function with parameter value

?>


Output:

9 5

PHP include () Function

PHP include function is used to include content of one page into another or to include anything in your
current page.. If an error occurs, the include

() function generates a warning, rest of code the script will
continue execution.


Example :

Here we are going to include your header.php file into your current page.

<?php

<?php include('header.php'); ?>

function Test()

{

echo "this is example of include

function";

}

Test();

?>


output:

this is example of include function. To include this file “include.php” you have to create this file which
will look like this:

<html>

<head>

</head>

<body>

Similarly you can create any file and can include it in your cu
rrent file using include( ).


PHP Array

Array are used to hold similar Data type.or using Array we can store multiple values in a variable at same
time.An array is a special variable, which can store multiple values in one single variable.

There are thre
e types of Arrays in PHP :

1)Numeric Array

2)Associative Array

3)Multidimensional Array



Numeric Array

Numeric array stored data in numeric index. Like $array[0],$array[1] etc.

<h4>syntax:</h4>

$myarray=array("Audi","Toyota","BMW","Santro");

Here $
myarray is a variable holding array of string.we can also strore integer values as well.

Each element in the array has its own index so that it can be easily accessed.Array indexes starts from
0.Like first element of array will be accessed as : $myarray[0
];


Example:

<?php

$myarray=array("Audi","Toyota","BMW","Santro");

echo $myarray[0];

echo $myarray[1]

echo$myarray[2]

echo $myarray[3]

?>



Output:

Audi

Toyota

BMW

Santro



<h2>Associative Array</h2>

Associative array stored data in key value pair.Like
$array[1,"nidhi"]


Syntex:

$myarray = array(30=>"Audi", 15=>"Santro", 34=>"toyota",25=>"BMW");

<h4>Example:</h4>

<?php

$myarray = array(30=>"Audi", 15=>"Santro", 34=>"toyota",25=>"BMW");

echo $['30'] ;

echo $['15'] ;

echo $['34'] ;


echo $[ '25'];

?>


Output:

Audi

Santro

toyota

BMW

<h2>Multidimensional Array </h2>

Multidimensional array is an array that has a value of an element as another array.This means each
element in the main array can also be an array.


Example:

$myarray = array

(

"car1"=>arr
ay

(

"Santro",

"Toyota",

"Santro"

),

"car2"=>array

(

"BMW"

));

echo $myarray['car1'][1];

echo $myarray['car2'][0;]

<h4>Output:</h4>

Toyota

BMW

The
PHP $_GET

and
$_POST

variables are used to
retrieve information from forms
.

PHP Form Handling

The example
below contains an HTML form with two input fields and a submit button:

Example

<html>

<body>


<form action="
home
.php" method="post">

Name: <input type="text" name="fname" />

Age: <input type="text" name="age" />

<input type="submit" />

</form>


</body>

<
/html>

When a user fills out the form above and click on the submit button, the form data is sent to a PHP file,
called "welcome.php":

"
hello
.php" looks like this:

<html>

<body>


Most
Welcome <?php echo $_POST["fname"]; ?>!<br />

You are <?php echo $_POS
T["age"]; ?> years old.


</body>

</html>

Output could be something like this:

Most
Welcome
siddharth

You are 28 years old.






The $_GET Function

The built
-
in $_GET function is used to collect values from a form sent with method="get".

Information sent

from a form with the GET method is visible to everyone (it will be displayed in the
browser's address bar) and has limits on the amount of information to send.

Example

<form action="
h
ome.php" method="get">

First
Name: <input type="text" name="fname" />

Ag
e: <input type="text" name="age" />

<input type="submit" />

</form>

When the user clicks the "Submit" button, the URL sent to the server could look something like this:

http://www.dulcetsolutions.com/home.php?fname=siddharth&age=28

The "welcome.php" fil
e can now use the $_GET function to collect form data (the names of the form
fields will automatically be the keys in the $_GET array):

Most Wel
come <?php echo $_GET["fname"]; ?>.<br />

You are <?php echo $_GET["age"]; ?> years old!


When to use method="
get"?

When using method="get" in HTML forms, all variable names and values are displayed in the URL.