Professional Documents
Culture Documents
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.
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.”;
?>
• 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: +=: 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; ?>
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”);
?>
• 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”;
?>
• 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
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’];
$file_name = $_FILES[‘userfile’][‘name’];
$file_tmp_name = $_FILES[‘userfile’][‘tmp_name’];
$file_size = $_FILES[‘userfile’][‘size’];
$file_type = $_FILES[‘userfile’][‘type’];