Oop Reviewer
Oop Reviewer
PHP Comments
A comment in PHP code is a line that is not executed
as a part of the program. Its only purpose is to be
read by someone who is looking at the code.
Comments can be used to:
A PHP file normally contains HTML tags, and some • Let others understand your code
PHP scripting code. • Remind yourself of what you did - Most
Below, we have an example of a simple PHP file, programmers have experienced coming back to
with a PHP script that uses a built-in PHP function their own work a year or two later and having to re-
“echo” to output the text “Hello World!” on a web figure out what they did. Comments can remind you
page: of what you were thinking when you wrote the code
Syntax for single-line comments:
Output Variables
The PHP echo statement is often used to output
data to the screen. The following example will show A variable declared within a function has a LOCAL
how to output text and a variable: SCOPE and can only be accessed within that
function:
Arithmetic Operators
There are following arithmetic operators supported Conditional Operator
by PHP language. There is one more operator called conditional
Assume variable A holds 10 and variable B holds 20. operator. This first evaluates an expression for a true
or false value and then execute one of the two given
statements depending upon the result of the
evaluation. The conditional operator has this
syntax:
Comparison Operators
Operators Categories
There are following comparison operators
All the operators we have discussed above can be
supported by PHP language.
categorized into following categories:
Assume variable A holds 10 and variable B holds 20.
• Unary prefix operators, which precede a single
operand.
• Binary operators, which take two operands and
perform a variety of arithmetic and
logical operations.
• The conditional operator (a ternary operator),
which takes three operands and
evaluates either the second or third expression,
depending on the evaluation of the
first expression.
• Assignment operators, which assign a value to a
variable.
Precedence of PHP Operators
Logical Operators
Operator precedence determines the grouping of
There are following logical operators supported by
terms in an expression. This affects how
PHP language.
an expression is evaluated. Certain operators have
Assume variable A holds 10 and variable B holds 20.
higher precedence than others; for
example, the multiplication operator has higher
precedence than the addition operator �
For example x = 7 + 3 * 2; Here x is assigned 13, not
20 because operator * has higher
precedence than + so it first get multiplied with 3*2
and then adds into 7.
Here operators with the highest precedence appear • GET can’t be used to send binary data, like images
at the top of the table, those with the or word documents, to the server.
lowest appear at the bottom. Within an expression, • The data sent by GET method can be accessed
higher precedence operators will be using QUERY_STRING environment
evaluated first. variable.
• The PHP provides $_GET associative array to
access all the sent information using
GET method.
Try out following example by putting the source
code in a PHP script.
Syntax:
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Syntax:
for (initialization; condition; increment){
code to be executed;
}
The foreach loop statement
The foreach statement is used to loop through
arrays. For each pass the value of the current array
element is assigned to $value and the array pointer
is moved by one and in the next pass next element
will be processed.
Syntax:
foreach (array as value) {
code to be executed;
}
Example. This example decrements a variable value Example. Try out following example to list out the
on each iteration of the loop and the counter values of an array.
increments until it reaches 10 when the evaluation
is false and the loop ends.
The continue statement There are three different kind of arrays and each
The PHP continue keyword is used to halt array value is accessed using an ID c which
the current iteration of a loop but it does not is called array index.
terminate the loop.
Just like the break statement the continue • Numeric array - An array with a numeric index.
statement is situated inside the statement block Values are stored and accessed in
containing the code that the loop executes, linear fashion.
preceded by a conditional test. For the pass • Associative array - An array with strings as index.
encountering continue statement, rest of the loop This stores element values in
code is skipped and next pass starts. association with key values rather than in a strict
linear index order.
• Multidimensional array - An array containing one
or more arrays and values are
accessed using multiple indices
Numeric Array
These arrays can store numbers, strings and any
object but their index will be represented by
numbers. By default array index starts from zero.
Associative Arrays
The associative arrays are very similar to numeric
arrays in term of functionality but they are different
in terms of their index. Associative array will have
their index as string so that you can establish a
strong association between key and values. To store
the salaries of employees in an array, a numerically
indexed array would not be the
best choice. Instead, we could use the employees Creating PHP Function
names as the keys in our associative array, and the Its very easy to create your own PHP function.
value would be their respective salary. Suppose you want to create a PHP function which
*NOTE: Don’t keep associative array inside double will simply write a simple message on your browser
quote while printing otherwise it would not return when you will call it. Following example creates a
any value. function called writeMessage() and then calls it just
after creating it.
Note that while creating a function its name should
start with keyword function and all the PHP code
should be put inside { and } braces as shown in the
following example below
Multidimensional Arrays
A multi-dimensional array each element in the main
array can also be an array. And each element in the
sub-array can be an array, and so on. Values in the PHP Functions with Parameters
multi-dimensional array are accessed using PHP gives you option to pass your parameters inside
multiple index. a function. You can pass as many as parameters
Example. In this example we create a two- your like. These parameters work like variables
dimensional array to store marks of three students inside your function. Following example takes two
in three subjects This example is an associative integer parameters and add them together and then
array, you can create numeric array in the print them.
same fashion.
Short Review
Which keyword is used A. Function
10/9/2023 2
Which keyword is used A. Function
10/9/2023 3
You are designing a PHP A. Private
application with multiple B. Protected
classes, and you want to control C. Public
the visibility of certain class D. Static
properties and methods. To
achieve this, you decide to use
access modifiers. Which access
modifier should you choose to
restrict a class property or
method to be accessible only
within its own class and derived
classes?
10/9/2023 4
You are designing a PHP A. Private
application with multiple B. Protected
classes, and you want to control C. Public
the visibility of certain class D. Static
properties and methods. To
achieve this, you decide to use
access modifiers. Which access
modifier should you choose to
restrict a class property or
method to be accessible only
within its own class and derived
classes?
10/9/2023 5
You’re debugging a PHP A. Current instance of the
codebase, and you encounter class
the ‘this’ keyword within a class. B. Static methods of the
Your team member asks you to class
clarify its reference. In this C. Parent class
scenario, what does the ‘this’ D. Abstract class
keyword typically refer to within
a class?
10/9/2023 6
You’re debugging a PHP A. Current instance of the
codebase, and you encounter class
the ‘this’ keyword within a class. B. Static methods of the
Your team member asks you to class
clarify its reference. In this C. Parent class
scenario, what does the ‘this’ D. Abstract class
keyword typically refer to within
a class?
10/9/2023 7
What is the output of the sample A. The fruit is Apple.
program?
B. The Fruit is Apple.
C. Fatal error: Class Fruit
cannot extend final class
Fruit2 in
C\xampp\htdocs\ex\loop
2.php on line 13
D. None of the choices
10/9/2023 8
What is the output of the sample A. The fruit is Apple.
program?
B. The Fruit is Apple.
C. Fatal error: Class Fruit
cannot extend final class
Fruit2 in
C\xampp\htdocs\ex\loop
2.php on line 13
D. None of the choices
10/9/2023 9
You are working on a PHP A. $date =
project where you need to date_create(‘now’);
manipulate and display dates.
B. $date = date_create(“m-
You want to create a DateTime d-Y”);
object to represent a specific
C. $date =
date and time. Which of the date_create(date(“m/d/Y
following options demonstrates ”));
the correct usage of date_create
D. $date = date_create();
to get the present datetime?
10/9/2023 10
You are working on a PHP A. $date =
project where you need to date_create(‘now’);
manipulate and display dates.
B. $date = date_create(“m-
You want to create a DateTime d-Y”);
object to represent a specific
C. $date =
date and time. Which of the date_create(date(“m/d/Y
following options demonstrates ”));
the correct usage of date_create
D. $date = date_create();
to get the present datetime?
10/9/2023 11
You have created a class named
User with variables: username A. $user1->username=“cliffsama”;
and password set as private, with $user2->username=“rebansome”;
getter/setter methods. You $user1->password=“mypeasants”;
instantiated the class as objects
named user1 and user2. Which $user2->password=“kneelbeforeme”;
of the following options B. $username=$this->username;
represents the correct syntax for
$password=$this->password;
setting the value of the variables
of the class inside the methods? C. $this->username=$username;
$this->password=$password;
D. $user1->set_username(“kneelbeforeme”);
$user2->set_username(“mypeasants”);
$user1->set_password(“mypeasants”);
$user2->set_password(“kneelbeforeme”);
10/9/2023 12
You have created a class named
User with variables: username A. $user1->username=“cliffsama”;
and password set as private, with $user2->username=“rebansome”;
getter/setter methods. You $user1->password=“mypeasants”;
instantiated the class as objects
named user1 and user2. Which $user2->password=“kneelbeforeme”;
of the following options B. $username=$this->username;
represents the correct syntax for
$password=$this->password;
setting the value of the variables
of the class inside the methods? C. $this->username=$username;
$this->password=$password;
D. $user1->set_username(“kneelbeforeme”);
$user2->set_username(“mypeasants”);
$user1->set_password(“mypeasants”);
$user2->set_password(“kneelbeforeme”);
10/9/2023 13
Sample Footer Text
* *****
** ****
*** ***
**** **
***** *
B. D.error
*****
*****
***
**
10/9/2023 14
Sample Footer Text
* *****
** ****
*** ***
**** **
***** *
B. D.error
*****
*****
***
**
10/9/2023 15
A ______ is a way to A. $_SESSION
store information (in B. $_POST
variables) to be used C. $_REQUEST
across multiple pages. D. $_GET
10/9/2023 16
A ______ is a way to A. $_SESSION
store information (in B. $_POST
variables) to be used C. $_REQUEST
across multiple pages. D. $_GET
10/9/2023 17
A _____ statement A. Printf
can be used to get B. Echo
output and returns a C. Write
value of 1. D. print
10/9/2023 18
A _____ statement A. Printf
can be used to get B. Echo
output and returns a C. Write
value of 1. D. print
10/9/2023 19
What will be printed A. Apple banana mango grape
to the screen when B. Grape mango banana apple
executing the code C. Banna apple grape mango
snippets below? D. Mango banana grape apple
$fruit=[“apple”, “banana”,
“mango”, “grape”];
foreeach($fruits as $fruit){
echo $fruit . “ “;
}
10/9/2023 20
What will be printed A. Apple banana mango grape
to the screen when B. Grape mango banana apple
executing the code C. Banna apple grape mango
snippets below? D. Mango banana grape apple
$fruit=[“apple”, “banana”,
“mango”, “grape”];
foreeach($fruits as $fruit){
echo $fruit . “ “;
}
10/9/2023 21