PHP Syntax and Basics Flashcards

1
Q

Q: What does PHP stand for?

A

A: PHP: Hypertext Preprocessor.

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

Q: What is PHP primarily used for?

A

A: Server-side scripting for web development.

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

Q: How do you start and end a block of PHP code?

A

A: <?php to start and ?> to end.

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

Q: What is the correct way to output text in PHP?

A

A: Using echo or print.

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

Q: How do you add a single-line comment in PHP?

A

A: Use // or #.

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

Q: How do you declare a variable in PHP?

A

A: Use the $ symbol, e.g., $variable_name = “value”;.

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

Q: What are the common data types in PHP?

A

A: String, Integer, Float, Boolean, Array, Object, NULL.

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

Q: What function is used to get the data type of a variable?

A

A: gettype().

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

Q: How do you check if a variable is set?

A

A: Use the isset() function.

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

Q: How do you unset a variable in PHP?

A

A: Use the unset() function.

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

Q: How do you define a constant in PHP?

A

A: Using the define() function.

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

Q: Can constants be changed after they are set?

A

A: No, constants are immutable.

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

Q: What is the correct syntax for defining a constant?

A

A: define(“CONSTANT_NAME”, “value”);.

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

Q: How do you access a constant?

A

A: Use the constant name directly, e.g., CONSTANT_NAME.

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

Q: What is the difference between define() and const?

A

A: const is used for class constants and must be defined in the global scope.

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

Q: What are the arithmetic operators in PHP?

A

A: +, -, *, /, %.

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

Q: What is the concatenation operator in PHP?

A

A: . (dot).

18
Q

Q: What are the comparison operators in PHP?

A

A: ==, ===, !=, !==, <, >, <=, >=.

19
Q

Q: What is the spaceship operator in PHP, and what does it do?

A

A: <=>, it returns -1, 0, or 1 depending on the comparison.

20
Q

Q: What are logical operators in PHP?

A

A: &&, ||, !, and, or.

21
Q

Q: What is the syntax for an if statement in PHP?

A

if (condition) {
// Code to execute
}

22
Q

Q: How do you write a switch statement in PHP?

A

switch (variable) {
case “value1”:
// Code
break;
default:
// Code
}

23
Q

Q: How do you write a while loop in PHP?

A

while (condition) {
// Code to execute
}

24
Q

Q: How do you write a for loop in PHP?

A

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

25
Q

Q: What is the difference between break and continue in loops?

A

A: break exits the loop, while continue skips to the next iteration.

26
Q

Q: How do you define a function in PHP?

A

function functionName() {
// Code
}

27
Q

Q: How do you return a value from a function?

A

A: Use the return keyword.

28
Q

Q: Can PHP functions have default arguments?

A

A: Yes, e.g., function example($arg = “default”).

29
Q

Q: How do you pass arguments by reference to a function?

A

A: Use & before the variable name in the function declaration.

30
Q

Q: What is an anonymous function in PHP?

A

A: A function without a name, created using the function keyword.

31
Q

Q: How do you create an indexed array in PHP?

A

A: $array = array(“value1”, “value2”);.

32
Q

Q: How do you create an associative array in PHP?

A

A: $array = array(“key1” => “value1”, “key2” => “value2”);.

33
Q

Q: What function is used to loop through an array?

A

A: foreach.

34
Q

Q: How do you add an element to an array?

A

A: $array[] = “new value”;.

35
Q

Q: What function checks if a value exists in an array?

A

A: in_array().

36
Q

Q: What is the difference between single and double quotes in PHP?

A

A: Double quotes parse variables, single quotes do not.

37
Q

Q: How do you find the length of a string in PHP?

A

A: Use the strlen() function.

38
Q

Q: How do you replace a substring in PHP?

A

A: Use the str_replace() function.

39
Q

Q: How do you convert a string to lowercase in PHP?

A

A: Use the strtolower() function.

40
Q

Q: How do you find the position of a substring in a string?

A

A: Use the strpos() function.