0
0
PHPprogramming~15 mins

Type casting syntax in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Type Casting Syntax in PHP
📖 Scenario: You are working on a simple PHP script that needs to convert different types of data into integers. This is common when you get input as strings but need numbers to do calculations.
🎯 Goal: Learn how to use type casting syntax in PHP to convert strings and floats into integers.
📋 What You'll Learn
Create variables with string and float values
Create a variable to hold the integer cast of the string
Create a variable to hold the integer cast of the float
Print the integer values
💡 Why This Matters
🌍 Real World
Type casting is useful when you get input as text but need numbers to do math or comparisons.
💼 Career
Many programming jobs require handling data conversions safely and correctly, especially in web development with PHP.
Progress0 / 4 steps
1
Create initial variables
Create a variable called $strNumber and set it to the string "123.45". Also create a variable called $floatNumber and set it to the float 67.89.
PHP
Need a hint?

Use = to assign values to variables. Strings go inside quotes.

2
Add integer type casting variables
Create a variable called $intFromStr and set it to the integer cast of $strNumber using (int) type casting. Also create a variable called $intFromFloat and set it to the integer cast of $floatNumber using (int).
PHP
Need a hint?

Use (int) before a variable to convert it to an integer.

3
Use a loop to print variable types and values
Use a foreach loop with variables $name and $value to iterate over an array with keys 'intFromStr' and 'intFromFloat' and their corresponding values. Inside the loop, print the variable name, its type using gettype(), and its value.
PHP
Need a hint?

Use foreach to loop over arrays. Use gettype() to find the type of a variable.

4
Print the final output
Run the program to print the type and value of $intFromStr and $intFromFloat variables.
PHP
Need a hint?

Make sure your echo statements print the exact text with new lines.