← back to the blog


How many data types has PHP? PhP vs Javascript primitive data types.

Posted on September 28th, 2018 in PhP by George

You can not be a programmer without properly understandig the data types of the program language you are using. Today we going to talk about data types in PhP and Javascript, enumerate all of the and add a short description/comment for each.

 

PhP Data Types 

// PHP has 4 scalar types
boolean
integer
floar
string

//Php has 4 compound types
array
object
callable
iterable
//Php special types
NULL
resource
 

 

1.Boolean 

Boolean is simple. It can be either True or False. It is importan to know tha this values are case-insensitive. 

In PhP Boolean value can be converted to integer using int or integer cast.

False is coerced to 0 and True to 1.

 

2.Integer 

Integer it is a number under the form of {..., -1, 0 , 1 , 2 ,.. }. Integer can be decimal, base 10, hexadecimal, base 16, octal or binary. 

3.Float or real number

Appear under the form $a =1.25 ; $z =  3.66; $c = 7E -8;  

 

4.String 

String appear under the form echo ' Basic string'. It is a series of characters and a character is equivalent to a byte. This makes PHP to support only a 256-character set. Strings are encoded in the way the script is specifying. String can be concatenated using dot/period concatenation 

$str = 'literal' . $n ;

 

5. Array  

In php array it is an ordered map, associates values to keys. Array in php can be a tree structure or multdimensional. Array can be created using array() or [ ] brakets as o PHP 5.4. 

$test = ['foo' => 'restult', 'some' => '23434'];

The key can be an integer or a string and the value can be of any type.

 
$array = [
"foo" => "bar",
"bar" => "fgfoo",
];

var_dump($array);
 

 

will output:


array(2) { ["foo"]=> string(3) "bar" ["bar"]=> string(5) "fgfoo" }

 

6. Object

In order to have an object you have to use new statement to instantiate a predefined class.

Se example bellow:

 
class today {
function learning(){
echo 'I am learning
about php object';
}
}

$today = new today;
$today -> learning();
 

 

7. Callables 

This tipe is similar to declaring a function in Javascript. It can be a method for an object as well.

I php does come in this form: 

 
function runme(){
echo 'I been running
.... so far ...';
}

call_user_func('runme');
 

 

8.Iterable

Iterable are an array or object implementing the Traversable interface.

Array and object are iterable using foreach and can be used with yield form iside a generator.

 

<?php

function gen(): iterable {
yield 1;
yield 2;
yield 45;
}
 
$generator = call_user_func('gen');

foreach($generator as $value){
echo $value;
}

?>

9.  NULL

Null it is a special value which allows a variable to not have a value.

In order for this to happen you can assign to a variable the constant NULL , not setting any value to a variable or use unset() callable.

10.  Resources

Resource it is a special variable which can open files, database connections, image canvas areas and more .

Pseudo Types

void

void as a return type means that the return value is useless. void in a parameter list means that the function doesn't accept any parameters. 

 

mixed

mixed indicates that a parameter may accept multiple (but not necessarily all). types.gettype() for example will accept all PHP types, while str_replace() will accept strings and arrays

 

 

JavaScript Data Types 

 
//JavaScript has 7 data types
// 6 primitive types
Boolean
String
Symbol
Null
Undefined (aka missing)
Number
//and
and Object
 

 

 For full reference of JavaScript data types visit this Data Structures link .

 OR  for a full leson go to You Don't Know JavaScript book series here