← العودة إلى الدورة: PHP from scratch

Variables, constants and data types

الفيديو قريبًا

A variable is a name that stores a value so you can use it later. In PHP every variable starts with a dollar sign, and you do not have to declare it first: it is created when you assign something with the equals sign. Names are case sensitive, so $age and $Age are different variables. Use clear lowercase English names, for example $user_name or $userName, and pick one style for the whole project.

php
<?php

$name = 'Ana';
$age = 31;
$height = 1.68;
$is_student = true;
$nothing = null;

echo $name . ' is ' . $age . ' years old' . "\n";
var_dump($age, $height, $is_student, $nothing);

var_dump is your best friend for learning: it shows the value and its exact type. You will see int(31), float(1.68), bool(true) and NULL. PHP's four scalar types are int (whole numbers), float (decimals), string (text) and bool (true or false). There are also null (no value), array (collections) and object (class instances, which we will see in week 6).

Dynamic typing and conversion

PHP is dynamically typed: the type belongs to the value, not the variable, and the same variable can hold a number now and a text later. This is flexible but risky, because PHP converts types automatically in some operations. You can convert explicitly with a cast, writing the type in parentheses before the value, or with functions such as intval. Explicit conversion makes your intent clear and avoids surprises.

php
<?php

$text = '42';
$number = (int) $text;      // 42 as int
$decimal = (float) '3.14'; // 3.14 as float
$flag = (bool) 'hello';    // true
$empty = (bool) '';        // false
$as_text = (string) 99;    // '99'

var_dump($number, $decimal, $flag, $empty, $as_text);
var_dump(gettype($number), is_int($number), is_string($as_text));

// Values PHP treats as false: false, 0, 0.0, '', '0', [], null
var_dump((bool) '0');

Beware of the text '0': although it looks true, PHP considers it false. The values PHP treats as false are false, 0, 0.0, the empty string, '0', an empty array and null. Everything else is true. Learning this list will save you bugs in conditions with form data.

Constants

A constant stores a value that does not change while the program runs. It is defined with the const keyword (no dollar sign) and by convention written in uppercase. It is useful for settings, limits or repeated texts. Trying to reassign it causes an error. PHP also offers magic constants such as __LINE__, __FILE__ and __DIR__, useful for debugging and for building paths.

php
<?php

const TAX_RATE = 0.21;
const SITE_NAME = 'My Shop';

$price = 100;
$total = $price * (1 + TAX_RATE);

echo SITE_NAME . ': ' . $total . "\n";   // My Shop: 121
echo 'Running ' . basename(__FILE__) . ' at line ' . __LINE__ . "\n";

Single and double quotes

With double quotes PHP replaces variables inside the text (interpolation) and interprets sequences such as \n. With single quotes the text is kept literal. For expressions or array elements inside double quotes use curly braces. Use single quotes by default and double quotes when you need interpolation.

php
<?php

$user = 'Luis';
$points = 7;

echo "Hello $user, you have {$points} points\n";
echo 'Hello $user\n';  // prints exactly: Hello $user\n
echo "\n";

✎ ملاحظاتي

محفوظ في هذا المتصفح فقط. سجّل الدخول لحفظه في حسابك.0/5000 حرف

← السابقالتالي →