0% found this document useful (0 votes)
51 views29 pages

PHP Primitive and Compound Types: Week2

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 29

2020

PHP Primitive and Compound Types


week2
2020

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

$integer_num = 10; // Integer value


$float_num = 3.14; // Float value
$string_var = “Hello World”; // String value
$bool_val = false; // Boolean value
$null_val = NULL; // Null declaration

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)

• A floating-point number is a number that contains decimal places or that is written in


exponential notation (-6.16, 3.17, 2.7541)
• Exponential notation, or scientific notation, is short for writing very large numbers or numbers with
many decimal places (2.0e11)

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

$variable = 10; // Integer value


$variable = 3.14; // Float value
$variable = “Hello World”; // String value
$variable = false; // Boolean value
$variable = NULL; // Null declaration

// $variable now is a null declaration

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

$numbers = [10, 15, 20, 25, 30];


$grades = [89.50, 90.75, 99.25, 100.00];
$cities = [“Quezon City”, “San Juan City” , “Caloocan City”];

Presentation Title 10
Arrays
2020

Variables in PHP

$numbers = [10, 15, 20, 25, 30];


$grades = [89.50, 90.75, 100.00];
$cities = [“Quezon”, “Caloocan”];
echo $numbers[0]; > 10
echo $numbers[4]; > 30
echo $numbers[5]; > Error
echo $cities[1]; > Caloocan

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

• print_r($array), var_export($array), and var_dump($array) functions


• Use to print or return information about variables
• Most useful with arrays because they print the index and value of each element

Presentation Title 12
Arrays
2020

Variables in PHP
// count() function finds the total
number of elements in an array

$numbers = [10, 15, 20, 25, 30];


echo count($numbers); > 5

$grades = [89.50, 90.75, 100.00];


echo count($grades); > 3

Presentation Title 13
Arrays
2020

Variables in PHP
// print_r(), var_export(), and
var_dump()

$numbers = [10, 15, 20, 25, 30];


print_r($numbers); > Array([0] => 10 [1] => 15 [2] => 20
[3] => 25 [4] => 30)

$grades = [89.50, 90.75, 100.00];


var_dump($grades);
> Array([0] => 89.50 [1] => 90.75 [2] =>
100.00)

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 = [10, 15, 20, 25, 30];


print_r($numbers); > Array([0] => 10 [1] => 15 [2] => 20
[3] => 25 [4] => 30)

$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

Student Name: Professor:


Student Number: Date:
Rating
Criteria Beginner Developing Acceptable Exemplary Score
1 2 3 4
Correctness All of the expected functionalities Most of the expected The file is able to do the expected The file is able to do functions  
of the file were not working functionalities of the file were functions based on the problem to correctly beyond what is expected
correctly. not working correctly. solve correctly. based on the problem to solve.
Time Bound No submission The activity was submitted one The activity was submitted one day The activity was submitted before  
week after the deadline after the deadline the deadline
Completeness No procedure was found in the Some of the procedures were Most of the procedures were completed Completed all procedures stated.  
activity completed
Other Comments/Observations:  
Total Score
 
 
 
  Rating = (Total Score/12) x 100%

 
Evaluated by: Date:
 
Presentation Title 28
Printed Name over Signature of Faculty Member
2020

You might also like