← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 12 of 13

PHP Type Casting

Type casting किसी juice को bottle से एक cup में डालने जैसा है: drink same रहती है लेकिन अब इसका shape अलग है। आप PHP को बताते हैं कि किसी value को, जैसे text five को, एक अलग kind की तरह treat करे, जैसे number five।
Syntax
php
$result = (int) $value;
$result = (float) $value;
$result = (string) $value;
$result = (bool) $value;
$result = (array) $value;

Type Casting क्या है?

Type casting PHP को explicitly बताना है कि किसी value को एक अलग type की तरह treat करे इससे पहले target type को parentheses में लिखकर, जैसे (int) $value।

यह एक deliberate, visible conversion है — PHP के automatic type juggling के उलट, जो चुपचाप तब होता है जब किसी operator को matching types के operands चाहिए।

उदाहरण: What is Type Casting?

php
<?php
// Declare `$value`, set to "42"
$value = "42";
// Declare `$intValue`, set to `(int) $value`
$intValue = (int) $value;
// Print a detailed dump (with types) of `$intValue`
var_dump($intValue);
?>

Integer और Float में Cast करना

(int) round करने के बजाय zero की ओर truncate करता है — (int) 9.9 10 नहीं, 9 देता है — और पहले non-numeric character पर एक string पढ़ना रोक देता है, इसलिए (int) "42abc" 42 है।

(float) same leading-numeric-prefix rule follow करता है लेकिन decimal हिस्सा preserve करता है।

उदाहरण: Casting to Integer and Float

php
<?php
// Print `(int) 9.9 . "\n"` to the output
echo (int) 9.9 . "\n";
// Print `(int) "42abc" . "\n"` to the output
echo (int) "42abc" . "\n";
// Print `(float) "3.14xyz"` to the output
echo (float) "3.14xyz";
?>

String और Boolean में Cast करना

(string) numbers और booleans को उनके textual form में convert करता है (true "1" बन जाता है, false "" बन जाता है), जो मायने रखता है क्योंकि PHP concatenation के दौरान यह automatically वैसे भी करता है।

(bool) 0, 0.0, "", "0", empty arrays, और null को false मानता है — बाकी हर चीज़, string "0.0" सहित, true count करती है।

उदाहरण: Casting to String and Boolean

php
<?php
// Print a detailed dump (with types) of `(string) true`
var_dump((string) true);
// Print a detailed dump (with types) of `(string) false`
var_dump((string) false);
// Print a detailed dump (with types) of `(bool) "0.0"`
var_dump((bool) "0.0");
// Print a detailed dump (with types) of `(bool) ""`
var_dump((bool) "");
?>

Array और Object में Cast करना

(array) एक scalar को एक single-element array में wrap कर देता है, या किसी object की public properties को key-value pairs में convert कर देता है।

(object) एक associative array के लिए उल्टा करता है, हर key को एक dynamic property में बदलते हुए — यह तब उपयोगी है जब किसी API को plain arrays के बजाय stdClass objects चाहिए।

उदाहरण: Casting to Array and Object

php
<?php
// Declare `$arr`, set to `(array) "hello"`
$arr = (array) "hello";
// Print a human-readable dump of `$arr`
print_r($arr);
// Declare `$obj`, set to `(object) ["name" => "Alice"]`
$obj = (object) ["name" => "Alice"];
// Print `$obj->name` to the output
echo $obj->name;
?>

Casting बनाम Automatic Type Juggling

PHP comparisons और arithmetic के दौरान चुपचाप types coerce करता है ("5" + 3 8 बन जाता है), जो convenient है लेकिन user input से loosely-typed data के साथ surprising results produce कर सकता है।

Explicit casting conversion को code में visible बनाता है और जब भी किसी value का type uncertain हो तब safer choice है।

उदाहरण: Casting vs Automatic Type Juggling

php
<?php
echo "5" + 3; // automatic juggling
echo "\n";
echo (int) "5" + 3; // explicit casting
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि (int) 9.9 round होकर 10 बन जाएगा, जबकि casting zero की ओर truncate करती है और 9 देती है; अगर rounding चाहिए तो round() इस्तेमाल करें।
  2. (int) "abc" से user input cast करना और 0 को valid data मानना, जबकि non-numeric strings चुपचाप 0 बन जाती हैं।
  3. यह मान लेना कि (bool) "0" true है क्योंकि string non-empty है, जबकि "0" false में cast होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.