PHP Type Casting
In this page:
$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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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) "");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
echo "5" + 3; // automatic juggling
echo "\n";
echo (int) "5" + 3; // explicit casting
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
(int) 9.9round होकर10बन जाएगा, जबकि casting zero की ओर truncate करती है और9देती है; अगर rounding चाहिए तोround()इस्तेमाल करें। (int) "abc"से user input cast करना और0को valid data मानना, जबकि non-numeric strings चुपचाप0बन जाती हैं।- यह मान लेना कि
(bool) "0"trueहै क्योंकि string non-empty है, जबकि"0"falseमें cast होता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: