Skip to main content

Some Variable Data Types In PHP

Published 2 min readMarkdown
Some Variable Data Types In PHP

Variables are capable of storing many types of data.

PHP supports all the typical types of data types that other programming languages support. These data types include: Integer Float (floating-point numbers — also known as double) String Boolean Array Object NULL Resource

PHP Integer A PHP integer is any non-decimal number between -2,147,483,648 and 2,147,483,647. Some of the rules that integers must follow are: -An integer must have one or more digits -There must be no digits after the invisible decimal -An integer may be either positive or negative <?php $a = 123; echo $a; ?>

Result

1*WNxjrU4CkjOoR6btXehi-w.png PHP Float A float is any positive or negative number. Unlike integers, you can have digits after the decimal point. Example

<?php
$a = 123.123;
echo $a;
?>

Result

2.png

PHP String

A string is any sequence of letters such as “Hi, how are you”. The entire text inside of the quotation is considered a string.

<?php $a = "word"; echo $a; ?>

Result 3.png

PHP Boolean This means the value can only be either TRUE or FALSE <?php $a = TRUE; echo $a; ?>

Result 4.png

Some Other Variable Types Some other types of commonly used variables include arrays, objects, NULL and resources.

*PHP Array An array means you are storing more than one values in the variable. Arrays are explained in more detail here.

PHP Object

An object is an instance of a class. An object can do two purposes: it can store data(often multiple values) and it can also process data in different way. To use an object, you must explicitly declare it. You must also have a particular class already created.

/***You can learn more about PHP classes here ***/

Example

class Person {
  public $name;


  function __construct($name) {
    $this->name = $name; 
  }
  function get_name() {
  echo $this->name;
  }
}

$person = new Person("John Smith");
 $person->get_name();
 ?>

Result

Screen Shot 2021-10-14 at 12.56.51 PM.png

In addition, a variable can also be null or a resource. PHP NULL

Null is a special type of data that can only have the value of NULL. By default, any variable that is not set has a value of NULL. You are free to set any variable’s value to NULL in order to get rid of it. Any variable that hasn’t been created also has a type and value of NULL.

PHP Resource

A resource is something returned by a built-in function. For example, a database connection or a file connection or a directory. Each resource has its own unique behaviour and is better understood individually. Resources are considered an advanced topic, so it is better you come back to this topic at a later time.

About the author

  • I'm a full-stack web developer who loves building new projects and learning new things

    It might not show from my posts, but I'm also quite funny.

No comments yet. Be the first to comment.

More from this blog