PHP – Data types

PHP – Data types

 

PHP like most other programming languages supports various data types. It is very important to understand that variable cand act in a distinct way depending on the type of data they contain.

Here are all data types that PHP is using:

  • String – Simple text surrounded by commas
  • Integer – Mathematical whole numbers, without decimals
  • Float – Decimal or fractional numbers
  • Boolean – True/false values
  • Array – multiple values are stored inside this type of variable.
  • Object – It mainly stores information of how to process data
  • NULL – empty variable in PHP
  • Resource – it stores a reference to external functions and resources.

 

Now let’s try to explain with more details and work with some real-world examples.

PHP strings

Strings are the most common type of data. They normally contain simple text or numbers as text. The simplest way to assign string values is to wrap it into single or double quotes.

PHP

<?php
	$a = 'This is how you assign a simple string type value!';	 
	$b = "Another string type value using double quotes.";

	echo $a;
	echo "<br />";
	echo $b;
?>

Escaping quotes in strings

Simple or double quotes are lots of times found in blocks of text. To prevent code from breaking down we need to escape quotation marks like this

PHP

$a = 'Here is some awesome code that I'll be writing to escape quotation marks.';
echo $a;

Other ways to define strings

There are also other ways to define string values. Besides single and double quotation you can use heredoc and nowdoc syntax if you run PHP 5.3.0 or later. Here is a heredoc example:

PHP

$here = <<<EOT
    I'm here to show you how to use heredoc syntax!
EOT;

Nowdoc syntax similar to heredoc, but just like single quotes, it doesn’t allow using and evaluating variable inside it.

Limits on string type data

According to the PHP manual: (http://php.net/manual/en/language.types.string.php) “as of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum)”.

 

PHP integer data type

Integers are basically numbers without a decimal separator. You will normally use the decimal system to declare it but it is good to know that there are also other systems that can be used like hexadecimal (base 16 prefixed with 0x), octal (base 8 prefixed with 0) and since PHP 5.4+ you can also specify integers in binary (base 2 prefixed with 0b) system. You can also use the minus symbol (-) to define negative numbers

We will use the PHP var_dump build in function to print out the variable type and value:

PHP

$a = 3; // decimal number
var_dump($a) . PHP_EOL;
 
$b = -5; // a decimal negative number
var_dump($b) . PHP_EOL;
 
$c = 0x1F; // hexadecimal number 
var_dump($c) . PHP_EOL;
 
$d = 07; // octal number
var_dump($d) . PHP_EOL;

$d = 0b010; // binary number
var_dump($d) . PHP_EOL;

PHP float data type

Floats, doubles, or real numbers, are all the same and they all define numbers with a decimal separator or fractional numbers. This is the simplest form of a float point number:

PHP

$a = 10.14; // float number
var_dump($a) . PHP_EOL;

PHP bolean data type

Boolean data is a very basic data type. It has only possible values either true or false. You can also use the equivalent, 1 and 0

PHP

$is_home_page = true;
var_dump($is_home_page);

PHP array data type

The array data type is one of the most useful types of variable and it is normally used to store multiple values that are related to each other.

PHP

$cryptocurrencies = array("Bitcoin", "Ethereum", "Ripple", "Litecoin");
var_dump($cryptocurrencies);

Arrays like this assumes a numerical index for each values starting from 0. This is called indexed arrays and they offer you a way to directly access any values. They are very fast compared to other types of arrays, but this is subject of another tutorial.

PHP

echo $cryptocurrencies[2]; // prints Ripple

Associative arrays

Associative arrays are arrays that you manually or dynamically set the keys of the values.

PHP

$cryptocurrencies = array("BTC" => "Bitcoin", "ETH" => "Ethereum", "XRP" => "Ripple", "LTC" => "Litecoin");
var_dump($cryptocurrencies);
var_dump($cryptocurrencies["XRP"]);

To conclude, the array data type is more about structuring and storing data than about the type of data it stores on every key. It can contain strings, floats, boolean values or even multiples arrays (multidimensional arrays).

PHP object data type

Objects are more like a different way of programming. They are normally used to store functions that are later used to build data again and again.

PHP

<?php
class user {

    public $name = "Nick";
    
    function print_user(){
        return $this->name;
    }
}
 
$my_user = new user;
var_dump($my_user);
?>

PHP NULL data type

NULL data type is the value that PHP assign to a variable when his value is not defined. Please note that NULL is not the same as an empty value.

PHP

<?php
$not_setted_var;
var_dump($not_setted_var); // Thais has a value of NULL

$empty_var = "";
var_dump($empty_var); // this has a an empty value
?>

PHP resource data type

A resource is a special variable, holding a reference to an external resource like a database connection for example.

PHP

$conn = mysqli_connect(localhost,"root","admin","users");

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars