PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
103: gettype, settype, and Type Juggling Functions
I was digging through some legacy code yesterday and found a classic PHP headache: a variable that was supposed to be a number, but because it came from a URL parameter, it was actually a string. In a language like Java or C#, that would crash the program. In PHP, it usually "just works," but that's exactly where the bugs hide.
Wait, what is this variable actually?
Let's say we're building a gaming profile and we've grabbed the user's current experience points from a database or a request. I'll start with something simple and see how PHP perceives it.
$userXP = "1500";
echo gettype($userXP);
// Output: string
Okay, it's a string. Now, if I try to add 500 points to it, PHP does something called "type juggling." It sees I'm trying to do math, so it silently converts that string to an integer on the fly.
$userXP = $userXP + 500;
echo gettype($userXP);
// Output: integer
It feels like magic, right? But relying on that is dangerous. If `$userXP` happened to be the string "1,500" (with a comma) or "1500XP", the juggling would fail or produce a warning in newer PHP versions. I need to know exactly what I'm dealing with before I perform logic on it.
Forcing the change with settype
I could just let PHP guess, but if I want to be explicit and actually change the variable's type in place, I can use settype(). This is different from casting—where you create a new value of a different type—because settype() modifies the original variable.
$score = "42";
echo gettype($score); // string
settype($score, "integer");
echo gettype($score); // integer
I'll be honest: in my day-to-day work, I rarely use settype(). I usually prefer casting, like $score = (int)$score;. However, settype() is useful when you're passing a variable into a function that specifically requires the internal type to be changed for the rest of the script's lifecycle.
The "is_" family is usually more useful
While gettype() is great for debugging—basically telling you "Hey, this is a string"—it's actually a bit clunky to use inside an if statement. I'd have to write if (gettype($var) === 'integer'), which is just too much typing.
Instead, I usually reach for the type-checking functions. Let's try these out on a mixed set of data.
$data = ["100", 200, 1.5, true];
foreach ($data as $item) {
if (is_int($item)) {
echo "Found an integer: $item\n";
} elseif (is_string($item)) {
echo "Found a string: $item\n";
} elseif (is_float($item)) {
echo "Found a float: $item\n";
} elseif (is_bool($item)) {
echo "Found a boolean\n";
}
}
These is_ functions are the bread and butter of defensive programming in PHP. By checking the type before acting, I avoid those weird "A non-numeric value encountered" warnings that plague so many PHP projects.
📋 Practical Task
The API Data Sanitizer for Game Scores
You have received an array of scores from an external API. Because of the way the API was built, some scores are sent as integers, some as strings, and some are accidentally sent as floats. To calculate the leaderboard, you must ensure every value is a strict integer.
Your Task:
- Create an array named
$rawScorescontaining these values:"450", 1200, "800", 310.5, "invalid", 900. - Loop through the array.
- Use
is_numeric()to check if the value can even be treated as a number. - If it is numeric, use
settype()to force the variable to become aninteger. - If it is NOT numeric (like the string "invalid"), replace that value with
0. - Print the final array using
var_dump()to prove that every single element is now anint.
There are no comments for now.