PHP String Manipulation Flashcards
Q: What are the two ways to define a string in PHP?
A: Single quotes (‘) and double quotes (“).
Q: What is the difference between single and double quotes in PHP?
A: Double quotes parse variables and escape sequences, while single quotes do not.
Q: How do you concatenate two strings in PHP?
A: Using the . operator, e.g., $str1 . $str2;.
Q: How do you find the length of a string in PHP?
A: Use the strlen() function.
Q: How do you access a character at a specific index in a string?
A: Use array-like syntax, e.g., $string[0];.
Q: How do you convert a string to lowercase in PHP?
A: Use the strtolower() function.
Q: How do you convert a string to uppercase in PHP?
A: Use the strtoupper() function.
Q: How do you capitalise the first letter of a string?
A: Use the ucfirst() function.
Q: How do you capitalise the first letter of each word in a string?
A: Use the ucwords() function.
Q: How do you convert the case of a string to title case?
A: Use ucwords(strtolower($string));.
Q: How do you find the position of the first occurrence of a substring?
A: Use the strpos() function.
Q: What function finds the last occurrence of a substring?
A: strrpos().
Q: How do you check if a string contains a substring?
A: Use strpos() and check if the result is not false.
Q: How do you replace all occurrences of a substring in a string?
A: Use the str_replace() function.
Q: How do you perform a case-insensitive string replacement?
A: Use the str_ireplace() function.
Q: How do you extract a substring from a string?
A: Use the substr() function.