What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson
Courses
/
PHP Fundamentals
/
Variables & Types

Next video in… 10

Arrays

Variables & Types

PHP Fundamentals

Variables & Types
Learn about PHP's basic types, variable declarations, and type handling. See how PHP manages strings, numbers, and other data types.


PHP variables always start with a $ symbol. This makes them easy to spot in your code. PHP variables can hold any type of data, and you don't need to declare types upfront - you can change a variable's type simply by assigning a different kind of value to it.

1$status = 404; // integer
2 
3$status = "active"; // sting
4 
5$status = true; // boolean

Strings

Strings in PHP are similar to what you might know from other languages. You can create them with single or double quotes. The main difference is that double quotes allow you to embed variables directly in your string, while single quotes treat everything as literal text. Strings can be combined using the dot (.) operator:

1$name = 'John'; // John
2$greeting = "Hello $name"; // Hello John
3$greeting = 'Hello $name'; // Hello $name
4 
5// You can chain multiple strings
6$fullName = 'John' . ' ' . 'Doe'; // Results in: John Doe

Numbers

PHP has two numeric types you'll commonly use:

1$age = 25; // Integer
2$price = 19.99; // Float (also called double)

Booleans

In PHP, like in many other programming languages, a boolean represents two possible values: true or false.

1$isActive = true;
2$isDeleted = false;

Type Casting

PHP allows you to explicitly convert (cast) variables from one type to another. This is similar to JavaScript's parseInt or toString, but PHP uses a simpler syntax:

1$price = "19.99"; // string
2$age = (int) $price; // converts to 19
3$isActive = (bool) 1; // converts to true
4$text = (string) 42; // converts to "42"

PHP also performs automatic type coercion, similar to JavaScript. This means PHP will automatically convert types when needed:

1$result = "123" + 456; // "123" becomes integer, result is 579
2$sum = 42 . " items"; // 42 becomes string "42 items"
3if ("hello") { // string is coerced to true
4 // this runs
5}
00:00
It's time to finally write some PHP and we're going to start with variables.
00:07
So variables like in any other coding language hold data that you can reuse during run time
00:12
during your program or you can change it.
00:15
In PHP all variables start with a dollar sign which makes them quite easy to spot in your
00:20
code and then you just define what should go into this variable.
00:24
So this is a number here and you can see I don't have to define that this should be a
00:27
number I can just put in this variable what I want I could also put in a text here or
00:33
maybe a boolean which we'll also talk about later a bit more like true but yeah let's
00:38
go back to the number here.
00:41
And we're going to use a function in PHP which is called var dump which dumps out our variable
00:47
and now this tells us what is inside this variable and we can see we get the content
00:51
of the variable which is 404 but we also can see that this is of the type integer.
00:58
So in PHP we have two types of numbers we have integers and when we use a decimal here
01:03
you can see this is now a float or you can also call it double and if we change this
01:09
back to a text by using quotes here you can see that this is our text and this is now
01:16
a string.
01:18
And of course like before if we take a look at this boolean value you can also see that
01:23
this is a boolean value which is true in this case.
01:27
All right let's talk a little bit more about strings so I'm using double quotes here but
01:32
I can also use single quotes like this and you will see this works as well.
01:39
So the only difference here is let's write another variable and let's call this text
01:45
and let's say this is just a string.
01:48
Yeah and if I now try to reuse this text variable here inside the status when we use single
01:54
quotes you can see it's not working mainly because I forgot the semicolon here but it's
01:59
also not working because we get just a variable named $text in here.
02:04
So the use case of using double quotes is here that we can use other variables inside
02:08
here like this and now you can see we get the text 404 not found in here.
02:14
But besides that we can also combine multiple strings with the dot operator and you will
02:23
see that we now get also a string back 404 not found.
02:26
Yeah so the dollar operator helps us to combine two strings and this can be two variables
02:31
or it can be just a string here and the variable this still works the same.
02:36
Yeah so these are some of the main types in PHP we will also have arrays and objects but
02:40
we're going to talk about those in the next videos.
02:44
Let's go back to this string here.
02:46
So we know this is now of the type of string but we can also tell PHP that this should
02:51
be another type and this is called casting.
02:54
We're now telling here with this casting operator that this string here should be an integer
03:00
and if we run a code you can see we'll get an integer back and this also works the other
03:04
way around.
03:05
So let's say we have an integer and we want this to be a string.
03:09
This now works as well yes and of course that's very important depending on if you're using
03:14
maybe a database and you have some specific columns with specific values and types there
03:20
and then you want to make sure that you store the right values.
03:23
So yeah in general it's a good idea to always make sure that you work with the types that
03:27
you are expecting.
03:29
And to round this up let's also talk here about boolean values.
03:34
So let's see what the string representative of true is which is one but also interesting
03:41
if you use false here you can see false is an empty string and also interesting if we
03:47
try to cast this to a number so false is zero and true is also one here which is yeah it's
03:57
also what you would expect here but also something that's quite unique and special in PHP we
04:02
have automatic type coercion which is just a fancy term for type juggling.
04:07
So let me show what this means.
04:09
So if I have here a string let's say 404 again and I'm going to add for the number an integer.
04:18
So what do you think will happen now.
04:20
So in many programming languages this will fail because you're trying to add an integer
04:25
to a string which is mostly not working but in PHP this is working and we get 408 back.
04:32
So PHP tries to be very smart about this and sees okay we have here a number inside this
04:38
text here and here we have a number so let's try to make an integer out of it which is
04:43
404.
04:44
But of course if we try to add this with the dot operator it will just add it to the string.
04:51
So this is something that you have to be aware of because you might end up with results that
04:55
you are not expecting.
04:57
But yeah that's all about variables for this video we're going to reuse them now in probably
05:01
all of our other videos.
05:03
So yeah this is already a good start so let's move on to the next video.