🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

PHP 7 Fundamental Tutorial for Beginners – PHP Data Types

The name itself indicating, data types means type of data.

The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects.

PHP supports total of eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource, and NULL. These data types are used to construct variables. Now let’s discuss each one of them in detail.

Types of data types in php:

PHP data types are mainly divided into 3 types:

  • Scalar Data Types: integer, string, Floating-Point Numbers or Doubles, Boolean
  • Composite Data Type: array, object
  • Special Data Types: Null, resource

Scalar Data Types: allow us to create variables or constants; which can hold a single value.

Integer: means whole number, i.e. number without floating point

Ex: 122, 2000, -400 etc.

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Integers</title>
</head>
<body>
<?php
$a = 123; // decimal number
var_dump($a);
echo "<br>";
$b = -123; // a negative number
var_dump($b);
echo "<br>";
$c = 0x1A; // hexadecimal number
var_dump($c);
echo "<br>";
$d = 0123; // octal number
var_dump($d);
?>
</body>
</html>

Double: Means decimal or float number, i.e. number containing floating Point.

Ex: 3.142, 9.8, -48.32 etc.

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Floats</title>
</head>
<body>
<?php
$a = 1.234;
var_dump($a);
echo "<br>";
$b = 10.2e3;
var_dump($b);
echo "<br>";
$c = 4E-10;
var_dump($c);
?>
</body>
</html>

String: indicates a sequence of characters enclosed within pair of double quotations or single quotations i.e. Textual data.

Ex: “Hello world” , ‘Hello JavaScript’ etc.

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Strings</title>
</head>
<body>
<?php
$a = 'Hello world!';
echo $a;
echo "<br>";
$b = "Hello world!";
echo $b;
echo "<br>";
$c = 'Stay here, I\'ll be back.';
echo $c;
?>
</body>
</html>

Boolean: indicates logical or conditional result Boolean data type has two values true and false value.

Note:
integers 0 and -0, double 0.0 and -0.0, string with value “, string with value “0”, Boolean false value, an array with zero elements, the special type NULL, and XML objects created from empty tags, are considered as false in PHP.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Booleans</title>
</head>
<body>
<?php
// Assign the value TRUE to a variable
$show_error = True;
var_dump($show_error);
?>
</body>
</html>

PHP Arrays

An array is a variable that can hold more than one value at a time. It is useful to aggregate a series of related items together, for example, a set of country or city names.

An array is formally defined as an indexed collection of data values. Each index (also known as the key) of an array is unique and references a corresponding value.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Arrays</title>
</head>
<body>
<?php
$colors = array("Red", "Green", "Blue");
var_dump($colors);
echo "<br>";
$color_codes = array(
"Red" => "#ff0000",
"Green" => "#00ff00",
"Blue" => "#0000ff"
);
var_dump($color_codes);
?>
</body>
</html>
view raw arry.php hosted with ❤ by GitHub

PHP Objects

An object is a data type that not only allows storing data but also information on, how to process that data. An object is a specific instance of a class which serve as templates for objects. Objects are created based on this template via the new keyword.

Every object has properties and methods corresponding to those of its parent class. Every object instance is completely independent, with its own properties and methods, and can thus be manipulated independently of other objects of the same class.

Here’s a simple example of a class definition followed by the object creation.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Objects</title>
</head>
<body>
<?php
// Class definition
class greeting{
// properties
public $str = "Hello World!";
// methods
function show_greeting(){
return $this->str;
}
}
// Create object from class
$message = new greeting;
var_dump($message);
?>
</body>
</html>

Special data Types: allow us to make a variable point to some external resource or not to point anywhere.

NULL: indicates nothing.
A variable of type objects points or refers to another memory location; if it should not point to any memory location then we assign a value null to it.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP NULL Value</title>
</head>
<body>
<?php
$a = NULL;
var_dump($a);
echo "<br>";
$b = "Hello World!";
$b = NULL;
var_dump($b);
?>
</body>
</html>

When a variable is created without a value in PHP like $var; it is automatically assigned a value of null. Many novice PHP developers mistakenly considered both $var1 = NULL; and $var2 = “”; are same, but this is not true. Both variables are different — the $var1 has null value while $var2 indicates no value assigned to it.

PHP Resources

Indicates variable is pointing to an external resource

A resource is a special variable, holding a reference to an external resource.

Resource variables typically hold special handlers to opened files and database connections.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Resources</title>
</head>
<body>
<?php
// Open a file for reading
$handle = fopen("note.txt", "r");
var_dump($handle);
?>
</body>
</html>

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.