1
CS1113 Web Programming
Lecture 19
Data Types
• Variable is a symbolic representation of a
value. Its change over time or vary.
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
Example
Variable Names
• $item • $Item
• $myVariable • $this_variable • $this-variable • $product3
• $_book
• $__bookPage
EXAMPLES
• <?php$var1 = 10;
echo $var1;
?>
• <?php
$var2 = “Hello World”;
echo $var2;
EXAMPLE OF CASE-SENSITIVE
• <?php$my_variable = “Hello World”;
$my_Variable = “Hello World Again”;
echo $my_Variable;
?>
• Output:
EXAMPLE
• <?php
$var1 = 10;
echo $var1;
echo “<br/ >”; $var1 = 100;
echo $var1;
?>
• Output: 10
STRINGS
• <?phpecho “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
EXAMPLE
• We can use variable in double quotes as well.
• <?php
$my_variable = “Hello World”;
?>
• <?php
echo “$my_variable Again.”;
?>
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
• <?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:
• 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); ?>
MORE STRING FUNCTIONS
• Repeat:
str_repeat($thirdString, 2);
• Make substring:
substr($thirdString, 5, 10);
• Find position:
strpos($thirdString, “brown”);
• Find character:
Numbers
• <?php$var1 = 3; $var2 = 4;
?>
Basic Math:
<?php echo ((1 + 2 + $var1)* var2) / 2 -5; ?>
<br />
EXAMPLE
• <?php
$var1 = 3; $var2 = 4;
?>
+=: <?php
$var2 += 4;
echo $var2;
?>
• Output: +=: 8
You can add any value and
• +=: <?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
INCREMENT & DECREMENT
• Increment by 1.
Increment: <?php $var2++; echo $var2; ?>
• Decrement by 1.
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
FLOATING POINT FUNCTIONS
Absolute Value: abs(0 - 300);
Exponential: pow(2, 8);
Square Root: sqrt(100);
Modulo: fmod(20, 7);
Random (any): rand();
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
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
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
• <?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
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); ?>
• 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,
• In array: <?php
echo in_array(3, $array1);
?>
This function help us to find a
particular string or value in our array.
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
• <?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
• <?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
• <?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/>
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;
?>
• <?php
$var1 = “2 brown tigers”; $var2 = $var1 + 3;
echo $var2;
?>
• <?php
echo gettype($var1); echo “<br/>”;
echo gettype($var2);
?>
• How actually switch the type.
• <?php
$var1 = “2 brown tigers”; $var2 = $var1 + 3;
echo $var2;
?> <br/>
• <?php
settype($var2, “string”); echo gettype($var2);
• Another way of switching the type
• <?php
$var1 = “2 brown tigers”; $var2 = $var1 + 3;
echo $var2;
?> <br/>
<?php
$var3 = (int) $var1;
echo gettype($var1);
• <?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/>
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
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
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.
$_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
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;
?>
$_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?
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; ?>
$_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
• C:\docs\project.zip : $_FILES[userfile][name]
20000 : $_FILES[userfile][size]
• $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
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>
• 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”); }