PHP Set 7 Flashcards

1
Q

What variables are used to collect form data?

A

The $_GET and $_POST superglobal variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you specify how a form is to be processed when submitted?

A

In the ‘action’ field on the <form> element you specify the name of a php script that will process the form at the server when it is submitted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When should you use GET?

A

Only when sending non-sensitive information as all variable names and values are specified in the URL. Also only when you need to send a smaller amount of data as GET only lets you send a maximum of 2048 characters.
Also only when sending purely ascii data as you cant send other types of data over GET.
It is typically used to retrieve data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When should you use POST?

A

When sending sensitive information since the information in the form is embedded in the body of the HTTP request and not put in the URL. Also when sending things that aren’t purely ASCII data as it can allow binary data as well. Also when sending large amounts of information since it doesn’t have the 2048 character cap of GET.
It is typically used for data insertion and data updates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you get the filename of the currently executing script?

A

Using $_SERVER[“PHP_SELF”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does htmlspecialchars() do and why should you use it?

A

Converts special characters to HTML entities. It should always be used with $_SERVER[“PHP_SELF”] because the URL is visible to users. Because of this, an attacker could inject code to the form by encoding JS scripts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the trim function do and what is its syntax?

A

trim strips whitespace or other characters from the beginning and end of a string
Syntax:
trim(string $string, string $chars=”\n\r\t\v\0”): string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the stripslashes function do and what is the syntax?

A

Unquotes a quoted strong and removes backslashes
Syntax:
stripslashes(string $string): string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the advantages of object oriented programming over procedural programming?

A
  1. Provides a clearer structure for the program
  2. Helps keep the PHP code DRY
  3. Makes the code easier to maintain
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you instantiate an object of a class?

A

Using the keyword ‘new’
Ex: $object = new Object();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the $this keyword?

A

References the current object of the class which allows you to access the properties and methods of the current instantiation of the object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What operator is used to access class methods and functions of an object?

A

Using the object operator (->)
Ex: $this->name = “Me”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you check if an object belongs to a specific class?

A

Using the instanceof keyword
Ex: if $apple instanceof Fruit;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you define a constructor method for a class?

A

Using the __construct() function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What function is needed to destroy an instance of a class?

A

Using the __destruct() function. This is automatically called for all objects at the end of the script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 3 access levels you can specify for properties and methods?

A
  1. public is accessible everywhere. This is the default behaviour
  2. private is accessible only within the same class
  3. protected is accessible within the class and from subclasses derived from that class
17
Q

How do you declare a class constant and how do you reference it within a class function?

A

Declare using the const keyword
Ex: const MESSAGE=”Hello, world”

Access using the scope resolution operator (::)
Ex: echo self::MESSAGE

18
Q

What is the difference between using $this->member and self::$member

A

$this-> should be used for non-static members. self:: should be used for static members

self:: refers to the scope at the point of definition, not at the point of execution