PHP Beginner

PHP Variables and Data Types

CodingerWeb
CodingerWeb
33 views 45 min read

Understanding PHP Variables and Data Types

Variables are containers that store data values. In PHP, variables are dynamic and can hold different types of data.

Variable Basics

PHP variables start with the $ symbol:

<?php
$name = "John Doe";
$age = 25;
$height = 5.9;
$isStudent = true;

echo $name; // Outputs: John Doe
?>

Variable Naming Rules

  • Must start with $ followed by a letter or underscore
  • Can contain letters, numbers, and underscores
  • Case-sensitive ($name ≠ $Name)
  • Cannot start with a number
<?php
// Valid variable names
$firstName = "John";
$_age = 25;
$user1 = "admin";

// Invalid variable names
// $1user = "invalid";  // Starts with number
// $first-name = "invalid";  // Contains hyphen
?>

PHP Data Types

1. String

<?php
$singleQuote = 'Hello World';
$doubleQuote = "Hello World";
$multiline = "This is a
multi-line string";

// String concatenation
$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName;
echo $fullName; // John Doe
?>

2. Integer

<?php
$positiveInt = 123;
$negativeInt = -456;
$hexInt = 0x1A; // Hexadecimal
$octInt = 0123; // Octal

var_dump($positiveInt); // int(123)
?>

3. Float (Double)

<?php
$price = 19.99;
$scientific = 2.5e3; // 2500
$negative = -3.14;

echo $price; // 19.99
?>

4. Boolean

<?php
$isTrue = true;
$isFalse = false;

// Boolean in conditions
if ($isTrue) {
    echo "This will execute";
}
?>

5. Array

<?php
// Indexed array
$fruits = array("apple", "banana", "orange");
// or
$colors = ["red", "green", "blue"];

// Associative array
$person = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);

echo $fruits[0]; // apple
echo $person["name"]; // John
?>

6. NULL

<?php
$nullVar = null;
$undefinedVar; // Also null

if (is_null($nullVar)) {
    echo "Variable is null";
}
?>

Type Checking Functions

<?php
$value = "123";

// Check data types
var_dump(is_string($value)); // bool(true)
var_dump(is_int($value));    // bool(false)
var_dump(is_numeric($value)); // bool(true)

// Get variable type
echo gettype($value); // string
?>

Type Conversion

<?php
// Automatic conversion
$num = "10" + 5; // 15 (string becomes int)

// Manual conversion
$str = "123";
$int = (int)$str;
$float = (float)$str;
$bool = (bool)$str;

// Using functions
$converted = intval("456");
$floatVal = floatval("78.9");
?>

Variable Scope

<?php
$globalVar = "I am global";

function testScope() {
    $localVar = "I am local";
    global $globalVar; // Access global variable
    echo $globalVar;
}

testScope();
// echo $localVar; // Error: undefined variable
?>

Practice Exercise

Create variables for:

  • Your name (string)
  • Your age (integer)
  • Your height (float)
  • Whether you like PHP (boolean)
  • Your hobbies (array)

Display all variables and their types using var_dump().

Key Takeaways

  • Variables in PHP start with $
  • PHP is dynamically typed
  • Use meaningful variable names
  • Understand different data types and when to use them