Data types and operators: primitive types (string, int, float, boolean); arrays and objects; type casting and juggling; operators (arithmetic, logical, comparison, assignment)

PHP's types (string, int, float, bool, array, object) and its operators, plus type juggling: where "5" quietly becomes 5, so == is loose and === is strict.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The string that added up

The event portal reads the number of seats from a form. Form data always arrives as a STRING, so $seats holds "350", with quotes. Yet this works:

echo "350" + 10; // prints 360

In Java that would be a type error; in PHP it just... works, because PHP quietly turned the string into a number. This helpful-but-dangerous behaviour is called type juggling, and understanding it (and the == vs === distinction it creates) is the heart of this lesson.

At a glance

PHP data types

TypeHoldsExample
stringText$e = "Garba Night";
intWhole numbers$seats = 350;
floatDecimals$fee = 49.5;
booltrue / false$open = true;
arrayAn ordered/keyed collection$list = ["Riya", "Aman"];
objectAn instance of a class$user = new User();

Theory

Type juggling and casting

PHP is dynamically typed: a variable's type follows whatever value it holds, and can change.

Type juggling is PHP AUTOMATICALLY converting types during an operation: "5" + 3 juggles the string "5" to the number 5, giving 8. Convenient, but it can surprise you.

Type casting is doing it EXPLICITLY: (int)"5abc" gives 5 (it reads leading digits), (int)"abc" gives 0, (float)"3.5" gives 3.5.

Because form input is always strings, juggling is why arithmetic on form data often just works, and casting is how you make the conversion deliberate and safe.

Practical

Operators and the == vs === trap (predict each)

<?php
  echo 10 % 3;          // 1   (modulus: remainder)
  echo 2 ** 3;          // 8   (exponent)
  echo "5" + 3;         // 8   (juggling: string to number)

  $msg = "Seats: ";
  $msg .= 350;          // .= appends -> "Seats: 350"
  echo $msg;

  var_dump("5" == 5);   // bool(true)   loose: juggles, then compares
  var_dump("5" === 5);  // bool(false)  strict: different types
?>

Theory

== is loose, === is strict

This is the single most important operator idea in PHP:

  • `==` (loose equality) juggles types BEFORE comparing, so "5" == 5 is true and even odd cases like 0 == "" can surprise you
  • `===` (identical) compares value AND type with NO juggling, so "5" === 5 is false (string vs int)

Because juggling can produce unexpected trues, the professional habit is to prefer `===` when you want a reliable comparison. The same applies to != (loose) versus !== (strict). When comparing a form's string to an expected value, === protects you from PHP's helpfulness backfiring.

Quiz

What do var_dump("5" == 5); and var_dump("5" === 5); print, respectively?

  1. true and true
  2. true and false: == juggles the string to a number (equal), === also checks type (string vs int, not identical)
  3. false and false
  4. false and true
Show the answer

true and false: == juggles the string to a number (equal), === also checks type (string vs int, not identical)

== is LOOSE: it juggles "5" to the number 5, so "5" == 5 is true. === is STRICT: it compares value AND type, and a string is not an int, so "5" === 5 is false. This is THE PHP comparison distinction, and prefering === avoids the surprises loose juggling can cause. Option A forgets === checks type. Option C forgets == juggles. Option D reverses both. Remember: == asks 'equal after juggling?', === asks 'same value and same type?'.

Think first

Why does form validation prefer ===?

The portal checks if a submitted role equals the string "admin". Why might using == here be risky, and === safer? Reason about juggling, then tap.

Show the answer

With ==, PHP juggles types before comparing, which can produce unexpected trues in edge cases (for example, comparisons involving 0 or empty values behave surprisingly under loose rules). For a security-sensitive check like 'is this user an admin?', an unexpected true is a disaster. === compares value AND type with no juggling, so it only returns true when the input is EXACTLY the string "admin": no surprises. The rule, especially for validation and authorization: use === (and !==) so PHP's automatic type conversion cannot backfire into a wrong match. Loose equality is a convenience you switch off where correctness matters.

Watch out

Type and operator traps

== surprises: loose equality juggles types; prefer === for reliable comparisons.

.= vs +=: .= appends to a STRING; += adds NUMBERS: mixing them up corrupts data.

Casting non-numeric strings: (int)"abc" is 0, not an error: validate before trusting.

Assuming form data is numeric: $_GET/$_POST values are STRINGS; cast or validate before math.

/ always gives a result: 10 / 3 is a float 3.333...; use intdiv() or % for whole-number needs.

Theory

Types in hand, now logic

You can now store and compute with every kind of data the portal needs, and you understand the juggling that makes PHP both convenient and occasionally tricky. Next the portal needs to make DECISIONS and REPEAT (if/switch, loops) and, crucially, to hold LISTS: the registration list is an ARRAY. Control structures and arrays, with PHP's powerful array functions, come next.

Summary

Key takeaways

  • PHP types: string, int, float, bool, array, object (and null); dynamically typed (type follows value).
  • Type juggling auto-converts types in operations: "5" + 3 = 8; type casting ((int)"5abc" = 5) is explicit.
  • Operators: arithmetic (+ - / % *), assignment (=, +=, .= appends strings), comparison, logical (&& || !).
  • == is loose (juggles types, so "5" == 5 is true); === is strict (value AND type, so "5" === 5 is false).
  • Prefer === (and !==) for reliable comparisons, especially in validation and authorization.
  • Form data arrives as strings: cast or validate before doing math.
  • Memory hook: == juggles, === does not.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Core PHP Programming

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Data types and operators: primitive types (string, int, float, boolean); arrays and objects; type casting and juggling; operators (arithmetic, logical, comparison, assignment) · Web Framework and Services (Major-12) · Gri-Learn