Skip to Content
Course content

103: gettype, settype, and Type Juggling Functions

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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:

  1. Create an array named $rawScores containing these values: "450", 1200, "800", 310.5, "invalid", 900.
  2. Loop through the array.
  3. Use is_numeric() to check if the value can even be treated as a number.
  4. If it is numeric, use settype() to force the variable to become an integer.
  5. If it is NOT numeric (like the string "invalid"), replace that value with 0.
  6. Print the final array using var_dump() to prove that every single element is now an int.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.