topic 2 : php
DESCRIPTION
TRANSCRIPT
Topic 2 : PHPEr. Pradip Kharbuja
1
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
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
A PHP Script<html>
<head>
<title>My First PHP Script</title>
</head>
<body>
<?php echo "<p>Hello World!</p>"; ?>
</body>
</html>
4
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
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
$text = "Hello World";
echo $text;
?>
<?php
$name = "John";
echo "Hello $name";
echo "Hello {$name}";
?>
7
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
Conditional Operators1. if statement
2. if...else statement
3. if...else if....else statement
4. switch statement
Task
Write different programs to demonstrate above statements.
9
Loop Statements1. for loop
2. while loop
3. do....while loop
4. foreach loop
Task
Write different programs to demonstrate above statements.
Array1. 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", "Joe", "John");
• Example #2 : ID key is assigned manually.
$names = array();
$names[0] = "Peter";
$names[1] = "Joe";
$names[2] = "John";
12
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
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
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
foreach loop• foreach statement is used to loop through arrays
Syntax:
foreach(array as value)
{
//code to be executed
}
16
foreach loop example<?php
$subjects = array("Dynamic Website", "Database", "Java");
echo "I studied following subject : ";
foreach($subjects as $subject)
{
echo "<br/>{$subject}";
}
?>
17
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
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
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
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
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
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
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
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
Type Juggling.
1. $foo = "0";
2. $foo++;
3. $foo = $foo + 1.3;
4. $foo= array( );
5. $foo= "car";
6. $foo[0] = "b"; echo $foo;
// String
// Integer
// Double
// Array
// String
// bar
26
We can find type of variable using gettype( ) function
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
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
Terminology1. 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
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
Assignment #3• 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_merge( )
array_rand( ) array_reverse( ) array_unique( )
count( ) in_array( ) shuffle( ) sort( )
31
Last Date to Submit : 20th Sept
Send to [email protected]
Questions???
32
End of Topic - 2
33