You are on page 1of 49

Web Engineering

Lecture 19
Data Types
(PHP - II)

1
Variables
• 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

5) Case sensitive
Example
Variable Names
• $item
• $Item
• $myVariable
• Shouldn’t use as a variable starting
$this_variable with single underscore, because
• $this-variable PHP uses single underscore itself to
define certain types of variables.
• $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:
Hello World Again
EXAMPLE
• <?php
$var1 = 10;
echo $var1;
echo “<br/ >”;
$var1 = 100;
echo $var1;
?>

• Output: 10
100
STRINGS HTML tags
can also be
• <?php used in
echo “Hello World <br />”; strings
echo ‘Hello World <br />’;
$my_variable = “Hello World”;
echo $my_variable;
echo $my_variable . “ Again”;
?> We can also
concatenate
• Output: Hello World Again 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; Another way
echo $thirdString; of
?> concatenation
• Output: The quick brown fox jumped over the lazy
dog.
• <?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/> This function removes
extra white spaces
• 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:
strchr($thirdString, “z”);
• Visit www.php.net for more string functions.
Numbers
• <?php
$var1 = 3;
$var2 = 4;
?>
Basic Math:
<?php echo ((1 + 2 + $var1)* var2) / 2 -5; ?>
<br />

• Output: Basic Math: 7


EXAMPLE
• <?php
$var1 = 3;
$var2 = 4;
?>
+=: <?php
$var2 += 4;
echo $var2; You can add any
?> value and
change the
value of variable
• Output: +=: 8
• +=: <?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
*=: 12
/=: 3
INCREMENT & DECREMENT
• Increment by 1.
Increment: <?php $var2++; echo $var2; ?>

• Decrement by 1.
Decrement: <?php $var2--; echo $var2; ?>
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
FLOATING POINT FUNCTIONS
Absolute Value: abs(0 - 300);
Exponential: pow(2, 8);
Square Root: sqrt(100);
Modulo: fmod(20, 7);
Random (any): rand();
Random (min, max): rand(1, 10);
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” => “Jon”,
“last_name” => “Von”);
?>

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


• <?php echo $array3[“first_name”] . “ ” .
$array3[“last_name”]; ?> We have created a
key value pair. First
name is the key,
• Output: Jon Yasir is the value
Jon Von
• <?php $array3[“first_name”] = “Jon”; ?>

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


$array3[“last_name”]; ?> Print readable
command gives
contents of an array
• Output: Jon Von
in readable form
position wise
• <pre><?php print_r($array2); ?></pre>
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
BOOLEANS
• <?php true/false doesn’t
mean it’s a string.
$bool1 = true;
When we type true,
$bool2 = false; php understands its
?> functionality.
$bool1: <?php echo $bool1; ?><br/>
$bool2: <?php echo $bool2; ?><br/>

• Output: $bool1: 1
$bool2:
Through this function we are
• <?php asking, is the value of the
$var1 = 3; variable is set or not. It will
either return true or false.
$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.
This function will
unset the value of a
variable.
• <?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
• <?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/>
$var6 empty: <?php echo empty($var6); ?><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;
?>
• Output: 5
• <?php
$var1 = “2 brown foxes”;
$var2 = $var1 + 3;
echo $var2;
?>
<?php
echo gettype($var1); echo “<br/>”;
echo gettype($var2);
?>
gettype will tell us, which type we are working
with.
• How actually switch the type.
• <?php
$var1 = “2 brown foxes”;
$var2 = $var1 + 3;
echo $var2;
?> <br/>
• <?php
settype($var2, “string”);
echo gettype($var2);
?>
• Another way of switching the type
• <?php
$var1 = “2 brown foxes”;
$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/>
• <?php echo is_string($var1); ?><br/>
CONSTANTS
It should be in all caps,
• <?php and shouldn’t have $ sign
$max_width = 980; because it is constant.
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
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


the page.
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.
$_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


super global array $_POST.
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;
?>
</body>
$_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?
keyword=pakistan&city=isb
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;
?>
</body>
$_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
random name.
• C:\docs\project.zip : $_FILES[userfile][name]
20000 : $_FILES[userfile][size]
“application/x-zip-compressed” :
$_FILES[userfile][type]
• $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
in our own directory.
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>
</body>
</html>
• 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”);
}
echo “$file_name has been successfully uploaded.”;
?>

You might also like