PHP Flashcards

1
Q

What are the three main areas that PHP is used?

A

Server-side scripting, Command line scripting, and writing desktop applications

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

How to comment in PHP docs?

A
// one line c++ style comment
# one-line shell-style comment
/* multi-line 
comments */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain how PHP tags work

A

ends php script,
if there is no HTML following last line of PHP code, PHP closing tag can be omitted. (HTML can be written directly in a PHP doc)

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

How many primitive datatypes does PHP have

A

10

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

Name the four scalar types.

A

bool, int, float (double), string

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

Name the four compound types.

A

array, object, callable, iterable

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

Name the two special types.

A

resource, null

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

How are PHP variable types different from other languages?

A

variable types are not determined by the programmer but are decided at runtime by PHP

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

How are variables represented?

A

Variable start with a $, followed by any letter or underscore, followed by any mix of letters, numbers, and underscores

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

What are constants and how can they be defined?

A

Constants are variables with values that can not change. They can be declared with either the const keyword or by calling define(); Only the four scalar types can be assigned to variables with the const keyword

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

What are +$a and -$a?

A

+$a is the identity operator being applied to a variable called $a. This converts $a into an appropriate int/float. -$a is the negate operator being applied to a variable called $a. This represents the opposite of $a.

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

What is a ternary operator in PHP?

A

Same as in JavaScript. expression ? “this if true” : “else, this if not true”

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

When does a condition get evaluated in a while loop?

A

Before each iteration.

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

How is a do-while loop different from a while loop?

A

The condition gets checked at the end of each iteration. This ensures that the loop runs at least 1 time.

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

Explain for loop syntax in PHP

A

for ( expression 1; express 2; express 3) {code block}

for($i=0; $i < 10; $i++) {
echo $i;
}

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

What are foreach loops used for?

A

foreach statements are used for iterating over arrays or objects. foreach($array as $value) {} or foreach($array as $key => $value){};

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

What does adding a number after a break do? (ex: break 2;)

A

Breaks can except this optional numeric argument which tells it how many nested enclosing structures are to be broken out of. break 2 will exit 2 levels of loops.

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

Explain continue

A

continue is used to skip the rest of the current loop iteration and begin again at the next condition check to the next iteration. Like break, continue can accept an optional numeric argument.

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

What is require? How does it work?

A

require includes and evaluates the specified file it was called with. (Think importing in JavaScript). Require is different from includes in that require will produce a fatal E_compile_error which will halt the script.

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

What is include? How does it work? How is it different from require?

A

Include includes and evaluates the specified file it was called with, just like require. However, unlike require, include will emit an E_warning errorif it can not find the file. This will not halt the script. The program can continue running.

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

Explain require_once and include_once.

A

Both do the same as require and include, however, PHP will check to see if the file has already been included and will not include/require again if so

22
Q

Explain anonymous function in PHP

A

Also known as closures, Anonymous functions are mostly used for callbacks (passing a function as an argument). Anonymous functions don’t have names (ex: function ($a, $b) {}). Anonymous functions are made possible by the closure class.

23
Q

Explain arrow functions and when were they introduced?

A

Arrow functions are anonymous function syntax that was introduced in PHP 7.4.

24
Q

Explain classes in PHP

A

Classes are object models with added features such as visibility, abstract, final classes and methods.

25
Q

How does $this work in PHP

A

When a method of a class is called, $this refers to the calling object

26
Q

What are __contruct() and __destruct()

A

__construct() is a method that is used to initialize a new instance of a class object. When the new operator is called with a class, the __contruct() method is called to initialize the new object. The __destruct() method gets called as soon as there are no other references to a particular object.

27
Q

What is property visibility?

A

Classes allow you to set the visibility of a variable with either, public, private, or protected.

28
Q

What are superglobals?

A

Super globals are built-in variables that can be accessed from any scope.

29
Q

What is $GLOBALS?

A

$GLOBALS references all variable available in a global scope

30
Q

What is $_SERVER?

A

$_SERVER is an array containing information about the server and execution environment. The array can include information such as headers, paths, and script locations. The entries in this array are created by the server and are not always guaranteed to provide any of the entries. See PHP documentation for a list of all possible entries.

31
Q

What is $_GET?

A

$_GET provides an associative array of variables passed to the script from the URL parameters (query string). (HTTP get variables)

32
Q

What is $_POST?

A

$_POST provides HTTP post variables in the form of an associative array. These are passed to the current script by using the HTTP post method on form controls.

33
Q

What is $_FILES?

A

$_FILES gives file upload variables. Returns an associative array of items uploaded to the script via the HTTP post method

34
Q

What is $_COOKIE?

A

An associative array of variables passed to the current script via HTTP Cookies.

35
Q

What is $_SESSION?

A

An associative array containing session variables available to the current script.

36
Q

What is $_REQUEST?

A

HTTP request variables. An associative array that by default contains the contents of $_GET, $_POST, and $_COOKIE.

37
Q

What is $_ENV?

A

An associative array of variables passed to the current script via the environment method.

38
Q

list all PHP data types.

A

String, Boolean, integer, floating-point number, Object, Array, Null

39
Q

Explain array()

A

Creates an array. Accepts are values as arguments and returns an array of the arguments.

40
Q

Explain array_keys()

A

Returns an array of all keys in an array if no other parameters are specified other than the search array.

41
Q

Explain array_reverse()

A

Returns a new reversed array.

42
Q

Explain array_search()

A

Searches an array for a search value. Returns the key of the search value if the value exists in the array. Otherwise returns false

43
Q

Explain array_slice()

A

Returns a sequence of elements from an array

44
Q

Explain array_push() & array_pop()

A

push: adds value to the beginning of the array (returns the new number of elements in the array).
pop: removes and returns the last value in the array.

45
Q

Explain count()

A

returns the number of elements in an array

46
Q

Explain indexed vs associative arrays

A

indexed arrays use numeric values for keys whereas associative array keys are named (strings).

47
Q

Explain explode()

A

Breaks up a string into an array based on a substring. Similar to string.split(“”) in javascript.

48
Q

Explain str_split()

A

Breaks a string into a character array. The length of each chunk can be specified with a second parameter.

49
Q

.htaccess vs php.ini

A

.htaccess is a configuration file for an apache server.

php.ini is a file for making changes to PHP default settings.

50
Q

How to do connect to MySQL database in a PHP script.

A

connect with MySQL database by using mysqli_connect.

51
Q

What are some common errors types in PHP?

A

Notices, warnings, and fatal errors

52
Q

explain strlen();

A

Returns the length of a string