23
1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

Embed Size (px)

Citation preview

Page 1: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

1

CS428 Web EngineeringLecture 19

Data Types (PHP - II)

Page 2: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

Variables Names1) Start with a $ sign.

2) Followed by a letter or underscore.

3) Can contain letters, numbers, underscores or dashes.

4) No spaces

5) Case sensitive• $item• $Item• $myVariable• $this_variable• $this-variable• $product3• $_book • $__bookPage

Shouldn’t use as a variable starting with single underscore, because PHP uses single underscore itself to define certain types of variables.

Page 3: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

EXAMPLES• <?php

$var1 = 10;

echo $var1;

?>• <?php

$var2 = “Hello World”;

echo $var2;

?>

Case Sensitivity with Variables• <?php

$my_variable = “Hello World”;

$my_Variable = “Hello World Again”;

echo $my_Variable;

?>• Output:

Hello World Again

Page 4: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

EXAMPLE• <?php

$var1 = 10;

echo $var1;

echo “<br/ >”;

$var1 = 100;

echo $var1;

?>

• Output: 10

100

Page 5: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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 a string and a variable

Page 6: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

EXAMPLE• We can use variable in double quotes as well.

• <?php

$my_variable = “Hello World”;

?>• <?php

echo “$my_variable Again.”;

?>

• But better way to do this is put variable in curly braces: echo “{$my_variable} Again”;

Page 7: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

String Functions• <?php

$firstString = “The quick brown fox”;

$secondString = “ jumped over the lazy dog”;

?>• <?php

$thirdString = $firstString;

$thirdString .= $secondString;

echo $thirdString;

?>• Output: The quick brown fox jumped over the lazy

dog.

Another way of

concatenation

Page 8: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• <?php $firstString = “The quick brown fox”; ?>

<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:

<?php echo ucwords($firstString); ?>

Page 9: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• 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); ?>

This function removes extra white spaces

Page 10: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

MORE STRING FUNCTIONS• Repeat:

str_repeat($thirdString, 2);

• Make substring:

substr($thirdString, 5, 10);

• Find position:

strpos($thirdString, “brown”);

• Find character:

strchr($thirdString, “z”);

Page 11: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

EXAMPLE• <?php

$var1 = 3;

$var2 = 4;

?>

<?php

$var2 += 4;

echo $var2;

?>• Output: 8• Increment by 1.

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

• Decrement by 1.

Decrement: <?php $var2--; echo $var2; ?>

You can add any value and

change the value of variable

Page 12: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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

Ceiling: 4

Floor: 3

Page 13: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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

Note: array’s position is starting from zero

The first pocket is zero.

Page 14: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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

y

Page 15: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

ADD/UPDATE THE VALUE OF ARRAY • <?php

$array2[3] = “cat”;

?>

<br />

• <?php

echo $array2[3];

?>

Page 16: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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

We have created a key value pair. First name is the key, Yasir is the value

Page 17: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• <?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 position wise

Page 18: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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); ?>

Reverse Sort: <?php rsort($array1);

print_r($array1); ?>

Page 19: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• 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, removed the *

Page 20: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• In array: <?php

echo in_array(3, $array1);

?>

This function help us to find a

particular string or value in our array.

It returns T/F

Page 21: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• <?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 either return true or false.

Page 22: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

• <?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

variable.

Page 23: 1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)

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.$_SERVER This array has data that server send to client including

webpage name, Server name, HTTP version, Remote IP address etc.