Bird
Raised Fist0
PHPprogramming~10 mins

Settype for changing types in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Settype for changing types
Start with variable
↓
Call settype(var, type)
↓
Change variable type
↓
Variable now has new type and value
↓
Use variable with new type
The flow shows how settype changes a variable's type and value, then you use the variable with its new type.
Execution Sample
PHP
<?php
$var = "123";
settype($var, "int");
echo gettype($var) . ": " . $var;
?>
This code changes a string variable to an integer and prints its type and value.
Execution Table
StepVariableOriginal TypeActionNew TypeNew ValueOutput
1$varstringInitialize with "123"string"123"
2$varstringsettype($var, "int")integer123
3$varintegerecho gettype($var) . ": " . $varinteger123integer: 123
4-----Execution ends
💡 After step 3, the variable is an integer with value 123, so execution ends.
Variable Tracker
VariableStartAfter settypeFinal
$var"123" (string)123 (integer)123 (integer)
Key Moments - 3 Insights
Why does the variable value change when we use settype?
Because settype converts the variable to the new type, PHP changes the value to match that type, as shown in step 2 of the execution_table.
Does settype return the new value or the variable itself?
settype returns a boolean true on success, but the variable itself is changed by reference, as seen in step 2 where $var changes type and value.
What happens if you settype to 'bool' with a non-empty string?
PHP converts any non-empty string to true (boolean), so the variable becomes true, similar to how it changed to integer in the example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of $var after step 2?
Ainteger
Bstring
Cboolean
Dfloat
💡 Hint
Check the 'New Type' column in step 2 of the execution_table.
At which step does the output 'integer: 123' appear?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table.
If we change settype($var, "int") to settype($var, "bool"), what would be the new value of $var after step 2?
Afalse
Btrue
C0
D"bool"
💡 Hint
Recall that any non-empty string converts to true when cast to boolean in PHP.
Concept Snapshot
settype(variable, type) changes the variable's type in place.
Supported types: 'int', 'integer', 'bool', 'boolean', 'float', 'string', etc.
The variable's value changes to match the new type.
settype returns true on success.
Use gettype() to check the variable's type after conversion.
Full Transcript
This example shows how PHP's settype function changes a variable's type and value. Initially, $var is a string with value "123". After calling settype($var, "int"), $var becomes an integer with value 123. When we print the type and value, it outputs 'integer: 123'. The variable changes by reference, and settype returns true. This helps convert variables to needed types easily.

Practice

(1/5)
1. What does the PHP function settype do?
easy
A. It changes the type of a variable directly.
B. It creates a new variable with a different type.
C. It checks the type of a variable without changing it.
D. It converts a variable to a string only.

Solution

  1. Step 1: Understand the purpose of settype

    The function settype modifies the type of the variable passed to it directly, not creating a new variable.
  2. Step 2: Compare options with the function behavior

    Only It changes the type of a variable directly. correctly states that it changes the type of a variable directly.
  3. Final Answer:

    It changes the type of a variable directly. -> Option A
  4. Quick Check:

    settype changes variable type = A [OK]
Hint: Remember: settype changes the original variable type [OK]
Common Mistakes:
  • Thinking settype creates a new variable
  • Confusing settype with type checking functions
  • Believing settype only converts to string
2. Which of the following is the correct syntax to change variable $var to an integer using settype?
easy
A. settype($var, "int");
B. settype($var, int);
C. settype($var, 'int');
D. settype($var, 'integer');

Solution

  1. Step 1: Recall the correct type string for integers in settype

    PHP expects the type as a string like 'int' or 'integer'. Both 'int' and 'integer' are accepted, but 'integer' is the official type name.
  2. Step 2: Check syntax correctness

    settype($var, 'integer'); uses quotes and the correct type string 'integer'. settype($var, 'int'); also works as an alias. settype($var, int); misses quotes, causing syntax error. settype($var, "int"); uses double quotes which is valid but less common; both single and double quotes work in PHP.
  3. Final Answer:

    settype($var, 'integer'); -> Option D
  4. Quick Check:

    Use quotes around type string 'integer' = A [OK]
Hint: Always put the type as a quoted string in settype [OK]
Common Mistakes:
  • Omitting quotes around the type string
  • Using undefined type names without quotes
  • Confusing 'int' and int without quotes
3. What will be the output of the following PHP code?
$var = "123abc";
settype($var, 'int');
echo $var;
medium
A. 0
B. 123abc
C. 123
D. Error

Solution

  1. Step 1: Understand how settype converts string to int

    When converting a string to int, PHP reads the number from the start until it hits a non-digit character. Here, "123abc" converts to 123.
  2. Step 2: Check the echo output

    After settype, $var is integer 123, so echo prints 123.
  3. Final Answer:

    123 -> Option C
  4. Quick Check:

    String to int conversion stops at first non-digit = A [OK]
Hint: String to int stops at first non-digit character [OK]
Common Mistakes:
  • Expecting the whole string to convert
  • Thinking conversion results in 0 for mixed strings
  • Assuming settype throws an error on mixed strings
4. Identify the error in the following PHP code:
$value = "true";
settype($value, bool);
echo $value;
medium
A. Variable name is invalid.
B. Missing quotes around 'bool' in settype.
C. Echo cannot print boolean values.
D. Cannot convert string 'true' to boolean.

Solution

  1. Step 1: Check settype syntax

    The second argument to settype must be a string in quotes. Here, bool is unquoted, causing a syntax error.
  2. Step 2: Verify other options

    Converting string 'true' to boolean is valid. Echo can print booleans as 1 or empty. Variable name is valid.
  3. Final Answer:

    Missing quotes around 'bool' in settype. -> Option B
  4. Quick Check:

    Type name must be quoted string = A [OK]
Hint: Always quote the type name in settype [OK]
Common Mistakes:
  • Forgetting quotes around type string
  • Thinking echo can't print booleans
  • Assuming variable names cause errors here
5. Given the array $arr = ['1', '2', '3.5', 'true', 'false', '0'];, which code snippet correctly converts all elements to their proper types using settype?
hard
A. foreach ($arr as &$v) { if ($v === 'true' || $v === 'false') settype($v, 'bool'); elseif (strpos($v, '.') !== false) settype($v, 'float'); else settype($v, 'int'); }
B. foreach ($arr as &$v) { if (is_numeric($v)) settype($v, 'float'); else settype($v, 'bool'); }
C. foreach ($arr as &$v) { settype($v, 'int'); }
D. foreach ($arr as &$v) { settype($v, 'string'); }

Solution

  1. Step 1: Analyze the array elements and desired types

    Elements are strings representing integers, floats, and booleans. We want to convert 'true'/'false' to bool, numbers with '.' to float, others to int.
  2. Step 2: Evaluate each option's logic

    foreach ($arr as &$v) { settype($v, 'int'); } converts all to int, losing floats and booleans. foreach ($arr as &$v) { if (is_numeric($v)) settype($v, 'float'); else settype($v, 'bool'); } converts all numeric to float, which is incorrect for integers. foreach ($arr as &$v) { if ($v === 'true' || $v === 'false') settype($v, 'bool'); elseif (strpos($v, '.') !== false) settype($v, 'float'); else settype($v, 'int'); } checks for 'true'/'false' strings to bool, floats by '.', else int - this matches the requirement. foreach ($arr as &$v) { settype($v, 'string'); } converts all to string, no type change.
  3. Final Answer:

    foreach ($arr as &$v) { if ($v === 'true' || $v === 'false') settype($v, 'bool'); elseif (strpos($v, '.') !== false) settype($v, 'float'); else settype($v, 'int'); } -> Option A
  4. Quick Check:

    Check string values carefully for correct type conversion = C [OK]
Hint: Check string content to decide type before settype [OK]
Common Mistakes:
  • Converting all to int losing floats and booleans
  • Using is_numeric alone without checking for booleans
  • Forgetting to handle 'true' and 'false' strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard