Topics PHP Basics Variables & Data Types
beginner 12 min read

Variables & Data Types

Variables, data types, type casting, and variable scope in PHP.

PHP Variables

Variables in PHP start with $ followed by the name. PHP is dynamically typed.

$name = "Alice";
$age = 30;
$price = 19.99;
$active = true;

Data Types

  • string — text
  • int — whole numbers
  • float — decimal numbers
  • bool — true/false
  • array — ordered collections
  • object — class instances
  • null — no value
  • resource — external resources

Type Casting

$num = (int) "42";
$str = (string) 100;

Variable Scope

Variables declared outside a function are global. Use global keyword or $GLOBALS array to access them inside functions.

Examples

<?php
$name = "Alice";
$age = 30;
echo "$name is $age years old.";

Your Notes

Sign in to take notes for this lesson.

Quiz

PHP Basics Quiz

0 questions

Sign in to take quiz

Discussion

Sign in to join the discussion.

Flashcards

Sign in to create flashcards.