4. Strings Flashcards

1
Q

When doing String interpolation, how can you assure that the correct variable is replaced?

A

Wrap the variable in ${var1}. Everything between curley braces will be evaluated.

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

How many times does String Interpolation run?

A

Only once, unlike variable variables in normal code.

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

What is a Heredoc? What’s the syntax?

A

<<< 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.

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

Does a Heredoc interpolate strings?

A

Yes

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

What’s the difference between printf() and sprintf()?

A

printf() – Writes directly to the screen.

sprintf() – Returns the built-up string so you can save it for future use.

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

What function is useful for debugging?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you get the length of a string?

A

strlen() function.

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

What are the functions we can use to trim up whitespace in a string?

A

trim() – Left & right trimming.

ltrim() – Left trimming

rtrim() – Right trimming

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

How can we convert an entire string to uppercase or lowercase?

A

strtolower()

strtoupper()

Functions respectively.

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

What functions can we use to capitalize the first character of a string? What about the first letter of every word in a string?

A

ucfirst() – First character in the whole string

ucwords() – First character in all words.

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

How can you parse a string to a text representation of html?

A

htmlentities() function

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

How can you strip out html from a string?

A

strip_tags()

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

What function would you use for URL encoding? URL decoding?

A

rawurlencode()

rawurldecode()

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

How would you encode a query string? How about decoding?

A

urlencode()

urldecode()

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

How SHOULD you compare 2 strings? Why do you need to do it this way?

A

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.

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

How do you compare strings by natural order?

A

strncmp(str1, str2)

strncasecmp(str1, str2)

17
Q

Give an example of natural order vs ASCII order? Think why we’d want to use natural order instead of just ASCII order.

A
18
Q

What’s the function to get a substring?

A

substr($str, start, length)

if length is removed, it copies to the end of the string.

19
Q

How can you count the number of times a substring occurs within a bigger string?

A

substr_count($big_string, $small_string)

20
Q

How can you replace at a location in a string?

A

substr_replace($original, $new_replace, startIndex, length)

If you put a length of 0, it will insert without deleting.

If you use an empty string for $new_replace, it will simply remove from the $original.

You can use negative values for the startIndex to start counting from the end of the string.

21
Q

What function would you use to reverse a string?

A

strrev($string)

22
Q

How do you Tokenize a string? What’s it useful for?

A

$firstChunk = strtok($string, $separator)

Then when you want the next chunk you just keep calling

$nextChunk = strtok($separator)

23
Q

How do you use sscanf(), how is it useful?

A

We can get an array of fields (similar to a printf formatting)

$array = sscanf($string, $template)

OR we can provide the variables that will be where results are stored

$count = sscanf($string, $template, $var1 … $var9)