Bird
Raised Fist0
PHPprogramming~10 mins

Type casting syntax in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Type casting syntax
Start with a variable
↓
Apply type cast syntax
↓
Variable changes type
↓
Use the new typed variable
↓
End
This flow shows how a variable is changed from one type to another using PHP's type casting syntax.
Execution Sample
PHP
<?php
$var = "123";
$intVar = (int)$var;
echo $intVar;
?>
This code converts a string containing digits into an integer and prints it.
Execution Table
StepActionVariableValue BeforeType BeforeValue AfterType AfterOutput
1Initialize variable$varundefinedundefined"123"string
2Apply (int) cast$intVarundefinedundefined123integer
3Print $intVar$intVar123integer123integer123
4End------
💡 Execution stops after printing the integer value 123.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$varundefined"123""123""123""123"
$intVarundefinedundefined123123123
Key Moments - 2 Insights
Why does (int)$var change the type but not the original $var?
Because (int)$var creates a new value with integer type assigned to $intVar, but $var remains the original string as shown in execution_table step 2 and variable_tracker.
What happens if the string contains letters, like "123abc"?
PHP converts the initial numeric part to integer and stops at the first non-digit, so (int)"123abc" becomes 123. This is consistent with PHP's casting rules.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the type of $intVar after casting?
Ainteger
Bfloat
Cstring
Dboolean
💡 Hint
Check the 'Type After' column for $intVar at step 2 in execution_table.
At which step is the output produced?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Output' column in execution_table to find when output appears.
If $var was "45.67" and cast to (int), what would $intVar be?
A45.67
B46
C45
D"45.67"
💡 Hint
Casting float-like strings to int truncates the decimal part, as per PHP casting rules.
Concept Snapshot
PHP Type Casting Syntax:
- Use (type) before a variable to convert it.
- Common types: (int), (float), (string), (bool), (array), (object).
- Casting creates a new value; original variable unchanged.
- Example: $intVar = (int)$var;
- Useful to ensure variable is the expected type.
Full Transcript
This lesson shows how PHP changes a variable's type using type casting syntax. We start with a string variable $var holding "123". Then we create a new variable $intVar by casting $var to integer using (int)$var. This changes the value to 123 as an integer type. When we print $intVar, the output is 123. The original $var remains a string. This process helps convert data types explicitly in PHP. The execution table traces each step, showing variable values and types before and after casting. Key points include that casting creates a new typed value without changing the original variable, and that PHP converts strings with numbers correctly to integers. The quiz checks understanding of types after casting, output timing, and casting behavior with float-like strings.

Practice

(1/5)
1. What does the following PHP code do?
$num = (int) "123";
easy
A. Converts the string "123" to an integer 123
B. Converts the integer 123 to a string
C. Throws a syntax error
D. Leaves the variable as a string

Solution

  1. Step 1: Identify the type casting operation

    The code uses (int) before a string variable, which means it converts the string to an integer.
  2. Step 2: Understand the conversion result

    The string "123" becomes the integer 123 after casting.
  3. Final Answer:

    Converts the string "123" to an integer 123 -> Option A
  4. Quick Check:

    Type casting (int) converts string "123" to integer 123 [OK]
Hint: Casting (int) changes strings of digits to numbers [OK]
Common Mistakes:
  • Thinking it converts integer to string
  • Expecting a syntax error
  • Assuming the variable stays a string
2. Which of the following is the correct syntax to cast a variable $value to a float in PHP?
easy
A. $floatValue = (float) $value;
B. $floatValue = cast float $value;
C. $floatValue = float($value);
D. $floatValue = (float)$value[];

Solution

  1. Step 1: Recall PHP type casting syntax

    PHP uses (type) before a variable to cast it, so (float) $value is correct.
  2. Step 2: Check each option

    $floatValue = float($value); uses a function-like syntax which is invalid. $floatValue = cast float $value; uses invalid syntax. $floatValue = (float)$value[]; tries to cast an array element incorrectly.
  3. Final Answer:

    $floatValue = (float) $value; -> Option A
  4. Quick Check:

    Correct cast syntax is (float) $value [OK]
Hint: Use (type) before variable for casting in PHP [OK]
Common Mistakes:
  • Using function call syntax like float($value)
  • Writing cast without parentheses
  • Adding invalid array brackets after cast
3. What is the output of this PHP code?
$a = "10 apples";
$b = (int) $a;
echo $b;
medium
A. Syntax error
B. 0
C. 10
D. 10 apples

Solution

  1. Step 1: Understand casting string with numbers and text

    When casting a string starting with numbers to int, PHP converts the leading number part only.
  2. Step 2: Apply casting to the string "10 apples"

    The string starts with "10", so (int) "10 apples" becomes integer 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    String starting with digits casts to those digits as int [OK]
Hint: Casting string with leading digits to int keeps those digits [OK]
Common Mistakes:
  • Expecting full string output
  • Thinking casting fails with text
  • Assuming zero because of text
4. Find the error in this PHP code snippet:
$value = "123.45";
$intValue = int($value);
echo $intValue;
medium
A. Missing semicolon after echo
B. int() is not a valid cast syntax in PHP
C. Variable $value is not defined
D. Casting float to int is not allowed

Solution

  1. Step 1: Check the casting syntax used

    The code uses int($value), which looks like a function call but PHP does not have int() function for casting.
  2. Step 2: Identify correct casting method

    PHP uses (int) $value to cast, not int($value).
  3. Final Answer:

    int() is not a valid cast syntax in PHP -> Option B
  4. Quick Check:

    Use (int) for casting, not int() function [OK]
Hint: Casting uses (int), not int() function [OK]
Common Mistakes:
  • Using int() like a function
  • Confusing casting with function calls
  • Ignoring PHP casting syntax rules
5. Given the array $data = ["5", "3.14", "true", "false", "0"];, which PHP code correctly casts each element to its proper type (int, float, bool) using type casting syntax and stores results in $result?
hard
A. $result = [int($data[0]), float($data[1]), bool($data[2]), bool($data[3]), bool($data[4])];
B. $result = [(float)$data[0], (float)$data[1], (string)$data[2], (string)$data[3], (bool)$data[4]];
C. $result = [(int)$data[0], (int)$data[1], (bool)$data[2], (bool)$data[3], (int)$data[4]];
D. $result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]];

Solution

  1. Step 1: Analyze required casts for each element

    "5" should be int, "3.14" float, "true" and "false" to bool, "0" to bool.
  2. Step 2: Check each option for correct casting syntax and types

    $result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]]; uses correct (type) syntax and casts each element properly. $result = [int($data[0]), float($data[1]), bool($data[2]), bool($data[3]), bool($data[4])]; uses invalid function-like casts. $result = [(int)$data[0], (int)$data[1], (bool)$data[2], (bool)$data[3], (int)$data[4]]; casts "3.14" to int incorrectly. $result = [(float)$data[0], (float)$data[1], (string)$data[2], (string)$data[3], (bool)$data[4]]; casts "true" and "false" to string incorrectly.
  3. Final Answer:

    $result = [(int)$data[0], (float)$data[1], (bool)$data[2], (bool)$data[3], (bool)$data[4]]; -> Option D
  4. Quick Check:

    Use (type) syntax and correct types for each element [OK]
Hint: Use (type) before each element for proper casting [OK]
Common Mistakes:
  • Using function calls instead of casting syntax
  • Casting floats as int incorrectly
  • Casting booleans as 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