Question about variables.

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
NirelG
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Apr 01, 2017 7:14 am

Sat Apr 01, 2017 8:22 am

Hi all,

I am quite new in php and I have a question regarding primitive variables in php.

I looked into the php documentation and found the following:

" PHP supports nine primitive types.

Four scalar types:

boolean
integer
float (floating-point number, aka double)
string

Three compound types:

array
object
callable

And finally two special types:

resource
NULL
"

my question is as follows:
Does this mean that all the variables in php are considered primitives? if so why?
I know that on most programming languages, you have both primitive variables and reference variables like, objects and arrays, yet in php it seem like even those are primitives.

Thanks in advance,
Nirel.

EDIT:
I might have not understood correctly what php primitive variables are, if I am mistaken, any help and/or explanation will be highly appreciated :)
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 998
Joined: Mon Feb 22, 2016 5:52 pm

Sat Apr 01, 2017 12:45 pm

A variable is stored somewhere in memory. When a CPU only needs to use one instruction to manipulate the variable, it is considered primitive.

As far as php is concerned, it makes very little difference as it has developed so that it does not matter.

That probably doesn't help you understand :? It really would only make a difference if you wanted to significantly increase the speed of processing when computing a very large number of numbers when using old and slow CPUs with older software with very small memories, modern computers and high level languages can deal with just about anything you'll need to do (as that is what they are designed to do).

I wouldn't get hung up on primitive variables.
NigelRen
php-forum GURU
php-forum GURU
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Sat Apr 01, 2017 10:44 pm

As the last bit of hyper's answer says - don't worry about it. There is sometimes confusion caused by the way that different versions of PHP have changed the way that variables can be dealt with. Type hinting in function declarations has been slowly getting better (IMHO) to the ability to define what your passing in and returning :)

But I would be interested in why you think it's so important - makes me wonder at times if I'm missing something important :?
NirelG
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Apr 01, 2017 7:14 am

Sun Apr 02, 2017 2:03 am

NigelRen wrote:
Sat Apr 01, 2017 10:44 pm
As the last bit of hyper's answer says - don't worry about it. There is sometimes confusion caused by the way that different versions of PHP have changed the way that variables can be dealt with. Type hinting in function declarations has been slowly getting better (IMHO) to the ability to define what your passing in and returning :)

But I would be interested in why you think it's so important - makes me wonder at times if I'm missing something important :?
Hi,
well, I am trying to have more information about the language itself and on how it works.
this is why this interests me.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 998
Joined: Mon Feb 22, 2016 5:52 pm

Sun Apr 02, 2017 4:27 am

I've been trying to find a way to explain it, it's not easy to explain since you need to know quite a lot to understand the difference (and if you knew it already you would already know why), primitive data types are more significant to lower level languages (which are compiled) for speed and size optimization on much older equipment, it's far more applicable to what hardware you have than the software.

Whilst I applaud your inquisitiveness and only wish to encourage it, some things take time to sink in and some things you'll never need to use or know about. If you do want to know more about primitive types, then, realistically you will need to look at assembly language and CPU architecture to appreciate the relevance since that is where it applies.

Sorry that I can't give you a simple explanation, knowing about it is technical (whilst at the same time being annoyingly simple :? )and has a fairly limited use; at the end of the day, if you need to be looking at this from a programming point of view, then PHP is probably the wrong language.
Post Reply