PHP 课程

PHP Variables and Data Types

Python 3初学者教程。

PHP Variables

In PHP, all variable names start with a $ sign. PHP is dynamically typed — you don't need to declare a type:

<?php
$name = "Alice";       // string
$age = 30;             // integer
$price = 9.99;         // float
$isActive = true;      // boolean
$nothing = null;       // null

echo $name;    // Alice
echo $age;     // 30

Data Types

<?php
$str = "Hello, World!";   // string
$int = 42;                // integer
$float = 3.14;            // float/double
$bool = false;            // boolean
$arr = [1, 2, 3];         // array
$obj = new stdClass();    // object
$null = null;             // null

// Check type
echo gettype($str);   // string
echo gettype($int);   // integer

Type Juggling

<?php
// PHP automatically converts types in operations
$result = "5" + 3;     // 8 (string "5" becomes int)
$result = "5 dogs" + 2; // 7 (parses leading number)

// Strict comparison
var_dump(0 == "foo");  // bool(true) — loose
var_dump(0 === "foo"); // bool(false) — strict (type must match)

Constants

<?php
define("MAX_SIZE", 100);
define("SITE_NAME", "pythonlib.ru");

const PI = 3.14159;

echo MAX_SIZE;    // 100
echo PI;          // 3.14159

var_dump and print_r for debugging

<?php
$data = ["name" => "Alice", "age" => 30];
var_dump($data);   // detailed type + value info
print_r($data);    // human-readable array structure