Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)"— Presentation transcript:

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

2 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 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.

3 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

4 EXAMPLE <?php $var1 = 10; echo $var1; echo “ ”; $var1 = 100; echo $var1; ?> Output: 10 100

5 STRINGS <?php echo “Hello World ”; echo ‘Hello World ’; $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

6 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”;

7 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

8 Lower case: Upper case: Uppercase first-letter: Uppercase words:

9 Length: Trim: Find: Replace by String: This function removes extra white spaces

10 MORE STRING FUNCTIONS Repeat: str_repeat($thirdString, 2); Make substring: substr($thirdString, 5, 10); Find position: strpos($thirdString, “brown”); Find character: strchr($thirdString, “z”);

11 EXAMPLE <?php $var1 = 3; $var2 = 4; ?> <?php $var2 += 4; echo $var2; ?> Output: 8 Increment by 1. Increment: Decrement by 1. Decrement: You can add any value and change the value of variable

12 FLOATING POINT NUMBERS Floating Point: Round: Ceiling: Floor: Output: Floating Point: 3.14 Round: 3.1 Ceiling: 4 Floor: 3

13 ARRAYS You can think of an array is a variable, in which you can assign multiple values. Output: 8 Note: array’s position is starting from zero The first pocket is zero.

14 EXAMPLE <?php $array2 = array(6, “amjad”, “aslam”, array(“x”, “y”, “z”)); ?> Output: aslam Array y

15 ADD/UPDATE THE VALUE OF ARRAY <?php $array2[3] = “cat”; ?> <?php echo $array2[3]; ?>

16 ASSOCIATIVE ARRAY “Yasir”, “last_name” => “Naeem”); ?> <?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

17 <?php echo $array3[“first_name”]. “ ”. $array3[“last_name”]; ?> Output: Kashif Naeem Print readable command gives contents of an array in readable form position wise

18 ARRAY FUNCTIONS Count: Max value: Min value: Sort: Reverse Sort: <?php rsort($array1); print_r($array1); ?>

19 Implode: Implode function is used to separate the array by * to make a string. Explode: It does the reverse, it takes the string that we just created find every instance, removed the *

20 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

21 <?php $var1 = 3; $var2 = “cat”; ?> $var1 is set: $var2 is set: 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.

22 $var1 is set: $var2 is set: Output: $var1 is set: $var2 is set: 1 This function will unset the value of a variable.

23 ArrayDetails $_GETStore data that is sent from URL as query string $_POSTData from FORM fields store in this array $_COOKIEData 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. $_FILESWhen through POST method we upload any file its information stored in this array $_SESSION $_REQUESTData in $_POST, $_GET, $_COOKIE store in this array, 3 in 1. $_SERVERThis array has data that server send to client including webpage name, Server name, HTTP version, Remote IP address etc.


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

Similar presentations


Ads by Google