Upload
hector-norman
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
1
CS428 Web EngineeringLecture 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.
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
EXAMPLE• <?php
$var1 = 10;
echo $var1;
echo “<br/ >”;
$var1 = 100;
echo $var1;
?>
• Output: 10
100
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
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”;
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
• <?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); ?>
• 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
MORE STRING FUNCTIONS• Repeat:
str_repeat($thirdString, 2);
• Make substring:
substr($thirdString, 5, 10);
• Find position:
strpos($thirdString, “brown”);
• Find character:
strchr($thirdString, “z”);
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
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
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.
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
ADD/UPDATE THE VALUE OF ARRAY • <?php
$array2[3] = “cat”;
?>
<br />
• <?php
echo $array2[3];
?>
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
• <?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
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); ?>
• 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 *
• 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
• <?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.
• <?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.
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.