DEV Community

Mateusz Jasiński
Mateusz Jasiński

Posted on • Edited on

PHP 0 to hero pt.4 - Variables, constants, arrays, superglobals

Welcome back - Today we will be learning about one of the most important concepts of programming - storing data

I won't make this intro too long - this article consists of 4 parts

  1. Variables
  2. Constants
  3. Arrays
  4. Superglobals

No more - let's start learning

Variables

So, what is a variable?

As Oxford dictionary says

a data item that may take on more than one value during the runtime of a program.

So basically a data item, that has assigned value and it can be changed

For example - If we wanted to store how many times, user clicked the button on our website - we would do this in variable, as it changes

In PHP, you define variable we use $ (dollar sign)

$[name] = [assigned value]
Enter fullscreen mode Exit fullscreen mode

Where [name] is variables name and [assigned value] is some value like number or string
In the same way, you change that variable - if we want to print it

echo $variable
Enter fullscreen mode Exit fullscreen mode

And when we are talking about assigning values - let's mention variable types*

In PHP when we assign value to variable, it also gets it's type

If we want to check it - use gettype() function

echo gettype($variable);
Enter fullscreen mode Exit fullscreen mode

As a result we can get

  • "boolean"
  • "integer"
  • "double"
  • "string"
  • "unknown type" [And some more]

PHP is dynamically typed (sometimes you can see word loosely instead of dynamically) language - what does it mean?
It means, that we as programmers don't have to specify what type will variable be, and it can change it's type multiple types during execution (which is both good and bad)

And last thing - naming conventions
We want our code to be readable, don't we? That's why there are naming conditions. For PHP most popular is PHP Standards Recommendations in short PSR

It's commonly agreed that variables should start with small letter

So instead of:

$Variable = 5;
Enter fullscreen mode Exit fullscreen mode

You should write

$variable = 5;
Enter fullscreen mode Exit fullscreen mode

For computer - it's the same. For reader - It makes a huge difference especially as almost every data structure is declarated with $ at the beginning in PHP - without naming conventions reading and understanding code is much harder

Constants

Constants are really similar to variables - but with a catch

You can't overwrite their value while executing code - It's fixed and will be their until finishing execution

As a naming convention - Constants should be always defined with capital letters and words should be separated with underscore

So how do we use it in practice?

There are 2 ways of declaring and accessing a constant

  1. Using const keyword and accessing it by it's name
const CONSTANT = 5;
echo CONSTANT;
Enter fullscreen mode Exit fullscreen mode
  1. Using define() function and constant() function
define("CONSTANT", 5);
constant("CONSTANT");
Enter fullscreen mode Exit fullscreen mode

If there is anywhere a incorrectly named constant (like 2CNST) you can access it only with constant() function, however it's not recommended and we should stick to mentioned earlier naming convention

Arrays

Last data structure I'll talk about are arrays - variables that can hold multiple values at one time

To declare, we can use either square brackets ([])

$array = [1, 2, 1, 4, 5];
Enter fullscreen mode Exit fullscreen mode

Or array() function

$array = array(1, 2, 1, 4, 5);
Enter fullscreen mode Exit fullscreen mode

In both of these examples we assign values - but we don't refer to them per value - as it may change or we can have 2 same values. That's why we use indexes

So, If we want to refer to fourth element in array we will write

$array[3];
// 4
Enter fullscreen mode Exit fullscreen mode

Why 3? Because arrays start their indexing from 0, not 1

So to access first element in array, we refer to a field with index = 0 (index is a number in brackets)

$array[0];
// 1
Enter fullscreen mode Exit fullscreen mode

Of course, we can change the array indexing - but to be honest I rarely used them

$array = array(
         "a",
    6 => "c",
         "d",
);
Enter fullscreen mode Exit fullscreen mode

And, when we dump value with var_dump value we use

var_dump($array)
/* 
array(4) {
  [0]=>
  string(1) "a"
  [6]=>
  string(1) "c"
  [7]=>
  string(1) "d"
}
*/
Enter fullscreen mode Exit fullscreen mode

This syntax

 6 => "c"
Enter fullscreen mode Exit fullscreen mode

Means "Assign key 6 to value "c"" - but we don't have to limit ourselves to only numbers

We can declare them with words too

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

//And we access it

$array['foo'];
Enter fullscreen mode Exit fullscreen mode

These arrays are called associative arrays - the array where keys are strings and not numbers.

But there is one last array type - multi-dimensional array. They are rarely used, yet still you should know them

We can represent this type of array like a table

Array represented as table

To declare it - we use array in an array - Create one, with data from that table, like this

$mDimArr = [
    ["Foo", "Bar", "Foo"],
    ["Fo2", "Bar2", "Foo2"],
    ["Foo3", "Bar3", "Foo4"],
];
Enter fullscreen mode Exit fullscreen mode

And to access it, just use double square brackets. For example

echo $mDimArr[2][0];

// Result: Foo3
Enter fullscreen mode Exit fullscreen mode

Of course, we can make it deeper and nest more tables - but this is just a example

Now, last but very important

Superglobals

Last data structure I'd like to talk about are Superglobals

Do you remember $_GET from last article? That's the perfect example of superglobal - For the definition, docs say that they are:

Built-in variables that are always available in all scopes

Now, another question arises - What is a scope?

To explain it simply - Scope can be defined as place where variable/constant/array etc. can be accessed - What does it mean?

This means ( And I'll also very simplify this so please, don't eat me for it) that there is a special range, where you can access the variable - so for example only in a file or a function and not outside.

Mostly our variables have single scope - differences come when we start to declare functions, what we will do later in this series

And coming back to superglobals - these variables can be used everywhere and you don't need them re-declared in every file

There are several predefined superglobals Like

  • $_GET - for data from GET requests
  • $_POST - for data from POST requests
  • $_COOKIE - for accessing websites cookies
  • $_SERVER - Used to access information about Server

I purposefully don't provide you with any generic example, as you don't really declarate them - they are pre-declarated and you can add data to some of them - but no worries, I'll cover how to do it in other articles

Conclusion

That's it for today - this is kinda long yet I hope useful. To learn more, check out links in the article - they lead to docs and may be worth visiting

But for now, share your feedback in comments below, I appreciate it so much. Check out other parts of this series and wait for the next part - It will appear next Thursday

That's is, see you later

Top comments (2)

Collapse
 
joolsmcfly profile image
Julien Dephix

Hi.

Nice intro!

I'd just point out that by convention constant do not have a leading $.
So instead of

const $CONSTANT = 5;
Enter fullscreen mode Exit fullscreen mode

you should write

const CONSTANT = 5;
Enter fullscreen mode Exit fullscreen mode

I have seen so many people confused with arrays so I think it would help if you showed the PHP code for the array with Foo's and Bar's that's on your image.
And said image should have indices that start at 0 so you could show that
$mDimArr[1][2] points to Bar3.

Collapse
 
wizarddos profile image
Mateusz Jasiński

Hi, thanks for the feedback

I have to admit, that I hadn't really tested the constants, so thanks for pointing it out

Rest if fixed by now, I changed the table and an example code a little bit

I think you have now saved people a bit of time, especially the less experienced once, thank you