Char тип данных php

PHP Data Types

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

  • String
  • Integer
  • Float (floating point numbers — also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP String

A string is a sequence of characters, like «Hello world!».

A string can be any text inside quotes. You can use single or double quotes:

Example

PHP Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

  • An integer must have at least one digit
  • An integer must not have a decimal point
  • An integer can be either positive or negative
  • Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation

In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

Example

PHP Float

A float (floating point number) is a number with a decimal point or a number in exponential form.

In the following example $x is a float. The PHP var_dump() function returns the data type and value:

Example

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE.

Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.

PHP Array

An array stores multiple values in one single variable.

In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

Example

You will learn a lot more about arrays in later chapters of this tutorial.

PHP Object

Classes and objects are the two main aspects of object-oriented programming.

A class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Let’s assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.

When the individual objects (Volvo, BMW, Toyota, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Example

class Car public $color;
public $model;
public function __construct($color, $model) $this->color = $color;
$this->model = $model;
>
public function message() return «My car is a » . $this->color . » » . $this->model . «!»;
>
>

$myCar = new Car(«black», «Volvo»);
echo $myCar -> message();
echo «
«;
$myCar = new Car(«red», «Toyota»);
echo $myCar -> message();
?>

PHP NULL Value

Null is a special data type which can have only one value: NULL.

A variable of data type NULL is a variable that has no value assigned to it.

Tip: If a variable is created without a value, it is automatically assigned a value of NULL.

Variables can also be emptied by setting the value to NULL:

Example

PHP Resource

The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.

A common example of using the resource data type is a database call.

We will not talk about the resource type here, since it is an advanced topic.

Источник

PHP Data Types

Summary: in this tutorial, you will learn about PHP data types including scalar types, compound types, and special types.

Introduction to PHP data types

A type specifies the amount of memory that allocates to a value associated with it. A type also determines the operations that you can perform on it.

PHP has ten primitive types including four scala types, four compound types, and two special types:

Scalar types

Compound types

Special types

Scalar types

A variable is a scalar when it holds a single value of the type integer, float, string, or boolean.

Integer

Integers are whole numbers defined in the set . The size of the integer depends on the platform where PHP runs.

The constant PHP_INT_SIZE specifies the size of the integer on a specific platform. PHP uses the int keyword to denote the integer type.

The following example illustrates some integers:

 $count = 0; $max = 1000; $page_size = 10;Code language: HTML, XML (xml)

Float

Floats are floating-point numbers, which are also known as floats, doubles, or real numbers.

PHP uses the IEEE 754 double format to represent floats. Like other programming languages, floats have limited precision.

PHP uses the float keyword to represent the floating-point numbers. The following example illustrates the floating-point numbers in PHP:

 $price = 10.25; $tax = 0.08;Code language: HTML, XML (xml)

Boolean

Boolean represents a truth value that can be either true or false . PHP uses the bool keyword to represent the Boolean type.

The bool type has two values true and false . Since keywords are case-insensitive, you can use true , True , TRUE , false , False , and False to indicate boolean values.

The following example shows how to assign Boolean values to variables:

 $is_admin = true; $is_user_logged_in = false;Code language: HTML, XML (xml)

When you use the values of other types in the boolean context, such as if-else and switch-case statements, PHP converts them to the boolean values.

PHP treats the following values as false :

  • The false keyword.
  • The integer 0 and -0 (zero).
  • The floats 0.0 and -0.0 (zero).
  • The empty string ( «» , » ) and the string “0”.
  • The empty array ( array() or [] ).
  • The null .
  • The SimpleXML objects created from attributeless empty elements.

The values that are not one of these falsy values above are true .

String

A string is a sequence of characters surrounded by single quotes (‘) or double quotes (“). For example:

 $str = 'PHP scalar type'; $message = "PHP data types";Code language: HTML, XML (xml)

Compound types

Compound data includes the values that contain more than one value. PHP has two compound types including array and object.

Array

An array is an ordered map that associates keys with values. For example, you can define a list of items in a shopping cart like this:

 $carts = [ 'laptop', 'mouse', 'keyboard' ];Code language: HTML, XML (xml)

The $carts array contains three string values. It maps the index 0 , 1 , and 2 to the values ‘laptop’ , ‘mouse’ , and ‘keyboard’ . The $carts is called an indexed array because it uses numeric indexes as keys.

To access a value in an array, you use the square brackets:

 echo $carts[0]; // 'laptop' echo $carts[1]; // 'mouse' echo $carts[2]; // 'keyboard'Code language: HTML, XML (xml)

Besides numeric indexes, you can use strings as keys for the array elements. These arrays are known as associative arrays. For example:

 $prices = [ 'laptop' => 1000, 'mouse' => 50, 'keyboard' => 120 ];Code language: HTML, XML (xml)

To access an element in an associative array, you specify the key in the square brackets. For example:

 echo $prices['laptop']; // 1000 echo $prices['mouse']; // 50 echo $prices['keyboard']; // 120Code language: HTML, XML (xml)

Object

An object is an instance of a class. It’s a central concept in object-oriented programming.

An object has properties. For example, a person object may have the first name, last name, and age properties.

An object also has behaviors, which are known as methods. For example, a person object can have a method called getFullName() that returns the full name.

To learn more about objects, check out the object tutorial.

Special types

PHP has two special types: null and resource

Null

The null type has one value called null that represents a variable with no value.

Resource

The resource type holds a reference to an external resource, e.g. a filehandle or a database connection.

Summary

  • PHP has four scalar types, four compound types, and two special types.
  • Scale types: integer, float, string, and boolean.
  • Compound types: array and object.
  • Special types: null and resource.

Источник

Читайте также:  Javascript вывод document write
Оцените статью