4. Strings Flashcards
When doing String interpolation, how can you assure that the correct variable is replaced?
Wrap the variable in ${var1}. Everything between curley braces will be evaluated.
How many times does String Interpolation run?
Only once, unlike variable variables in normal code.
What is a Heredoc? What’s the syntax?
<<< Identifier
You choose an identifier, whatever you want. Then next line start writing your string and end with your identifier by itself with a semi-colon.
Does a Heredoc interpolate strings?
Yes
What’s the difference between printf() and sprintf()?
printf() – Writes directly to the screen.
sprintf() – Returns the built-up string so you can save it for future use.
What function is useful for debugging?
var_dump(), print_r() is as well but it doesn’t meaningfully display Bool or NULL so that’s why var_dump() is preferred.
How can you get the length of a string?
strlen() function.
What are the functions we can use to trim up whitespace in a string?
trim() – Left & right trimming.
ltrim() – Left trimming
rtrim() – Right trimming
How can we convert an entire string to uppercase or lowercase?
strtolower()
strtoupper()
Functions respectively.
What functions can we use to capitalize the first character of a string? What about the first letter of every word in a string?
ucfirst() – First character in the whole string
ucwords() – First character in all words.
How can you parse a string to a text representation of html?
htmlentities() function
How can you strip out html from a string?
strip_tags()
What function would you use for URL encoding? URL decoding?
rawurlencode()
rawurldecode()
How would you encode a query string? How about decoding?
urlencode()
urldecode()
How SHOULD you compare 2 strings? Why do you need to do it this way?
strcmp(str1, str2)
OR strcasecmp(str1, str2) which converts to lower before comparing.
They return a number less than 0 of str1 sorts before str2, and a number greater than 0 if str2 sorts before str1.
We need to use these functions because if you accidentally compared a string to an integer, it will cast the string to an int and try to do a comparison that we didn’t intend to do.