PHP

PHP Menu

PHP supports eight primitive data types in total: integer, floating point number or float, string, boolean, array, object, resource, and null. Variables are assigned values with these data types.

Integers

Integers are whole numbers that do not contain a decimal point (..., -2, -1, 0, 1, 2, ...). Integers can be specified in decimal (base 10) notation, hexadecimal (base 16 - prefixed with 0x) notation, or octal (base 8 - prefixed with 0) notation, with or without a sign (- or +).

<?php
$w = 123; var_dump($w); echo "<br>";
$x = -123; // a negative number var_dump($x); echo "<br>";
$y = 0x1A; // hexadecimal number var_dump($y); echo "<br>";
$z = 0123; // octal number var_dump($z); echo "<br>";

Strings

Strings are sequences of characters, where every character is the same as a byte. It can hold letters, numbers, and special characters. Specify a string by enclosing it in single quotes (e.g., 'Hello world!'), or double quotes ("Hello world!").

<?php
$x = 'Hello world!'; echo $x; echo "<br>";
$y = "Hello world! \n"; echo $y; echo "<br>";
$z = 'Stay here, I\'ll be back.'; echo $z; echo "<br>";

Floating Point Numbers or Double

Floating point numbers (also known as "floats," "doubles," or "real numbers") are decimal or fractional numbers, as shown in the following example.

<?php
$x = 1.234; var_dump($x); echo "<br>";
$y = 10.2e3; var_dump($y); echo "<br>";
$z = 4E-10; var_dump($z); echo "<br>";

Booleans

A Boolean represents two possible states: true or false.

<?php
// Assign the value true to a variable $myBool = true; var_dump($myBool); echo "<br>";

Arrays

An array is a variable that can hold multiple values at once. It is useful to group together a group of related items.

A formal definition of an array is an indexed collection of data values. An array's index (also known as the key) is unique and points to a corresponding value.

<?php
$myColors = array("Red", "Green", "Blue"); var_dump($myColors); echo "<br>";
$color_array = array( "Red" => "#ff0000", "Green" => "#00ff00", "Blue" => "#0000ff" ); var_dump($color_array); echo "<br>";

Objects

An object is a data type that not only allows for the storage of data but also for the storage of instructions on how to process that data. An object is a specific instance of a class that acts as a template for other objects. The new keyword is used to create objects based on this template.

Every object has the same properties and methods as its parent class. Each object instance is completely self-contained, with its own set of properties and methods, and can thus be manipulated independently of other objects of the same class.

The following is an example of a class definition and object creation.

<?php
// Class definition class Hello { // properties public $str = "Hello World!";
// methods function say_hello(){ return $this->str; } }
// Create object from class $message = new Hello; var_dump($message);

null

A variable of type null is a variable without any data.

<?php
$x = null; var_dump($x); echo "<br>";
$y = "Hello World!"; $y = null; var_dump($y); echo "<br>";

Resources

A resource is a special variable, holding a reference to an external resource. It typically holds special handlers to opened files and database connections.

<?php // Open a file for reading $handle = fopen("mynotes.txt", "r"); var_dump($handle);
// Connect to MySQL database server with default setting $link = mysqli_connect("localhost", "root", ""); var_dump($link);
{ "error_message":"Sorry, file handling is not supported." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods