⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

PHP Variables

Variables are the fundamental building blocks of any program, acting as containers for storing information. In PHP, every variable starts with the $ sign, followed by its name. The name must start with a letter or underscore, but not a number. It can also only contain alphanumeric characters and underscores (A-z, 0-9, and _).

EXAMPLE
RESULT
The result will be displayed here...

It is important to understand three key properties:

  1. Case sensitivity: The names $name and $Name are two completely different variables.
  2. Loose typing: PHP automatically recognizes the data type (string, number, etc.) based on the value assigned to the variable.
  3. Scope: Variables can be global (accessible anywhere using the global keyword) or local (accessible only within a function).

PHP Variables Cheat Sheet

Overview of built-in PHP functions for checking, modifying, and outputting variable information:

Name Description Example (Test Code)
boolval() Returns the boolean value of a variable. var_dump(boolval(1));
debug_zval_dump() Outputs internal variable structure with memory usage info (refcount). $x = "test"; debug_zval_dump($x);
doubleval() Alias for floatval(). Returns variable value as a decimal number. echo doubleval("10.55");
empty() Checks if a variable is empty (e.g., 0, "", NULL, false). var_dump(empty(""));
floatval() Returns variable value as a decimal number (float). echo floatval("3.14abc");
get_defined_vars() Returns an array of all currently defined variables. print_r(get_defined_vars());
get_resource_type() Returns resource type (e.g., database link or open file). $f = fopen("index.php", "r"); echo get_resource_type($f);
gettype() Returns data type name of a variable (e.g., string, integer, array). echo gettype(52);
intval() Returns integer value of a variable. echo intval("42.77");
is_array() Checks if a variable is an array. var_dump(is_array([1, 2]));
is_bool() Checks if a variable is boolean (true/false). var_dump(is_bool(true));
is_callable() Checks if variable content can be called as a function. var_dump(is_callable("gettype"));
is_countable() Checks if variable content is countable (array or object implementing Countable). var_dump(is_countable([1, 2, 3]));
is_double() Alias for is_float(). var_dump(is_double(1.5));
is_float() Checks if the variable is a decimal number. var_dump(is_float(12.34));
is_int() Checks if the variable is an integer. var_dump(is_int(100));
is_integer() Alias for is_int(). var_dump(is_integer(100));
is_iterable() Checks if a variable is iterable (array or object implementing Traversable). var_dump(is_iterable([1, 2]));
is_long() Alias for is_int(). var_dump(is_long(123456));
is_null() Checks if the variable is NULL. var_dump(is_null(null));
is_numeric() Checks if the variable is a number or numeric string. var_dump(is_numeric("42"));
is_object() Checks if a variable is an object. var_dump(is_object(new stdClass()));
is_resource() Checks if a variable is a resource. var_dump(is_resource(fopen("index.php", "r")));
is_scalar() Checks if the variable is a scalar (int, float, string, or bool). var_dump(is_scalar(3.14));
is_string() Checks if the variable is a string. var_dump(is_string("PHP"));
isset() Checks if the variable is defined and is not NULL. var_dump(isset($x));
print_r() Outputs readable info about a variable (useful for arrays and objects). print_r(["a", "b"]);
serialize() Converts variable into a string representation (storable format). echo serialize([1, 2, 3]);
settype() Changes variable type. $x = "5"; settype($x, "int"); var_dump($x);
strval() Returns string value of a variable. echo strval(123.45);
unserialize() Converts serialized string back into PHP value. print_r(unserialize('a:2:{i:0;s:1:"a";i:1;s:1:"b";}'));
unset() Deletes a variable. $x = 1; unset($x); var_dump($x);
var_dump() Outputs detailed info about one or more variables (type and value). var_dump(true, 42, "PHP");
var_export() Outputs or returns a string representation of a variable (valid PHP code). var_export(["x" => 10]);