1. 11
    Type juggling (coercion) in PHP
    2m 48s

Type juggling (coercion) in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Sometimes internet programming languages such as JavaScript & PHP do something funky when working with variables. Learn about this "type juggling", and how it works.

Instructor: [0:00] PHP has generally gotten a bad rap for something known as type juggling. If you attempt to combine multiple different data types together, either on purpose or by accident, the data types are automatically converted from one of the data types to the other, known as juggling.

[0:17] This provides a fallback mechanism for those new to PHP, but it can be pretty dangerous if dependent upon in larger apps because it can lead to undesired output. Let's remove the typecast on both of these variables. They are instead using PHP's automatic data type assignment. When we save and refresh the page, we are seeing our expected output.

[0:43] I'm going to use a couple weird examples right here just for demonstrating how type juggling can work.

[0:49] Let's multiply our numPosts by 10, and then we will wrap our 10 within single quotes, essentially making it a string. We save and refresh the page, we'll see something interesting.

[1:03] You'd probably expect an error to be outputted here, but instead, PHP uses data coercion or juggling to come up with a reasonable guess of what the output should be.

[1:14] When you're creating dependable web apps, you don't really want to be relying on reasonable guesses. You want to be 100 percent sure that what you expect to happen will, in fact, happen.

[1:26] Let's elaborate on this just a bit more by saying that this first number in numPost is also a string. When we refresh our page, nothing changes, it's still 100.

[1:39] It seems a bit odd because we're multiplying a string and a string. PHP assumes that since you are using this math operand of multiply that each of these values should, in fact, be a number.

[1:54] Let's take it a step further and add these strings together rather than multiply. Let's save and refresh the page, and this still holds up. Let's do something a bit different. Let's use a period here, instead of a dot.

[2:12] We haven't learned this quite yet, but this is how you join two strings together. When we refresh the page, we don't get a number, we get a string.

[2:21] Rather than converting these two strings to integers and then adding them together, both of these values remain strings and then they were concatenated together. I'm sure this 1010 output is probably not the output that you expected to see.

[2:37] Later on, when we get to functions, we can enable something in PHP called strict mode, which will help prevent situations like this. We'll learn more about that later.

egghead
egghead
~ 48 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today