• No results found

Lec 19 PHP - II.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 19 PHP - II.pptx"

Copied!
49
0
0

Loading.... (view fulltext now)

Full text

(1)

1

CS1113 Web Programming

Lecture 19

Data Types

(2)

• Variable is a symbolic representation of a

value. Its change over time or vary.

(3)

Variables Names

1) Start with a $ sign.

2) Followed by a letter or underscore.

3) Can contain letters, numbers, underscores

or dashes.

4) No spaces

(4)

Example

Variable Names

• $item • $Item

• $myVariable • $this_variable • $this-variable • $product3

• $_book

• $__bookPage

(5)

EXAMPLES

• <?php

$var1 = 10;

echo $var1;

?>

• <?php

$var2 = “Hello World”;

echo $var2;

(6)

EXAMPLE OF CASE-SENSITIVE

• <?php

$my_variable = “Hello World”;

$my_Variable = “Hello World Again”;

echo $my_Variable;

?>

• Output:

(7)

EXAMPLE

• <?php

$var1 = 10;

echo $var1;

echo “<br/ >”; $var1 = 100;

echo $var1;

?>

• Output: 10

(8)

STRINGS

• <?php

echo “Hello World <br />”;

echo ‘Hello World <br />’;

$my_variable = “Hello World”;

echo $my_variable;

echo $my_variable . “ Again”;

?>

• Output: Hello World Again

HTML tags can also be

used in strings

We can also concatenate

(9)

EXAMPLE

• We can use variable in double quotes as well.

• <?php

$my_variable = “Hello World”;

?>

• <?php

echo “$my_variable Again.”;

?>

(10)

String Functions

• <?php

$firstString = “The quick brown tiger”;

$secondString = “ jumped over the lazy cat”;

?>

• <?php

$thirdString = $firstString;

$thirdString .= $secondString; echo $thirdString;

?>

• Output: The quick brown tiger jumped over the lazy cat.

Another way of

(11)

• <?php $firstString = “The quick brown tiger”; ?> <br />

• Lower case:

<?php echo strtolower($firstString); ?><br />

• Upper case:

<?php echo strtoupper($firstString); ?><br />

• Uppercase first-letter:

<?php echo ucfirst($firstString); ?><br />

• Uppercase words:

(12)

• Length:

<?php echo strlen($firstString); ?><br/>

• Trim:

<?php echo $fourthString = $firstString .

Trim(secondString); ?><br/>

• Find:

<?php echo strstr($thirdString, “brown”); ?><br />

• Replace by String:

<?php echo str_replace(“quick”, “super-fast”, $thirdString); ?>

(13)

MORE STRING FUNCTIONS

• Repeat:

str_repeat($thirdString, 2);

• Make substring:

substr($thirdString, 5, 10);

• Find position:

strpos($thirdString, “brown”);

• Find character:

(14)
(15)

Numbers

• <?php

$var1 = 3; $var2 = 4;

?>

Basic Math:

<?php echo ((1 + 2 + $var1)* var2) / 2 -5; ?>

<br />

(16)

EXAMPLE

• <?php

$var1 = 3; $var2 = 4;

?>

+=: <?php

$var2 += 4;

echo $var2;

?>

• Output: +=: 8

You can add any value and

(17)

• +=: <?php $var2 += 4; echo $var2; ?> <br/> • -=: <?php $var2 -= 4; echo $var2; ?> <br/> • *=: <?php $var2 *= 3; echo $var2; ?> <br/> • /=: <?php $var2 /= 4; echo $var2; ?> <br/>

• Output: +=: 8 -=: 4

(18)

INCREMENT & DECREMENT

• Increment by 1.

Increment: <?php $var2++; echo $var2; ?>

• Decrement by 1.

(19)

FLOATING POINT NUMBERS

• <?php $var1 = 3.14; ?>

• Floating Point: <?php echo $myFloat = 3.14; ?>

• Round: <?php echo round($myFloat, 1); ?>

• Ceiling: <?php echo ceil($myFloat); ?>

• Floor: <?php echo floor($myFloat); ?>

• Output: Floating Point: 3.14 Round: 3.1

(20)

FLOATING POINT FUNCTIONS

Absolute Value: abs(0 - 300);

Exponential: pow(2, 8);

Square Root: sqrt(100);

Modulo: fmod(20, 7);

Random (any): rand();

(21)

ARRAYS

• You can think of an array is a variable, in which you can assign multiple values.

• <?php $array = array(4, 8, 15, 17 , 23, 42); ?> <?php echo $array[1]; ?>

Output: 8

(22)

EXAMPLE

• <?php

$array2 = array(6, “amjad”, “aslam”, array(“x”, “y”, “z”));

?>

<?php echo $array2[2]; ?> <br/>

<?php echo $array2[3]; ?> <?php echo $array2[3][1]; ?>

• Output: aslam Array

(23)

ADD/UPDATE THE VALUE OF ARRAY

• <?php

$array2[3] = “cat”;

?>

<br />

• <?php

echo $array2[3];

(24)

ASSOCIATIVE ARRAY

• <?php $array3 = array(“first_name” => “Yasir”, “last_name” => “Naeem”);

?>

• <?php echo $array3[“first_name”]; ?> <br/> • <?php echo $array3[“first_name”] . “ ” .

$array3[“last_name”]; ?>

• Output: Yasir

Yasir Naeem

(25)

• <?php $array3[“first_name”] = “Kashif”; ?>

• <?php echo $array3[“first_name”] . “ ” .

$array3[“last_name”]; ?>

• Output: Kashif Naeem

• <pre><?php print_r($array2); ?></pre>

Print readable command gives

contents of an array in readable form

(26)

ARRAY FUNCTIONS

• <?php $array1 = array(4, 8, 15, 16, 23, 42); ?>

Count: <?php echo count($array1); ?><br/>

Max value: <?php echo max($array1); ?><br/>

Min value: <?php echo min($array1); ?><br/>

Sort: <?php sort($array1); print_r($array1); ?>

(27)

• Implode: <?php echo $string1 = implode(“*”, $array1); ?> <br/>

Implode function is used to separate the array by * to make a string.

• Explode: <?php print_r(explode(“ * ”,

$string1)); ?>

It does the reverse, it takes the string that we just created find every instance,

(28)

• In array: <?php

echo in_array(3, $array1);

?>

This function help us to find a

particular string or value in our array.

(29)

BOOLEANS

• <?php

$bool1 = true; $bool2 = false;

?>

$bool1: <?php echo $bool1; ?><br/> $bool2: <?php echo $bool2; ?><br/>

• Output: $bool1: 1 $bool2:

true/false doesn’t mean it’s a string. When we type true, php understands its

(30)

• <?php

$var1 = 3;

$var2 = “cat”;

?>

$var1 is set: <?php echo isset($var1); ?>

$var2 is set: <?php echo isset($var2); ?>

• Note: isset() is very useful function, which we can use with conditional statements.

Through this function we are asking, is the value of the variable is set or not. It will

(31)

• <?php unset($var1); ?>

$var1 is set: <?php echo isset($var1); ?>

$var2 is set: <?php echo isset($var2); ?>

• Output: $var1 is set: $var2 is set: 1

This function will unset the value of a

(32)

• <?php

var4 = 0; var5 = “0”;

var6 = NULL;

?>

$var1 empty: <?php echo empty($var1); ?><br/>

$var4 empty: <?php echo empty($var4); ?><br/>

$var5 empty: <?php echo empty($var5); ?><br/>

(33)

TYPE CASTING

• The idea here is that, all these different types, we are working with i.e. numbers,

strings. There is fluidity between them. We actually want to switch one type to another.

• <?php

$var1 = “2”; $var1 += 3; echo $var1;

?>

(34)

• <?php

$var1 = “2 brown tigers”; $var2 = $var1 + 3;

echo $var2;

?>

• <?php

echo gettype($var1); echo “<br/>”;

echo gettype($var2);

?>

(35)

• How actually switch the type.

• <?php

$var1 = “2 brown tigers”; $var2 = $var1 + 3;

echo $var2;

?> <br/>

• <?php

settype($var2, “string”); echo gettype($var2);

(36)

• Another way of switching the type

• <?php

$var1 = “2 brown tigers”; $var2 = $var1 + 3;

echo $var2;

?> <br/>

<?php

$var3 = (int) $var1;

echo gettype($var1);

(37)

• <?php echo is_array($var1); ?><br/>

• <?php echo is_bool($var1); ?><br/>

• <?php echo is_float($var1); ?><br/>

• <?php echo is_int($var1); ?><br/>

• <?php echo is_null($var1); ?><br/>

• <?php echo is_numeric($var1); ?><br/>

(38)

CONSTANTS

• <?php

$max_width = 980;

define(“MAX_WIDTH”, 980);

echo MAX_WIDTH; echo “<br />”

// MAX_WIDTH += 1; // echo MAX_WIDTH;

$max_width += 1; echo $max_width;

?>

Error: Cannot apply += to constant. Because

MAX_WIDTH is fixed and unchangeable

It should be in all caps, and shouldn’t have $ sign

(39)

SuperGlobal Arrays

• In PHP, there is pre-defined array variables

present that is called superglobals.

• It means its data you can get any part of

(40)

Array Details

$_GET Store data that is sent from URL as query string

$_POST Data from FORM fields store in this array

$_COOKIE Data from Cookie store in this array e.g. you login to

hotmail.com, you provide username and password below it there is a checkbox and when you check this checkbox your username and password stored in cookie, next time to log on you don’t need to provide username and password it

retrieve from cookie.

$_FILES When through POST method we upload any file its information stored in this array

$_SESSION

$_REQUEST Data in $_POST, $_GET, $_COOKIE store in this array, 3 in 1.

(41)

$_POST

• We use post method in a form. When we

submit the button of form, all the data of

form is send to $_POST array.

<form method=“post” action=“process.php”>

• We get this data on any other page from this

(42)

EXAMPLE

• Myform.php <body>

<form method=“post” action=“dataform.php”> <input type=“text” name=“txtname” />

<input type=“submit” value=“Say Hello” name=“sbtbtn” /> </form>

</body>

• dataform.php <body>

<?php

$Name = $_POST[‘txtname’]; echo “Hello” . $Name;

?>

(43)

$_GET

The data that we send through URL also known as

query string is stored in this super global array $_GET.

http://www.example.com/search.php?keyword=pakistan

• Query string started in URL after ? Sign.

• In a URL there may be more than one query string.

http://www.example.com/search.php?

(44)

EXAMPLE

• link.php

<body>

<a href=“get.php?site=ITDunya”>Sending QueryString</a>

</body>

• get.php

<body> <?php

$siteName = $_GET[‘site’];

echo “Welcome to ” . $siteName; ?>

(45)

$_FILES

• When we upload a file or an image to

server, it stores in $_FILES array.

• Example:

• User uploads a file from his computer c:\

docs\project.zip

• Its size is 20,000 bytes

• PHP store in a temp folder, and gives it a

(46)

• C:\docs\project.zip : $_FILES[userfile][name]

20000 : $_FILES[userfile][size]

(47)

• $file_name = $_FILES[‘userfile’][‘name’];

• $file_tmp_name = $_FILES[‘userfile’][‘tmp_name’]; • $file_size = $_FILES[‘userfile’][‘size’];

• $file_type = $_FILES[‘userfile’][‘type’];

• Because the file is uploaded on a

temporary folder, that is why through this

function move_uploaded_file() we copy it

(48)

EXAMPLE

• Upload_form.php

<html> <head>

<title>Upload File</title> </head>

<body>

<form method=“post” action=“upload.php”

enctype=“multipart/form-data”> <input type=“file” name=“userfile” />

<input type=“submit” value=“Upload” name=“action” /> </form>

(49)

• Upload.php <?php

$upload_dir = “./user_files”;

$file_name = $_FILES[‘userfile’][‘name’];

$file_tmp_name = $_FILES[‘userfile’][‘tmp_name’]; $file_size = $_FILES[‘userfile’][‘size’];

$file_type = $_FILES[‘userfile’][‘type’];

if($file_size <= 0){

die(“cannot upload empty file”); }

if(is_uploaded_file($_FILES[‘userfile’][‘tmp_name’])){

if(!move_uploaded_file($file_tmp_name, “$upload_dir/$file_name”)){ die(“cannot copy $file_name”);

} }else{

die(“possible file upload attack”); }

References

Related documents

Multi-Trip Claims: This makes it easier to enter details of a journey with multiple legs, encouraging staff to include more detailed information within their expense claims..

Next time when you sign in Google services (using HSMC account), you will be prompted for the “second factor” in addition to your username and password?. Now you can use the mobile

In Username and Password, enter your Username and Password provided to you by your Server (Created in NAVAN while adding PPTP member i.e. User Name as test and Password as

If you forget your username or password, you could reset the camera to factory settings to get the default username and password.

When adding computers to a list, Apple Remote Desktop will challenge you for a username and password of an administrator on the computer.. Type in your administrator

If you have an Office 365 Exchange email account in Server, enter outlook.office365.com, and then add your Username and Password.. Username : MyMail Email

You will next be asked for your username &amp; password, this will be the same that you have used to login previously – and should be formatted as (TROPICALCLOUD\*username*).

• Enter your e-mail address and click on “Forgot Username or Password” button to have your username and a temporary password sent to you.. • The temporary password will need to