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

PHP Data Types

Data types वे अलग-अलग तरह की information हैं जिन्हें PHP hold कर सकता है, जैसे whole numbers, decimals, words, और yes-or-no answers। यह toys को अलग-अलग boxes में sort करने जैसा है क्योंकि एक ball और एक puzzle एक जैसी चीज़ नहीं हैं।
Syntax
php
$integer = 42;
$float = 3.14;
$string = "text";
$boolean = true;
$array = [item1, item2];
var_dump($variable);   // shows type and value

Integers

एक integer बिना किसी decimal component के एक whole number रखता है, positive या negative — counts, IDs, ages।

PHP integers को एक fixed-size binary format में store करता है, इसीलिए बहुत बड़े numbers silently एक float में overflow हो सकते हैं; ज़्यादातर everyday values के लिए यह कभी मायने नहीं रखता।

उदाहरण: Integers

php
<?php
// Declare `$age`, set to `30`
$age = 30;
// Declare `$id`, set to `-5`
$id = -5;
// Print a detailed dump (with types) of `$age`
var_dump($age);
?>

Strings

एक string characters की एक sequence है — text — और इसे single या double quotes में wrap किया जाना चाहिए ताकि PHP को पता चले कि यह कहाँ शुरू और खत्म होती है।

Strings web work में PHP का सबसे ज़्यादा इस्तेमाल होने वाला type है, क्योंकि HTML output, form data, और database results fundamentally सब text हैं।

उदाहरण: Strings

php
<?php
// Declare `$text`, set to "Hello, world!"
$text = "Hello, world!";
// Print a detailed dump (with types) of `$text`
var_dump($text);
?>

Floats

एक float (जिसे double भी कहते हैं) एक decimal point वाला number store करता है, या इतना बड़ा/छोटा कि इसे exponential notation चाहिए हो।

Floats fractional values वाली किसी भी चीज़ के लिए ज़रूरी हैं — prices, measurements, averages — जहाँ एक integer precision खो देगा।

उदाहरण: Floats

php
<?php
// Declare `$price`, set to `19.99`
$price = 19.99;
// Print a detailed dump (with types) of `$price`
var_dump($price);
?>

Booleans

एक boolean exactly दो values में से एक रखता है, true या false, और यह आपके code में हर conditional check की backbone है। लगभग हर if statement आख़िरकार अपनी condition को एक boolean तक reduce कर देता है यह decide करने से पहले कि कौन सी branch चलानी है।

उदाहरण: Booleans

php
<?php
// Declare `$isActive`, set to `true`
$isActive = true;
// Print a detailed dump (with types) of `$isActive`
var_dump($isActive);
// Check whether `$isActive`
if ($isActive) {
    // Print "Every if reduces to a boolean." to the output
    echo "Every if reduces to a boolean.";
}
?>

Arrays

एक array एक single variable name के तहत कई values store करता है, या तो एक numerically indexed list की तरह या key-value pairs की तरह।

आप इसे array() function या shorter [] syntax से बना सकते हैं, और arrays वह तरीका हैं जिससे PHP shopping cart से लेकर database result row तक हर चीज़ represent करता है।

उदाहरण: Arrays

php
<?php
// Declare `$cart`, set to `array("Apple", "Bread", "Milk")`
$cart = array("Apple", "Bread", "Milk");
// Declare `$cart2` as an array: `["Apple", "Bread", "Milk"]`
$cart2 = ["Apple", "Bread", "Milk"];
// Print a detailed dump (with types) of `$cart`
var_dump($cart);
?>
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. एक number को quotes में store करना, जैसे $age = "25";, जो इसे एक string बना देता है; var_dump int(25) के बजाय string(2) दिखाता है।
  2. floats को == से compare करना, उदाहरण के लिए 0.1 + 0.2 == 0.3, जो binary floating-point rounding की वजह से false है।
  3. यह मान लेना कि "false" (एक non-empty string) falsy है, जबकि सिर्फ "" और "0" false हैं; if ("false") block चला देता है।

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.