0
0
PHPprogramming~5 mins

Settype for changing types in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the settype() function do in PHP?
The settype() function changes the type of a variable to a specified type, like integer, string, or boolean.
Click to reveal answer
beginner
How do you change a variable $var to an integer using settype()?
Use settype($var, 'integer'); to convert $var to an integer type.
Click to reveal answer
intermediate
What types can you use with settype()?
You can use types like 'boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array', and 'object'.
Click to reveal answer
intermediate
Does settype() return the converted value or something else?
settype() returns true on success and false on failure. It changes the variable itself by reference.
Click to reveal answer
advanced
What happens if you try to convert a string like '123abc' to an integer using settype()?
PHP converts the string to the integer 123, stopping at the first non-numeric character.
Click to reveal answer
What is the correct way to change a variable $x to a boolean using settype()?
Asettype($x, 'boolean');
Bsettype($x, 'bool');
Csettype($x, 'boolean_value');
Dsettype($x, 'boolean()');
What does settype() return after converting a variable?
ATrue on success, false on failure
BNothing (void)
CThe old value of the variable
DThe new value of the variable
Which of these is NOT a valid type for settype()?
Ainteger
Bstring
Cnumber
Darray
If $var = '45.67', what will settype($var, 'integer') do?
A$var becomes 45
B$var becomes 45.67
C$var becomes 0
DConversion fails
Can settype() convert a variable to an object?
ANo
BOnly if the variable is a string
COnly if the variable is an array
DYes
Explain how settype() changes the type of a variable in PHP.
Think about how PHP handles variable types and what settype() does to the variable itself.
You got /4 concepts.
    Describe what happens when you convert a string with numbers and letters (e.g., '123abc') to an integer using settype().
    Consider how PHP reads strings when converting to numbers.
    You got /4 concepts.