Topic 2 : PHP
Er. Pradip Kharbuja

1
PHP
•

PHP : PHP Hypertext Prepocessor

•

Open Source Software
 Free to download and use

•

PHP is a server side script...
Starting PHP
•

A web-server with PHP installed. eg. XAMPP, WAMP

•

Some kind of programming environment.
 Notepad++, Dr...
A PHP Script
<html>
<head>
<title>My First PHP Script</title>
</head>
<body>
<?php echo "<p>Hello World!</p>"; ?>
</body>
...
My First PHP Script
•

PHP works like standard HTML, except you can set
sections of the page to be interpreted by the serv...
The Produced HTML
<html>
<head>
<title>My First PHP Script</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>

6
Variables
•

starts with a $ symbol.

•

We do not provide the type of a variable just a variable name.

•

eg.

<?php
$te...
Operators
•

Arithmetic, Assignment, Comparison, Logical, Conditional
Operators similar to JavaScript

•

String Operators...
Conditional Operators
1.

if statement

2.

if...else statement

3.

if...else if....else statement

4.

switch statement
...
Loop Statements
1.

for loop

2.

while loop

3.

do....while loop

4.

foreach loop

Task
Write

different programs to d...
Array
1.

Numeric Array

2.

Associative Array

3.

Multidimesional Array

11
Numeric Array
•

An array with a numeric ID key

•

Example #1 : ID key is automatically assigned.
$names = array("Peter",...
Numeric Array [Contd.]
<?php
$names[0] = "Peter";
$names[1] = "Joe";
$names[2] = "John";
echo $names[1] . " and " . $names...
Associative Array
•

When storing data, numeric array is not always the best way to do.

•

Example #1
$ages = array("Pete...
Multidimensional Array
•

each element in main array can also be an array

•

and each element in sub array can be an arra...
foreach loop
•

foreach statement is used to loop through arrays

Syntax:
foreach(array as value)
{
//code to be executed
...
foreach loop example
<?php
$subjects = array("Dynamic Website", "Database", "Java");
echo "I studied following subject : "...
Functions
•

similar to JavaScript

<?php
function add($x, $y){
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(...
HTML Forms & User Input
-

need HTML to get user input using get / post method

<form action = "result.php" method = "get"...
result.php
<body>
<?php
$full_name = $_GET['full_name'];
$pass_word = $_GET['pass_word'];
$question = $_GET['question'];
?...
HTML Forms & User Input
-

getting user input using post method

<form action = "result_post.php" method = "post">
<p>What...
result_post.php
<body>
<?php
$full_name = $_POST['full_name'];
$pass_word = $_POST['pass_word'];
$question = $_POST['quest...
Why use PHP?
•

PHP runs on different platforms (Windows, Linux, Unix, etc.)

•

PHP is compatible with almost all servers...
Why not use PHP?
•

It is hard to find good ‘example’ programs.

•

Unlike JavaScript, PHP requires server to execute.

•
...
Some More about PHP
•

PHP does not distinguish between variables of one type, and
variables of another in code.
 They ar...
Type Juggling.
We can find type of variable using gettype( ) function
1.

$foo = "0";

// String

2.

$foo++;

// Integer
...
Type Casting
•

Changing the contents of a variable from one type to another.

•

This is done through type casting:
$num ...
String Comparison in PHP
•

You might be tempted to use the == operator to compare
strings in PHP.
 You will not get the ...
Terminology
1.

Loosely typed
 A programming language that does not require the type of
variables to be declared.

2.

Ty...
Practice Questions
•

WAP to generate multiplication table of <your_roll_no>.

•

Generate a form to get user input, submi...
Assignment #3

Last Date to Submit : 20th Sept
Send to college.pradip@gmail.com

•

WAP to change background color of html...
Questions???
32
End of Topic - 2
33
Upcoming SlideShare
Loading in …5
×

Topic 2 : PHP

2,419 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,419
On SlideShare
0
From Embeds
0
Number of Embeds
265
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Topic 2 : PHP

  1. 1. Topic 2 : PHP Er. Pradip Kharbuja 1
  2. 2. PHP • PHP : PHP Hypertext Prepocessor • Open Source Software  Free to download and use • PHP is a server side scripting language.  You request a page on the internet.  The server interprets the PHP.  It returns the results of that interpretation to you as an HTML page. 2
  3. 3. Starting PHP • A web-server with PHP installed. eg. XAMPP, WAMP • Some kind of programming environment.  Notepad++, Dreamweaver, Eclipse, Netbeans • An internet browser to interact with the application. 3
  4. 4. A PHP Script <html> <head> <title>My First PHP Script</title> </head> <body> <?php echo "<p>Hello World!</p>"; ?> </body> </html> 4
  5. 5. My First PHP Script • PHP works like standard HTML, except you can set sections of the page to be interpreted by the server. • PHP sections are marked by blocks.  <?php starts a block of PHP  ?> ends a block of PHP  All of your PHP code goes in this block. • The echo function is used to output some text to the browser.  This script will display the text “Hello World” in a browser. 5
  6. 6. The Produced HTML <html> <head> <title>My First PHP Script</title> </head> <body> <p>Hello World!</p> </body> </html> 6
  7. 7. Variables • starts with a $ symbol. • We do not provide the type of a variable just a variable name. • eg. <?php $text = "Hello World"; echo $text; ?> <?php $name = "John"; echo "Hello $name"; echo "Hello {$name}"; ?> 7
  8. 8. Operators • Arithmetic, Assignment, Comparison, Logical, Conditional Operators similar to JavaScript • String Operators . .= <?php $first_name = "Bill"; $last_name = "Gates"; echo $first_name . " " . $last_name; $full_name = $first_name; $full_name .= " " . $last_name; ehco $full_name; ?> 8
  9. 9. Conditional Operators 1. if statement 2. if...else statement 3. if...else if....else statement 4. switch statement Task Write different programs to demonstrate above statements. 9
  10. 10. Loop Statements 1. for loop 2. while loop 3. do....while loop 4. foreach loop Task Write different programs to demonstrate above statements.
  11. 11. Array 1. Numeric Array 2. Associative Array 3. Multidimesional Array 11
  12. 12. Numeric Array • An array with a numeric ID key • Example #1 : ID key is automatically assigned. $names = array("Peter", "Joe", "John"); • Example #2 : ID key is assigned manually. $names = array(); $names[0] = "Peter"; $names[1] = "Joe"; $names[2] = "John"; 12
  13. 13. Numeric Array [Contd.] <?php $names[0] = "Peter"; $names[1] = "Joe"; $names[2] = "John"; echo $names[1] . " and " . $names[2] . " are " . $names[0] . "'s neighbours."; ?> Output : Joe and John are Peter's neighbours. 13
  14. 14. Associative Array • When storing data, numeric array is not always the best way to do. • Example #1 $ages = array("Peter" => 32, "Joe" => 30, "Bill" => 34); • Example #2 $ages["Peter"] = 32; $ages["Joe"] = "30"; $ages["Bill"] = 34; 14
  15. 15. Multidimensional Array • each element in main array can also be an array • and each element in sub array can be an array too & so on. $courses = array( "level4" => array("Database", "Java", "OSD"), "level5" => array("PHP", "Agile", "Network"), "level6" => array("C#", "Advanced Database") ); 15
  16. 16. foreach loop • foreach statement is used to loop through arrays Syntax: foreach(array as value) { //code to be executed } 16
  17. 17. foreach loop example <?php $subjects = array("Dynamic Website", "Database", "Java"); echo "I studied following subject : "; foreach($subjects as $subject) { echo "<br/>{$subject}"; } ?> 17
  18. 18. Functions • similar to JavaScript <?php function add($x, $y){ $total = $x + $y; return $total; } echo "1 + 16 = " . add(1, 16); echo "20 + 9 = " . add(20, 9); ?> 18
  19. 19. HTML Forms & User Input - need HTML to get user input using get / post method <form action = "result.php" method = "get"> <p>What is your name?</p> <input type = "text" name = "full_name"> <p>What is your question?</p> <input type = "text" name = "question"> <input type = "submit" value = "Ask"> <input type = "reset" value = "Clear values"> </form> 19
  20. 20. result.php <body> <?php $full_name = $_GET['full_name']; $pass_word = $_GET['pass_word']; $question = $_GET['question']; ?> <p><?php echo "<b>{$full_name}</b> asked : "; ?></p> <p><?php echo $question; ?></p> </body> 20
  21. 21. HTML Forms & User Input - getting user input using post method <form action = "result_post.php" method = "post"> <p>What is your name?</p> <input type = "text" name = "full_name"> <p>What is your question?</p> <input type = "text" name = "question"> <input type = "submit" value = "Ask"> <input type = "reset" value = "Clear values"> </form> 21
  22. 22. result_post.php <body> <?php $full_name = $_POST['full_name']; $pass_word = $_POST['pass_word']; $question = $_POST['question']; ?> <p><?php echo "<b>{$full_name}</b> asked : "; ?></p> <p><?php echo $question; ?></p> </body> 22
  23. 23. Why use PHP? • PHP runs on different platforms (Windows, Linux, Unix, etc.) • PHP is compatible with almost all servers used today (Apache, IIS, etc.) • PHP is FREE to download from the official PHP resource: www.php.net • PHP is easy to learn and runs efficiently on the server side • PHP supports many databases (MySQL, Informix, Oracle, Sybase, PostgreSQL, Generic ODBC, etc.) 23
  24. 24. Why not use PHP? • It is hard to find good ‘example’ programs. • Unlike JavaScript, PHP requires server to execute. • the names of built-in functions are inconsistent. array_rand( ) - Pick random entries out of an array shuffle( ) - shuffles array 24
  25. 25. Some More about PHP • PHP does not distinguish between variables of one type, and variables of another in code.  They are all just ‘variables’. • In technical terms, it is loosely typed.  This means you have to be careful. • PHP is famous for type juggling.  The automatic type conversions that PHP performs 25
  26. 26. Type Juggling. We can find type of variable using gettype( ) function 1. $foo = "0"; // String 2. $foo++; // Integer 3. $foo = $foo + 1.3; // Double 4. $foo= array( ); // Array 5. $foo= "car"; // String 6. $foo[0] = "b"; echo $foo; // bar 26
  27. 27. Type Casting • Changing the contents of a variable from one type to another. • This is done through type casting: $num = 10; //integer $strnum = (string)$num; //string 1. (int), (integer) - cast to integer 2. (bool), (boolean) - cast to boolean 3. (float), (double), (real) - cast to float 4. (string) - cast to string 5. (array) - cast to array 6. (object) - cast to object 27
  28. 28. String Comparison in PHP • You might be tempted to use the == operator to compare strings in PHP.  You will not get the behaviour you want doing that.  The == in PHP is called a loose comparison operator. • PHP offers strict comparison operators too  ===  !==  These should be used for string comparisons 28
  29. 29. Terminology 1. Loosely typed  A programming language that does not require the type of variables to be declared. 2. Type juggling  The automatic type conversions that PHP performs 3. Type casting  Changing the type of a variable from one kind of data to another 29
  30. 30. Practice Questions • WAP to generate multiplication table of <your_roll_no>. • Generate a form to get user input, submit to find whether it is prime or not. • Generate a form to get user input, submit to find the fibonacci series. • Generate a form to get user input, submit to find the factorial. 30
  31. 31. Assignment #3 Last Date to Submit : 20th Sept Send to college.pradip@gmail.com • WAP to change background color of html according to day. [Hint : research about date( ) function] • Demonstrate different format of PHP Date. • Demonstrate function with default value. • Demonstrate a program of local & global variable. • Demonstrate different PHP Array functions array_combine( ) array_key_exists( ) array_rand( ) array_reverse( ) count( ) in_array( ) array_merge( ) array_unique( ) shuffle( ) sort( ) 31
  32. 32. Questions??? 32
  33. 33. End of Topic - 2 33

×