PHP String
In PHP, a string is a sequence of characters enclosed in quotes. You can use single quotes (‘ ‘) or double quotes (” “) to define a string. Here’s an example of a string in PHP:
$name = "Alice";
In this case, $name is a string variable that contains the value “Alice”.
You can concatenate strings in PHP using the period (.) operator:
$first_name = "Alice"; $last_name = "Smith"; $full_name = $first_name . " " . $last_name; echo $full_name;
This will output the string “Alice Smith” to the web page or console.
You can also use double quotes to include variables directly in a string:
$name = "Alice"; echo "Hello, $name!";
This will output the string “Hello, Alice!” to the web page or console.
You can access individual characters in a string using square brackets and the character’s index (starting from 0):
$name = "Alice"; echo $name[0]; // outputs "A"
You can also use a number of built-in functions to manipulate strings in PHP. For example, the strlen() function returns the length of a string:
$name = "Alice"; echo strlen($name); // outputs 5
Other string functions include strpos() , which finds the position of a substring within a string, and substr() , which extracts a substring from a string.
Overall, strings are a fundamental data type in PHP and are used extensively in web development for tasks like storing user input, generating dynamic content, and manipulating data.
PHP Integer
In PHP, an integer is a whole number that can be either positive or negative. Integers are commonly used in mathematical operations and comparisons.
To define an integer in PHP, you can simply assign a whole number to a variable:
$num = 42; // positive integer $negative_num = -10; // negative integer
PHP automatically determines the data type of a variable based on its value, so the variables $num and $negative_num in the example above are automatically assigned the integer data type.
You can perform mathematical operations on integers using the standard arithmetic operators like + , – , * , and / . Here’s an example:
$num1 = 10; $num2 = 20; $sum = $num1 + $num2; // $sum is now 30
You can also use comparison operators like > , < , >= , and <= to compare integers. Here’s an example:
$num1 = 10; $num2 = 20; if ($num1 < $num2) { echo "$num1 is less than $num2"; }
This code will output “10 is less than 20”.
It’s important to note that in PHP, integers have a range limit depending on the platform and the system architecture. On most 32-bit systems, the maximum integer value is 2,147,483,647, while on 64-bit systems, the maximum integer value is 9,223,372,036,854,775,807.
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:
<?php $x = 10.365; var_dump($x); ?>
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true; $y = 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:
<?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?>
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.
<?php 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 "<br>"; $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:
<?php $x = "Hello world!"; $x = null; var_dump($x); ?>
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.