0
0
PHPprogramming~15 mins

Gettype and typeof checks in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Gettype and typeof checks
📖 Scenario: Imagine you are working on a PHP program that needs to check the types of different variables to make sure they are what you expect. This is like checking the ingredients before cooking to ensure the recipe turns out right.
🎯 Goal: You will create variables of different types, set up a variable to hold a type to check against, use gettype() to compare types, and finally print the result of the check.
📋 What You'll Learn
Create variables of type integer, string, and boolean with exact values.
Create a variable called checkType with the exact string value to check.
Use gettype() function to get the type of a variable and compare it with checkType.
Print the result of the comparison as 'true' or 'false'.
💡 Why This Matters
🌍 Real World
Checking variable types is important when you want to make sure your program handles data correctly, like checking ingredients before cooking.
💼 Career
Many programming jobs require you to validate data types to avoid errors and bugs in software.
Progress0 / 4 steps
1
Create variables of different types
Create three variables: number with the integer value 10, text with the string value "hello", and flag with the boolean value true.
PHP
Need a hint?

Use $variable = value; syntax to create variables.

2
Set the type to check
Create a variable called checkType and set it to the string "integer".
PHP
Need a hint?

Remember to put the string value in double quotes.

3
Compare variable type with checkType
Use gettype() on the variable number and compare it with checkType. Store the result (true or false) in a variable called isMatch.
PHP
Need a hint?

Use gettype($number) and compare with $checkType using ===.

4
Print the comparison result
Print the value of isMatch using var_export() to show true or false.
PHP
Need a hint?

Use var_export($isMatch); to print boolean as text.