Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 3: Type Casting of Variables

Lesson 4 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To perform explicit type casting of PHP variables between scalar and compound types — string, int, float, bool, array and object — and verify the exact conversion rules the engine applies.

Theory

PHP is dynamically typed: a variable's type is whatever its current value is, and the engine freely performs type juggling (implicit conversion) inside expressions. Explicit casting overrides this with a cast operator placed before the value: (int), (float), (string), (bool), (array), (object).

Conversion rules exercised by this practical:

  • String → int: PHP parses the leading numeric portion and stops at the first invalid character, so (int) "123.45" yields 123 — the fraction is truncated, never rounded.
  • String → float: full numeric parse — (float) "123.45" gives 123.45.
  • → bool: exactly these values become false: 0, 0.0, "", "0", null, []. Everything else — including "0.0" and "false" — becomes true.
  • Scalar → array: the value is wrapped as element [0 => value].
  • Associative array → object: produces a stdClass whose properties are the array keys, read with the -> operator.
  • Related tooling: settype($var, "integer") converts in place; gettype() / var_dump() inspect types; intval(), floatval(), strval() are the function forms; declare(strict_types=1) switches scalar parameter coercion off so mismatches throw TypeError.

Requirements

  • XAMPP with PHP 8.x, or the PHP CLI
  • VS Code (or any editor), browser or terminal

Procedure

  1. Start Apache in the XAMPP Control Panel.
  2. Save the snippet as p03_type_casting.php inside C:\xampp\htdocs\wbplab.
  3. Open http://localhost/wbplab/p03_type_casting.php in the browser (optionally wrap output in <pre> for clean formatting) or run php p03_type_casting.php.
  4. Experiment: cast "12abc", "abc", "0" and 3.99 to different types, predicting each result before running.

Explanation of the Code

  • $numString = "123.45" is the source value. $asInt = (int) $numString truncates to 123, while $asFloat = (float) $numString preserves 123.45.
  • $asString = (string) $asInt converts the integer back to the text "123" — the round trip has permanently lost the decimal part.
  • $asBool = (bool) $boolString casts "1" to true; the ternary prints the word true because echo would otherwise render boolean true as a bare 1.
  • $asArray = (array) $asInt produces [0 => 123], displayed with print_r().
  • $asObject = (object) ["name" => "Aman", "age" => 20] builds a stdClass; the interpolated string reads its members as $asObject->name and $asObject->age.

Expected Output

  • Original string: 123.45
  • Cast to int: 123
  • Cast to float: 123.45
  • Int back to string: 123
  • String to bool: true
  • Int to array: Array ( [0] => 123 )print_r spreads this over several lines
  • Array to object sample: Aman, 20

🎯 Viva Questions

  1. What does (int) "123.45" return? 123 — leading numeric parse followed by truncation toward zero.
  2. List the values that cast to false. 0, 0.0, "", "0", null, [].
  3. Casting vs settype()? A cast returns a converted copy; settype() mutates the variable in place.
  4. Which class does (object) create? stdClass, PHP's generic empty class.
  5. Type juggling vs type casting? Juggling is implicit engine conversion inside expressions; casting is programmer-forced.
  6. What does declare(strict_types=1) change? Scalar type declarations stop coercing; a wrong argument type throws a TypeError.

CO Mapping

CO1, CO2