Posted on Leave a comment

Crash Course Into PHP – Data Types

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

PHP Data Types

PHP variables can store several different data types. Different data types can perform different functions, the basic data types includ:

  • String.e
  • Integer.
  • Float (floating point numbers – also called double)
  • Boolean.
  • Array.
  • Object.
  • NULL.

String

A string is a series of characters wrapped in either single or double quotes, a few examples of strings include:

  • $x = 'Hello my name is Jyrone';
  • $x = "Programming is awesome!"
  • $x = 'The number 13 is very unlucky'

Integer

An integer is any NON-DECIMAL number between -2,147,483,648 and 2,147,483,647 some examples include

  • $x = 1234;
  • $x = -4567;

Float

A float (floating point number) is a number with a decimal point or a number in exponential form some examples include:

  • $x = 1.234
  • $x =1.2e3;
  • $x = 9E-10

Boolean

The boolean data type only has two possible states, true(1) or false(0) some examples include:

  • $x = true;
  • $x = false;

Array

The array data type maps several values to a single variable. Arrays can be initialized with the array() function or the [] shorthand syntax. Arrays can contain other arrays, some examples include:

  • $x = [
    "a" => 1,
    "b" => "xyz"
    ];
  • $x = [
    "a" => [
    "a1" => 2
    ],
    "b" =>345
    ];

Object

The object data type is arguably the most important data type. An object stores data in its properties, and manipulates that data using methods. Objects have to be declared explicitly using the class keyword, a class is a structure that contains methods and properties, an example would be:


class Person{
protected $height, $weight;
public function __construct( $height,$weight){
$this->height = $height;
$this->weight = $weight;
}
public function getWeight(){
return $this->weight;
}
public function getHeight(){
return $this->height;vb
}
public function setWeight($weight){
$this->weight = $weight;
}
public function setHeight($height){
$this->height = $height;
}
}
$person = new Person(70,163);
//returns 70
echo $person->getHeight();

Objects and classes will be covered more in depth as time goes on.

Null

The null data type can only be assigned one value, and that is you guessed it, null. You use null when no value has yet been assigned to a variable:

  • $x = null;

Conclusion

As you progress through my tutorials I will go deeper and deeper into each data type as the need arises in code. Next I will explain the basic operators of PHP. If you haven’t already please subscribe to my newsletter to get real time updates on my blog entries.