PHP Primitive and Compound Types: Week2
PHP Primitive and Compound Types: Week2
PHP Primitive and Compound Types: Week2
Variables in PHP
Getting started with PHP
Presentation Title 2
Rules in Variable Declaration
2020
Variables in PHP
// Example
$num = 5;
$num = $num + 5;
echo $num; > 10
// Another example
$a = 5;
$b = 5;
echo ($a + $b); > 10
Presentation Title 3
Rules in Variable Declaration
2020
Variables in PHP
// Modifying Variables
$Total = 10;
echo “<p>Total is $$Total</p>”;
// This will enclose in a paragraph tag > Total is $10
$Total = 150;
echo “<p>New total is $$Total</p>”; > New total is $150
Presentation Title 4
Working with Data Types
2020
Variables in PHP
Presentation Title 5
Numeric Data Types
2020
Variables in PHP
• PHP supports two numeric data types:
• An integer is a positive or negative number with no decimal places (-250, 2, 100, 10,000)
Presentation Title 6
Boolean Values
2020
Variables in PHP
• A Boolean value is a value of true or false
• It decides which part of a program should execute and which part should compare data
• In PHP programming, you can only use true or false
• In other programming languages, you can use integers such as 1 = true, 0 = false
Presentation Title 7
Dynamic Typing
2020
Variables in PHP
Presentation Title 8
Arrays
2020
Variables in PHP
• An array contains a set of data represented by a single variable name
Pasay
Quezon
San Juan $cities[]
array name
Marikina
Caloocan
Array data
Presentation Title 9
Arrays
2020
Variables
Presentation Title 10
Arrays
2020
Variables in PHP
Presentation Title 11
Arrays
2020
Variables in PHP
• count($array) function
• Use the count() function to find the total number of elements in an array
Presentation Title 12
Arrays
2020
Variables in PHP
// count() function finds the total
number of elements in an array
Presentation Title 13
Arrays
2020
Variables in PHP
// print_r(), var_export(), and
var_dump()
Presentation Title 14
Arrays
2020
Variables in PHP
• Modifying elements in an array is possible.
• Include the index for an individual element of the array:
$HospitalDepts = array(
"Anesthesia", // first element(0)
"Molecular Biology", // second element (1)
"Neurology"); // third element (2)
• To change the first array element in the $HospitalDepts[] array from “Anesthesia” to “Anesthesiology” use:
$HospitalDepts[0] = "Anesthesiology";
Presentation Title 15
Arrays
2020
Variables in PHP
$numbers[2] = 100;
print_r($numbers); > Array([0] => 10 [1] => 15 [2] => 100
[3] => 25 [4] => 30)
Presentation Title 16
2020
Computer Laboratory 2
PHP Primitive and Construction of an expression
Presentation Title 17
1
2020
Computer Laboratory 2
• Write a PHP program to compute the sum of the two given integer values. If the two values are the same, then
returns triple their sum
• Sample Input
• 1, 2
• 3, 2
• 2, 2
• Sample Output:
• 3
• 5
• 12
Presentation Title 18
2
2020
Computer Laboratory 2
• Write a PHP program to get the absolute difference between n and 51. If n is greater than 51 return triple the
absolute difference.
• Sample Input:
• 53
• 30
• 51
• Sample Output:
• 6
• 21
• 0
Presentation Title 19
3
2020
Computer Laboratory 2
• Write a PHP program to check two given integers, and return true if one of them is 30 or if their sum is 30.
• Sample Input:
• 30, 0
• 25, 5
• 20, 30
• 20, 25
• Sample Output:
• bool(true)
• bool(true)
• bool(true)
• bool(false)
Presentation Title 20
4
2020
Computer Laboratory 2
• Write a PHP program to exchange the first and last characters in a given string and return the new string.
• Sample Input:
• "abcd“
• "a“
• "xy“
• Sample output:
• dbca
• a
• yx
Presentation Title 21
5
2020
Computer Laboratory 2
• Write a PHP program to check if the value of each element is equal or greater than the value of previous element of
a given array of integers.
• Sample Input:
• { 5, 5, 1, 5, 5 }
• { 1, 2, 3, 4 }
• { 3, 3, 5, 5, 5, 5}
• { 1, 5, 5, 7, 8, 10}
• Sample Output:
• bool(false)
• bool(true)
• bool(true)
• bool(true)
Presentation Title 22
1
2020
Computer Laboratory 2
• Write a PHP program to compute the sum of the two given integer values. If the two values are the same, then
returns triple their sum
• Sample Input
• 1, 2
• 3, 2
• 2, 2
• Sample Output:
• 3
• 5
• 12
Presentation Title 23
2
2020
Computer Laboratory 2
• Write a PHP program to get the absolute difference between n and 51. If n is greater than 51 return triple the
absolute difference.
• Sample Input:
• 53
• 30
• 51
• Sample Output:
• 6
• 21
• 0
Presentation Title 24
3
2020
Computer Laboratory 2
• Write a PHP program to check two given integers, and return true if one of them is 30 or if their sum is 30.
• Sample Input:
• 30, 0
• 25, 5
• 20, 30
• 20, 25
• Sample Output:
• bool(true)
• bool(true)
• bool(true)
• bool(false)
Presentation Title 25
4
2020
Computer Laboratory 2
• Write a PHP program to exchange the first and last characters in a given string and return the new string.
• Sample Input:
• "abcd“
• "a“
• "xy“
• Sample output:
• dbca
• a
• yx
Presentation Title 26
5
2020
Computer Laboratory 2
• Write a PHP program to check if the value of each element is equal or greater than the value of previous element of
a given array of integers.
• Sample Input:
• { 5, 5, 1, 5, 5 }
• { 1, 2, 3, 4 }
• { 3, 3, 5, 5, 5, 5}
• { 1, 5, 5, 7, 8, 10}
• Sample Output:
• bool(false)
• bool(true)
• bool(true)
• bool(true)
Presentation Title 27
Rubrics
2020
Evaluated by: Date:
Presentation Title 28
Printed Name over Signature of Faculty Member
2020