PHP Set 7 Flashcards
What variables are used to collect form data?
The $_GET and $_POST superglobal variables
How do you specify how a form is to be processed when submitted?
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
When should you use GET?
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
When should you use POST?
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 do you get the filename of the currently executing script?
Using $_SERVER[“PHP_SELF”]
What does htmlspecialchars() do and why should you use it?
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
What does the trim function do and what is its syntax?
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
What does the stripslashes function do and what is the syntax?
Unquotes a quoted strong and removes backslashes
Syntax:
stripslashes(string $string): string
What are the advantages of object oriented programming over procedural programming?
- Provides a clearer structure for the program
- Helps keep the PHP code DRY
- Makes the code easier to maintain
How do you instantiate an object of a class?
Using the keyword ‘new’
Ex: $object = new Object();
What is the $this keyword?
References the current object of the class which allows you to access the properties and methods of the current instantiation of the object
What operator is used to access class methods and functions of an object?
Using the object operator (->)
Ex: $this->name = “Me”
How do you check if an object belongs to a specific class?
Using the instanceof keyword
Ex: if $apple instanceof Fruit;
How do you define a constructor method for a class?
Using the __construct() function
What function is needed to destroy an instance of a class?
Using the __destruct() function. This is automatically called for all objects at the end of the script