2. Language Basics Flashcards

1
Q

What does Lexical Structure mean?

A

Set of basic rules that governs how you write in that language.

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

Which constructs are case-insensitive, and which are case-sensitive?

A

Case Insensitive – Classes & functions as well as built-in constructs and keywords.

Case Sensitive – Variables.

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

Is PHP Whitespace sensitive?

A

No, generally semi-colon is used for ends of lines and whitespace isn’t taken into account.

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

What are the multiple types of comments in PHP?

A

Single Line

  • //
  • #

Multi Line

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

What are the legal identifiers for variables?

A

Must begin with a dollar sign, first character must be ASCII letter (upper or lower) or an underscore. After the first character, 0-9 are also valid characters.

Valid

$bill

$head_count

$I_HEART_PHP

$_secret

Invalid

$not valid

$*w

$3a

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

How do you define/use a constant?

A

Need to call the define(‘CONST_NAME’, ‘VALUE) function. Can be used from then on my referring to it by name.

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

What are Keywords in PHP?

A

Reserved words for the languages core functionality. They are case insensitive and can’t be used for variables/classes defined by the user.

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

What are the data types in PHP?

A

Scalar Types

  • integer
  • floating-point
  • string
  • boolean

Compound Types

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

How do you define an Octal number in PHP?

A

Start an integer with a leading 0, and a series of digits 0-7.

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

How do you define a Hexadecimal value in PHP?

A

Prefix an integer with 0x followed by digits 0-9 or letters A-F.

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

How do you define a Binary value in PHP?

A

Write an integer value that begins with 0b followed by a sequence of 0-1.

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

What happens if you try to store a value that’s too big to hold an integer?

A

It will automatically convert to a floating-point value.

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

How can we test if a value is an integer?

A

Use the is_int() function.

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

Floating point values are only approximations, with that in mind how can we test if floats are equal?

A

Convert them to integers for comparison

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

How can you test if a value is a float?

A

Use the function is_float();

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

What’s the difference between single and double quoted strings in PHP?

A

Single quotes – String literals, they will not be interpolated.

Double quotes – Strings will be interpolated before returning value. (Variables will be expanded)

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

How can you test if two strings are equal?

A

Simply use == operator.

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

How can you test if a variable is a string in PHP?

A

Use the is_string() function

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

Booleans represent a truth value, what all values evaluate to false?

A
  • keyword ‘false’
  • integer 0
  • floating-point 0.0
  • empty string “” AND string “0”
  • Empty array []
  • the value NULL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How can we test if a value is a boolean?

A

use the is_bool() function

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

What are the 2 ways of creating an array in PHP?

A

using the array(“”) construct or the shorthand []

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

What are the 2 ways we can use a foreach loop with an array?

A

We can do foreach ($array as $value) OR foreach ($array as $index => $value) depending on if we need the index or not.

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

How can we determine if a variable is an array?

A

Use the is_array() function

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

How do we define a class?

A

Using the class keyword.

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

How do we create a new object from a class?

A

Use the new keyword. You can optionall pass parameters to the constructor if needed as well, or you can omit the parenthesis entirely.

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

How can you access the methods/properties of an object?

A

Use the -> (Arrow) construct.

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

How can you test if something is an object?

A

Use the is_object() function.

28
Q

What purpose do Resources provide in PHP?

A

Usually provide several functions for dealing with the outside world, such as a database connection or reading a file.

29
Q

How can you test if a value is a Resource or not?

A

Use the is_resource() function.

30
Q

How can you test if a value is NULL?

A

Use the is_null() function

31
Q

How can you create a pointer/alias to another variable?

A

Use the =& operand.

32
Q

How can we destroy a pointer/alias without changing the underlying value?

A

Use the unset() function.

33
Q

What’s the syntax for getting a function return value by reference? What’s special about the assignment?

A

You need to use the & operator in front of the function name. You also need to use reference assignment when assigning to a variable, else PHP will use the value and not the actual reference.

34
Q

What’s the scope of variables declared outside of a function? Are they available within a function?

A

They’re global scope, and aren’t accessible within a function. They’re available from any part of a program, but not within functions.

35
Q

How can you access a global variable within a function?

A

Use the global keyword to ‘pull it into the function’.

36
Q

What’s special about static variables within functions?

A

They retain their value between calls to the function, but are only visible within that function.

37
Q

What are the 2 ways PHP managed memory?

A

Reference counting – Ensures memory is returned to OS when it’s no longer needed (not referenced anywhere)

copy-on-write – When copying a value from on variable to another, PHP doesn’t get more memory. Instead it updates the symbol table to indicate both are using the SAME chunk of memory. Subsequently if we modify one of our values it will NOW copy, since we’re modying it. (See image for example)

38
Q

How can you see if a variable is set to any value?

A

Use the isset() method.

39
Q

If you wanted to destroy a variable (and remove it from the symbol table) how would you do that?

A

Use the unset() function. If you set it to NULL, this will destroy the value but it’s still a defined variable according to PHP.

40
Q

What is implicit casting called in PHP?

A

Type Juggling

41
Q

If you type juggle ‘Integer + Floating-Point’ what is the result?

A

Integer is converted to a Floating-Point.

42
Q

If you type juggle ‘Integer + String’ what is the result?

A

The string is converted to a number either Integer or Floating-Point (Rules for converting string to integer later).

If result of String conversion is floating-point then the integer is also converted to a floating-point.

43
Q

If you type juggle ‘Floating-Point + String’ what’s the result?

A

String is converted to a floating-point.

44
Q

In regard to type juggling, how are Strings converted to integers?

A

The string is presumed to start with a number, if no number is found at the start it’s set to 0.

If the String contains a ‘.’ or ‘E’ or ‘e’ it will be evaluated as a floating-point value. Int he case of ‘e/E’ it will assume scientific notation.

45
Q

What’s the syntax for Exponentiation?

A

$base ** $exponent

46
Q

How do you do String concatenation?

A

‘String part ‘ . $var1 . ‘ ending part’

47
Q

What’s the difference in both syntax and logic for pre-increment vs post-increment? Same question for decrement operator.

A

Post-Increment/Decrement – Returns the value as it exists at that moment, then increments the value. Place the ++ or – AFTER the variable. $var++

Pre-Increment/Decrement – Returns the value AFTER doing the increment or decrement. Place the ++ or – BEFORE the variable. –$var

48
Q

What happens when you increment Strings?

A

It gives you the next higher letter. Example “a” => “b”, this wraps around to “z” => “aa”

49
Q

What’s in $var after executing this code?

$var = “K9”;

$var++;

A

“L0”

Remember it increments the “9” which resets it with a carry, that carry changes “K” => “L”.

50
Q

What are the 2 types of comparisons PHP can do?

A

Numeric (9 > 3)

Lexicographic (“Dan” > “Jeff”)

51
Q

When are Lexicographic comparisons done? How can we force it?

A

Comparing 2 strings that aren’t entirely numeric. We can manually check it by using the strcmp() function.

52
Q

Compare Equality and Identity.

A

Equality – Check if both operands are equal.

Identity – Checks that both operands are equal and of same type. Objects it checks if they’re literally the same object.

53
Q

What is the Spaceship operator? What does it do?

A

<=>

It does a comparison to left and right hand operands.

Equal => 0

Left greater => -1

Right greater => 1

54
Q

What is the Null Coalescing operator? What does it do?

A

??

Operator check if lefthand operand is not null, if not it uses that. Else evalutes righthand operand.

55
Q

What does casting an object to an array do?

A

Builds an array of properties of the object.

56
Q

What happens if you cast an array to an object?

A

You build an object whose properties correspond to the array’s keys and values.

57
Q

What does the assignment operator look like? What does it do?

A

=

Assigns values to variables. It also returns the value that was just assigned.

58
Q

What is the Execution operator? What does it do?

A

ls -ls /tmp;

Executes the string contained between the backticks as a shell command when echo to screen.

59
Q

What does the instanceof operator do? How is it useful?

A

$a instanceof Person

Returns true/false. Test whether a variable of an instantiated object is of a given class or implements an interface. Useful to test if an object implements an interface.

60
Q

What is the alternative syntax for an if statement?

A
61
Q

What is the alternative syntax for control structures such as loops/if useful for?

A

Mixing in PHP with HTML. You can in a much cleaner way mix the two without echo for a ton of html.

62
Q

What’s the syntax for a switch statement? What is a gotcha for them?

A

Start with switch ($var) then list your cases, making sure to break all statements as PHP does fall-through. Can leave default statement as catch-all.

63
Q

What does putting a number after a break or a continue statement do?

A

Determines how many levels of structures to break out of, or loop to continue for.

64
Q

How do you leave the entire application in PHP?

A

exit() or die()

You can also specify a message, which will be printed before exiting.

65
Q

What’s the difference between include and require?

A

include – paste contents right here, if not found throws a warning.

require – paste contents right here, if not found will fail the whole program. Useful for libraries and things that would cause catostrophic failure if not present.

66
Q

How can we include another file, but only the once so if it’s already loaded don’t load it a second time?

A

include_once/require_once

67
Q

What’s the echo short tag? What’s the availability of this tag?

A

With this tag you don’t need the echo.

This tag is avaiable at all times.