Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Web Technologies

Lecture PHP
Instructor
Dr. Mariam Rehman
Professor

Department of Information Technology, Government College University


Contents

• Conditional Statement
• Loop
• Array
• Cookies
• Echo & Print
• Print_r
Conditional Statement

• if statement - use this statement to execute


some code only if a specified condition is true
• if...else statement - use this statement to
execute some code if a condition is true and
another code if the condition is false
• if...elseif....else statement - use this statement
to select one of several blocks of code to be
executed
• switch statement - use this statement to select
one of many blocks of code to be executed
Conditional Statement---If Statement

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


1. <html>
2. <body>
3. <?php
4. $d=date("D");
5. if ($d=="Fri") echo "Have a nice weekend!";
6. ?>
7. </body>
8. </html>
Conditional Statement---
If… else Statement
• if (condition)
• code to be executed if condition is true;
• else
• code to be executed if condition is false;
1. <?php
2. $d=date("D");
3. if ($d=="Fri")
4. echo "Have a nice weekend!";
5. else
6. echo "Have a nice day!";
7. ?>
Conditional Statement---
If… else Statement
If more than one line should be executed if a condition is
true/false, the lines should be enclosed within curly braces:
1. <?php
2. $d=date("D");
3. if ($d=="Fri")
4. {
5. echo "Hello!<br />";
6. echo "Have a nice weekend!";
7. echo "See you on Monday!";
8. }
9. ?>
Conditional Statement---
If… else Statement
If more than one line should be executed if a condition is
true/false, the lines should be enclosed within curly braces:
1. <?php
2. $d=date("D");
3. if ($d=="Fri")
4. {
5. echo "Hello!<br />";
6. echo "Have a nice weekend!";
7. echo "See you on Monday!";
8. }
9. ?>
Conditional Statement---
if...elseif....else Statement
Use the if....elseif...else statement to select one of several
blocks of code to be executed. if (condition)
code to be executed if condition is true;
1. <?php elseif (condition)
code to be executed if condition is true;
2. $d=date("D"); else
3. if ($d=="Fri") code to be executed if condition is false;

4. echo "Have a nice weekend!";


5. elseif ($d=="Sun")
6. echo "Have a nice Sunday!";
7. else
8. echo "Have a nice day!";
9. ?>
Conditional Statement---
Switch Statement Cont.
Use the switch statement to select one of many blocks of
code to be executed.
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1
and label2;
}
Conditional Statement---
Switch Statement
Use the switch statement to select one of many blocks of
code to be executed.
1. <?php
2. switch ($x)
3. {
4. case 1:
5. echo "Number 1";
6. break; 12. break;
7. case 2: 13. default:
8. echo "Number 2"; 14. echo "No number between 1 and
9. break; 3";
15. }
10. case 3: 16. ?>
11. echo "Number 3";
LOOP

• while - loops through a block of code while a


specified condition is true
• do...while - loops through a block of code once,
and then repeats the loop as long as a specified
condition is true
• for - loops through a block of code a specified
number of times
• foreach - loops through a block of code for each
element in an array
LOOP --- While Loop

• The while loop executes a block of code while a


condition is true.
1. <?php while (condition)
{
2. $i=1; code to be executed;
3. while($i<=5) }
4. {
5. echo "The number is " . $i . "<br />";
6. $i++;
7. }
8. ?>
LOOP --- do...while Statement

do...while statement will always execute the block


of code once, it will then check the condition, and
repeat the loop while the condition is true.
1. <?php
do
2. $i=1; {
3. do code to be executed;
}
4. { while (condition);
5. $i++;
6. echo "The number is " . $i . "<br />";
7. } while ($i<=5);
8. ?>
LOOP --- do...while Statement

For loop is used when you know in advance how


many times the script should run.

for (init; condition; increment)


1. <?php {
2. for ($i=1; $i<=5; $i++) code to be executed;
}
3. {
4. echo "The number is " . $i . "<br />";
5. }
6. ?>
LOOP --- do...while Statement

foreach loop is used to loop through arrays

• For every loop iteration, the value of the current


array element is assigned to $value (and the
array pointer is moved by one)

• so on the next loop iteration, you'll be looking at


the next array value.
LOOP --- do...while Statement

1. <?php foreach ($array as $value)


{
2. $x=array("one","two","three"); code to be executed;
3. foreach ($x as $value) }
4. {
5. echo $value . "<br />";
6. }
7. ?>
Arrays

• A variable is a storage area holding a number or text.


The problem is, a variable will hold only one value.
• An array is a special variable, which can store multiple
values in one single variable.

In PHP, there are three kind of arrays:


A. Numeric array - An array with a numeric index
B. Associative array - An array where each ID key is
associated with a value
C. Multidimensional array - An array containing one or
more arrays
Arrays --- Numeric Arrays Cont.

• A numeric array stores each array element with a


numeric index.
• There are two methods to create a numeric array.

• Method 1: Index is assigned automatically (the index


starts at 0)

• Method 2: Assign index manually.


Arrays --- Numeric Arrays

• Method 1: Index is assigned automatically (the index


starts at 0)
– $fruits=array(“mango",“apple",“banana,“orange");

• Method 2: Assign index manually.


– $fruits[0]=“mango";
– $fruits[1]=“apple"; <?php
– $fruits[2]=“banana"; $fruits[0]=“mango";
$fruits[1]=“apple";
– $fruits[3]=“orange"; $fruits[2]=“banana";
$fruits[3]=“orange";
echo $fruits[0] . " and " . $fruits[1] . " are King
fruits.";
?>
Arrays --- Associative Arrays

An associative array, each ID key is associated with a


value.
Example 1:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
Or
• $ages['Peter'] = "32";
• $ages['Quagmire'] = "30";
• $ages['Joe'] = "34"; <?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
Arrays --- Multidimensional Arrays Cont.

• In a multidimensional array, each element in the


main array can also be an array.
• And each element in the sub-array can be an
array, and so on.
Example
$families = array( "Griffin"=>array ( "Peter", "Lois", "Megan"
), "Quagmire"=>array ( "Glenn" ), "Brown"=>array
( "Cleveland", "Loretta", "Junior") );
Arrays --- Multidimensional Arrays Cont.

• Array ([Griffin] => Array


• ( [0] => Peter
• [1] => Lois
• [2] => Megan
• )
• [Quagmire] => Array ( [0] => Glenn ) [Brown]
=> Array ( [0] => Cleveland [1] => Loretta [2] =>
Junior ) )
Output?
echo "Is " . $families['Griffin'][2] ." a part of the
Griffin family?";

Output
Is Megan a part of the Griffin family?
So what is the difference between
Basic Array and Associative Array?
e s
ki
oo
C
Cookies

• A cookie is often used to identify a user.


• A cookie is a small file that the server embeds on
the user's computer.
• Each time the same computer requests a page with
a browser, it will send the cookie too.
• With PHP, you can both create and retrieve cookie
values.
Cookies

• A cookie is often used to identify a user.


• A cookie is a small file that the server embeds on
the user's computer.
• Each time the same computer requests a page with
a browser, it will send the cookie too.
• With PHP, you can both create and retrieve cookie
values.
Cookies --- Create Cookie

• setcookie() function is used to set a cookie.


• setcookie() function must appear BEFORE the <html>
tag.
• The value of the cookie is automatically URLencoded
when sending the cookie, and
• automatically decoded when received (to prevent
URLencoding, use setrawcookie() instead).
Cookies --- Create Cookie

• Syntax: setcookie(name, value, expire, path, domain);


• <?php What you think about this?

• setcookie("user", "Alex Porter", time()+3600);


• ?>

OR
• <?php $expire=time()+60*60*24*30; setcookie("user",
"Alex Porter", $expire); ?>
Cookies ---
Retrieve a Cookie Value

PHP $_COOKIE variable is used to retrieve a cookie value.


1. <?php
2. // Print a cookie

3. echo $_COOKIE["user"];
4. // A way to view all cookies
5. print_r($_COOKIE);

6. ?>
Cookies ---
Retrieve a Cookie Value

• Use the isset() function to find out if a cookie has been set

1. <?php
2. if (isset($_COOKIE["user"]))

3. echo "Welcome " . $_COOKIE["user"] . "!<br />";

4. else

5. echo "Welcome guest!<br />";

6. ?>
Cookies ---
Delete a Cookie

• While deleting a cookie, make sure that the expiration


date is in the past.
1. <?php
2. // set the expiration date to one hour ago
3. setcookie("user", "", time()-3600);
4. ?>
Cookies ---
Delete a Cookie

• While deleting a cookie, make sure that the expiration


date is in the past.
1. <?php
2. // set the expiration date to one hour ago
3. setcookie("user", "", time()-3600);
4. ?>
What to do if browser does not support
cookies?
PHP Output--- ECHO and Print Cont.

• PHP echo and print both are PHP language constructs.


• A programming language construct refers to a built-in
feature of programming language.

• Both are used to display the output in PHP.


PHP Output--- ECHO Cont.

• echo is a statement i.e used to display the output. it can


be used with parentheses echo or without parentheses
echo.
• echo can pass multiple string separated as ( , )
• echo doesn’t return any value
• echo is faster than print
PHP Output--- ECHO Cont.

Example #1 Example #2

1. <?php
1. <?php
2.
2. 3. $name = “Web Engineering ";
3. $name=“Allah Bakhsh"; 4.
4. 5. $profile = "PHP Learners";
5. echo $name; 6.
7. $age = 21;
6. //or
8.
7. 9. echo $name , $profile , $age, "
8. echo ($name); years old";
9. 10.
10. ?> 11. ?>
PHP Output--- ECHO

1. <?php
2.
3. $name = “Allah Bakhsh ";
4.
5. $ret = echo $name;
6.
7. ?> We got Error because echo has no return type.

Output ?

Parse error: syntax error, unexpected T_ECHO


PHP Output--- Print Cont.

• Print is also a statement i.e used to display the output. it


can be used with parentheses print( ) or without
parentheses print.
• print does not pass multiple argument
• print always return 1
• it is slower than echo
PHP Output--- Print Cont.

Example #1 Example # 2
1. <?php 1. <?php

2. $name=“Jannat Bibi"; 2. $name = “Janant Bibi";


3. $ret = print $name;
3. print $name;
4. //To test it returns or not
4. //or
5. echo $ret;
5. print ($name);
6. ?>
6. ?>
Output? Output ?
PHP Output--- Print

1. <?php
2. $name = “Derya Khan”;
3. $profile = "PHP Developer";
4. $age = 21;
5. print $name , $profile , $age, " years old";
6. ?>

Output?
Parse error: syntax error

Error because multiple argument are not allow for print


Guess!
What is Print_r in PHP?

print_r() displays information about a variable


in a way that's readable by humans.
References and Readings
• https://www.safaribooksonline.com/library/view/learning-php-
5/0596005601/ch04.html
• https://www.phptpoint.com/php-echo-print/
• https://www.youtube.com/watch?v=tOuym4a7XjY
• https://www.youtube.com/watch?v=I01XMRo2ESg
• https://www.youtube.com/watch?v=JCvPnwpWVUQ&t=180s
• https://www.youtube.com/watch?v=AkcwvwwAZiA
• https://www.youtube.com/watch?v=-X4XQkOR-k4
• https://www.youtube.com/watch?v=cLUaBc8985g
• https://www.youtube.com/watch?v=-
oUXcTz1eEA&list=PL_c9BZzLwBRK-pdEkJHvsqd5yBDxvggbs
Assignment Cont.
• Have you check the difference between two
installation types?

• Installation through WAMP


• Installation separately IIS, MYSQL and PHP
Assignment Cont.
• Have you tried to use any of theses tools?

• Notepad++
• Dream Weaver
• Eclipse
• NetBeans

You might also like