4 | PHP Variable and Data Type Tutorial | 2023 | Learn PHP Full Course for Beginners

Dani Krossing
12 Mar 202317:29

Summary

TLDRThis video tutorial delves into PHP basics, focusing on syntax and variable creation. The instructor guides viewers through setting up a local server with XAMPP, navigating to the 'Local Host' to view websites, and embedding PHP within HTML. The concept of variables is introduced as memory locations for data storage, with examples of scalar data types like strings, integers, floats, and Booleans. The video also touches on arrays and object types, emphasizing best practices for variable naming and initialization. Practical examples illustrate how variables can be used and assigned within PHP code, offeringεˆε­¦θ€… a foundational understanding of PHP programming.

Takeaways

  • πŸ˜€ The video discusses PHP syntax and how to create variables and understand data types.
  • πŸ’» To view a website in a browser, ensure the editor is open, XAMPP is running with Apache and MySQL servers started, and then navigate to 'Local Host' in the browser.
  • πŸ“ Embedding PHP within HTML is demonstrated, highlighting the use of PHP tags and the `echo` statement to output data.
  • πŸ”‘ Variables in PHP are introduced as memory locations that store data, likened to labeled boxes containing items.
  • πŸ› οΈ Variables are declared with a dollar sign (`$`) and are named to reflect the data they hold, adhering to naming conventions for readability.
  • πŸ”‘ Data types covered include scalar types (string, integer, float, Boolean), which hold single values, and the concept of default values for each type when not initialized.
  • πŸ“Š Arrays are mentioned as a data type that can hold multiple values within a single variable, with examples of declaring and initializing arrays.
  • 🚫 The video cautions against declaring variables without initialization to avoid potential errors and suggests assigning default values instead.
  • πŸ”‘ The video briefly introduces object types, relating them to classes and instantiation, though noting they are not the focus for beginners.
  • πŸ“š It reassures viewers that it's normal to feel overwhelmed with new information and emphasizes the importance of practical experience for learning and retention.

Q & A

  • What is the first step to view a website in a browser when using an AMP stack like XAMPP?

    -The first step is to have your editor open, which is essential for viewing and editing your website's code.

  • Which two servers need to be started in XAMPP to run a PHP website locally?

    -You need to start the Apache and MySQL servers in XAMPP to run a PHP website locally.

  • How do you access your local PHP website in a browser?

    -You access your local PHP website by typing 'Local Host' in the browser's URL bar.

  • What is the significance of the dollar sign ($) in PHP when creating variables?

    -The dollar sign ($) is used to declare a variable in PHP, indicating the start of a variable name.

  • What is the purpose of the 'echo' statement in PHP?

    -The 'echo' statement in PHP is used to output data to the browser, allowing you to display content on the webpage.

  • Why is it a good practice to end PHP statements with a semicolon even if it's the last statement?

    -Ending PHP statements with a semicolon is a good practice because it ensures consistency in your code, and it doesn't hurt to do so even if it's the last statement.

  • What is a variable in the context of PHP programming and how is it represented?

    -A variable in PHP is a memory location that stores data, represented as a 'box' with a label to easily reference and manipulate the data it contains.

  • What are scalar data types in PHP and give an example of each?

    -Scalar data types in PHP are data types that contain a single value. Examples include strings (text), integers (whole numbers), floats (numbers with decimal points), and Booleans (true or false).

  • How do you declare a variable in PHP without assigning a value to it?

    -You can declare a variable in PHP without assigning a value to it by using the dollar sign followed by the variable name, but it's recommended to always initialize variables to avoid potential errors.

  • What are the default values for different data types in PHP when a variable is declared without an initial value?

    -The default values in PHP for different data types when a variable is declared without an initial value are: strings default to an empty string, integers and floats default to zero, Booleans default to false, arrays default to an empty array, and objects default to null.

  • How can you use a variable to output data within an HTML element using PHP?

    -You can use a variable to output data within an HTML element by embedding PHP tags within the HTML and using the 'echo' statement to output the variable's value.

Outlines

00:00

πŸ’» PHP Syntax and Variables Introduction

The paragraph begins with a recap of the previous video's discussion on PHP syntax and transitions into an exploration of variable creation and data types. The speaker suggests using an editor to write PHP code and emphasizes the practical experience of coding. They guide viewers on how to start a website using XAMPP, detailing the steps to launch Apache and MySQL servers and access the website through a browser. The explanation continues with embedding PHP within HTML, illustrating the use of PHP tags and the 'echo' statement to output data. The speaker also touches on the importance of semicolons in PHP, even when they seem optional. The paragraph concludes with a practical demonstration of mixing PHP and HTML to dynamically generate content on a webpage.

05:02

πŸ“ Understanding Variables and Naming Conventions

This section delves into the concept of variables in PHP, likening them to labeled boxes that store data. The speaker simplifies the idea by suggesting that variables are used to label and access data easily within code. They explain how to declare and initialize variables using the dollar sign ($) and provide an example of creating a string variable. The paragraph also covers variable naming conventions, advocating for a lowercase first letter followed by capitalized letters for subsequent words (camelCase). The speaker advises against starting variable names with numbers or special characters, except for underscores. The discussion serves as a foundation for understanding how variables are used in PHP to handle and reference data within applications.

10:03

πŸ”’ Exploring Data Types in PHP

The speaker introduces the concept of data types in PHP, focusing on scalar types that include strings, integers, floats, and Booleans. They explain that scalar types hold single values and provide examples for each type, including how to declare and initialize them. The paragraph distinguishes between data types by showing the difference between a string with quotes and a number without quotes, emphasizing the importance of type distinction in PHP. Boolean values are also introduced with the values 'true' and 'false'. Additionally, the speaker briefly mentions arrays and objects, describing arrays as variables that can hold multiple values and objects as instances of classes, although the latter is noted as an advanced topic for future discussion. The paragraph aims to familiarize viewers with the basic data structures in PHP.

15:05

🧩 Practical Usage of Variables in PHP

In this part, the speaker addresses potential confusion about arrays and objects, reassuring learners that detailed explanations will come later in the course. They introduce the concept of variable instantiation, explaining how variables can be assigned default values based on their context if not explicitly initialized. The speaker outlines the default values for different data types: empty strings for strings, zero for numbers, 'false' for Booleans, empty arrays, and 'null' for objects. The paragraph concludes with a practical example demonstrating how to use variables within HTML and PHP to display dynamic content on a webpage. The speaker also shows how to assign one variable to another, creating a copy of the data. The paragraph ends with encouragement for learners to practice PHP and to not be overwhelmed by the amount of new information, promising more practical examples in future lessons.

Mindmap

Keywords

πŸ’‘PHP

PHP is a server-side scripting language designed for web development. It is used to create dynamic web pages and manage server-side logic. In the video, PHP is the central theme as the instructor discusses its syntax, variables, and data types, which are fundamental to understanding how to write PHP code. The script mentions embedding PHP within HTML to create dynamic content on a website.

πŸ’‘XAMPP

XAMPP is a free and open-source cross-platform web server solution stack package. It integrates Apache, MySQL, PHP, and Perl, making it a popular choice for local web development. The script describes starting XAMPP to run a local server, which is necessary to view PHP websites in a browser during development.

πŸ’‘Variables

In programming, a variable is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. In the video, variables are introduced as a way to store and manage data within PHP. The instructor uses the analogy of a box with a label to explain how variables hold and reference data.

πŸ’‘Data Types

Data types in programming define the classification or category of data, determining what kind of data can be stored in a variable. The video script explains several data types in PHP, including scalar types like strings, integers, floats, and Booleans. Understanding data types is crucial for managing different kinds of data within PHP scripts.

πŸ’‘Echo

The 'echo' statement in PHP is used to output one or more strings. It is a fundamental concept in PHP for displaying content in a web browser. The script uses 'echo' to demonstrate how to display the content of variables within HTML elements on a webpage.

πŸ’‘HTML

HTML (Hypertext Markup Language) is the standard markup language for creating web pages and web applications. The video script discusses embedding PHP within HTML to create dynamic web content. HTML provides the structure, while PHP adds functionality and interactivity.

πŸ’‘Scalar Types

Scalar types in PHP refer to data types that store a single value. The script mentions string, integer, float, and Boolean as examples of scalar types. These are the basic building blocks for handling simple data in PHP, and the video provides examples of how to declare and use them.

πŸ’‘Arrays

An array in PHP is a data structure that contains multiple values in a single variable. The script briefly introduces arrays, explaining that they can hold multiple pieces of data, unlike scalar types which hold only one value. Arrays are useful for managing lists or collections of data.

πŸ’‘Boolean

A Boolean data type in PHP has only two possible values: true and false. It is used for logical operations and conditions. The video script explains that Booleans are used to represent truth values and are essential for control flow in programming, such as in conditional statements and loops.

πŸ’‘Naming Conventions

Naming conventions are a set of rules for choosing the character sequence used in names of identifiers. In the video, the instructor discusses the customary naming conventions for variables in PHP, such as starting with a lowercase letter and using uppercase letters for the first letter of subsequent words (camelCase). Following naming conventions helps in writing readable and maintainable code.

πŸ’‘Default Values

Default values in PHP refer to the initial values that variables take on when they are declared without an explicit value. The script explains that different data types have different default values, such as an empty string for strings, zero for numbers, and false for Booleans. Understanding default values is important for avoiding errors and unexpected behavior in PHP code.

Highlights

Introduction to creating variables and data types in PHP.

Practical experience with PHP code by starting a website.

Steps to view a website in a browser using XAMPP.

How to embed PHP within HTML to output content.

The importance of semicolons in PHP statements.

Demonstration of mixing PHP and HTML to create dynamic content.

Explanation of what a variable is in PHP.

Analogy of variables as boxes with labels for data storage.

How to declare and initialize a variable in PHP.

Naming conventions for variables in PHP.

Introduction to scalar data types in PHP.

Understanding strings, integers, floats, and Booleans as scalar types.

The concept of arrays as a data type to store multiple values.

Different ways to create arrays in PHP.

Mention of object-oriented programming with classes and objects.

Default values for different data types when variables are declared.

Advice on always initializing variables to avoid errors.

Encouragement for beginners to not be overwhelmed by the information.

Practical example of using variables in PHP code.

How to assign one variable to another in PHP.

Closing thoughts and anticipation for the next video.

Transcripts

play00:00

so in the last video we talked a bit

play00:01

about PHP syntax and today we're going

play00:03

to talk a bit about how to create

play00:04

variables and data types now I do think

play00:07

it's a really good idea that we go

play00:08

inside our editor in this video and just

play00:10

talk a bit about this so we have some

play00:11

practical experience writing vhp code

play00:13

and with that we do need to actually

play00:16

start up our website and I thought why

play00:18

not do that together since that is

play00:19

something we haven't done yet so what

play00:21

you need to make sure you do every

play00:22

single time you want to see your website

play00:24

inside the browser is first of all you

play00:26

need to have your editor open which is

play00:27

step one then you need to make sure

play00:29

you're opening up xampp so you want to

play00:31

open up the software make sure you

play00:33

actually start the Apache and MySQL

play00:35

server then you want to go inside your

play00:37

browser and then you want to go into the

play00:38

URL and type Local Host and when you do

play00:42

that you can see all the websites that

play00:43

you have inside your htdocs folder in my

play00:45

case here I do have two websites you

play00:47

should not have two you should only have

play00:49

one so go ahead and pick the one that we

play00:50

created together in the first episode

play00:52

and then you can see we have our website

play00:54

in front of us here and that's the basic

play00:56

step in order to open up your website

play00:58

when you're running it inside A7 server

play01:00

like xampp so now we can go back inside

play01:02

our index.php and just to kind of talk

play01:05

briefly about the last episode because I

play01:07

know some people are a little bit

play01:08

confused when it came to embedding HTML

play01:10

and PHP together so if I were to go

play01:12

inside I can create a regular HTML

play01:14

paragraph and I can go and create this

play01:17

is a paragraph and just write some text

play01:20

inside this paragraph here now what you

play01:22

can do is you can create the PHP tags so

play01:24

the opening and closing tags so angle

play01:26

bracket question mark PHP then you can

play01:29

say question mark angle bracket so what

play01:31

I could do is echo which we talked about

play01:34

is how to Output things inside the

play01:36

browser I can say this is also a

play01:40

paragraph and we do need to make sure

play01:42

remember that semicolon even though

play01:44

because this is the last statement

play01:45

inside this particular PHP tag here we

play01:48

don't technically need to have this last

play01:50

semicolon but like I said it is best

play01:52

practice to remember to do it every

play01:53

single time because it doesn't hurt

play01:55

anything to do it so let's just go and

play01:56

do it every single time and when you do

play01:58

this you can go back inside your website

play02:00

refresh it and then we can see we have

play02:02

two pieces of text we have this is a

play02:04

paragraph and this is also a paragraph

play02:06

So we have two paragraphs inside our

play02:08

website and just to show something a

play02:10

little bit more complicated you can also

play02:12

go inside existing HTML elements and

play02:14

create PHP so I can go in between here

play02:17

and I can open up my PHP tags and then I

play02:20

can write something additionally in here

play02:22

so it would have to Echo again because

play02:23

we need to make sure to Output whatever

play02:25

we're doing using PHP and I can go ahead

play02:28

and Echo awesome paragraph

play02:31

semicolon save this one go back inside

play02:34

my website and then you can see we've

play02:35

included this awesome paragraph in here

play02:38

and this is just to kind of show that we

play02:39

can mix and match PHP and HTML together

play02:41

in any sort of way that we want by

play02:43

embedding it directly inside the HTML

play02:45

but now this is not what I wanted to

play02:46

talk about in this video here I do

play02:48

actually want to talk about variables

play02:50

and data types because we do have

play02:52

variables and data types which we use

play02:53

constantly when it comes to writing PHP

play02:55

code so this is something we have to

play02:57

memorize so let's go and talk a bit

play02:59

about what exactly a variable is now a

play03:01

variable is when we have a memory

play03:04

location inside our application that

play03:06

stores some sort of data which is a very

play03:09

confusing way for beginners to

play03:10

understand what exactly a variable is

play03:12

because a lot of people don't understand

play03:13

what is memory location and that kind of

play03:16

thing so instead just for practice here

play03:18

let's pretend that a variable is a box

play03:22

and that box has a label so you know

play03:24

when you're moving into a new house you

play03:25

put you know some writing on the box

play03:27

whether it's kitchen utensils or for the

play03:30

the kids room something so you know

play03:32

what's inside the box but then you do

play03:34

also have something inside the Box some

play03:36

actual data so whenever you grab this

play03:38

box by referring to the name of the box

play03:40

then you grab whatever is inside the box

play03:42

as well and that is technically what a

play03:44

variable does so when you reference to a

play03:47

variable you grab the data and it's just

play03:48

a very easy way for us to refer to data

play03:50

and label it so we know exactly what the

play03:53

data is now when it comes to PHP the way

play03:56

you refer to a variable or the way you

play03:58

declare a variable is by creating a

play04:01

dollar sign so if I were to go inside my

play04:03

PHP tags here I can create a dollar sign

play04:05

which means that now we are declaring a

play04:07

variable and then we can call it

play04:09

something so we can come up with a name

play04:11

for this variable that we think makes

play04:13

sense to the data that is inside the

play04:15

variable so in this case here I could

play04:17

say that this is a name so I could say

play04:19

that this is a name and it's going to be

play04:21

equal to some kind of data so now we're

play04:23

initializing this variable by assigning

play04:25

a piece of data to it so in this case

play04:27

here I could say that it is a string

play04:29

which we talked about before called

play04:31

Danny Crossing so now every time I refer

play04:34

to this variable called name I'm going

play04:36

to get a value called Danny Crossing so

play04:39

if I would go below here and just echo

play04:41

which means that we're outputting

play04:42

something inside the browser and I can

play04:44

go ahead and Echo out name or the

play04:47

variable called name so we need to

play04:49

remember the dollar sign here if it were

play04:51

to do this go back inside my website you

play04:53

can see that we're echoing out Danny

play04:55

Crossing so in this sort of way we can

play04:57

create boxes or labels four pieces of

play04:59

data that we can refer to in order to

play05:01

better handle data inside our code so in

play05:03

this case if we create a variable called

play05:05

name and you can call whatever you want

play05:07

but now we do also have naming

play05:08

conventions when it comes to naming

play05:10

these variables here so in this case you

play05:11

can see we call this one variable name

play05:13

but we can also go in and either start

play05:15

it with a letter or a underscore in

play05:18

order to name this variable now the

play05:20

customary thing is to start with a

play05:22

non-capitalized letter and then make

play05:24

sure that any other new words inside

play05:26

this variable name starts with a

play05:27

capitalized letter so if I were to write

play05:30

full name instead then we start out with

play05:31

a non-capitalized F and then a

play05:34

capitalized n for name so whenever you

play05:36

have multiple words inside the variable

play05:38

name then you just make sure the first

play05:39

letter of that second word or third word

play05:42

or fourth word is going to be

play05:43

capitalized and again it's not really a

play05:45

customary thing you have to follow but

play05:47

it's it's the way that people do it so I

play05:49

would recommend doing it the same way so

play05:51

people don't misunderstand what your

play05:53

code does or why you named your

play05:55

variables in this way that other people

play05:57

don't usually do it and just to mention

play05:59

it when it comes to any other letters

play06:00

that isn't the first letter inside the

play06:02

variable name then you can also go ahead

play06:04

and create either underscores or you can

play06:06

create numbers so we can say one but

play06:09

make sure you stick within letters

play06:10

underscores or numbers when it comes to

play06:13

the variable name after the first letter

play06:15

inside the variable name so now let's go

play06:17

ahead and delete what we have here and

play06:18

talk a bit about data types because when

play06:20

it comes to data types inside PHP we

play06:22

have many different data types we're not

play06:24

going to talk about all of them in this

play06:25

video here since some of them are a

play06:27

little bit more complex than others but

play06:29

we will talk about some of the base

play06:30

types that you have inside PSP the first

play06:33

one is going to be what is called scalar

play06:35

types and scalar basically just means

play06:37

that the variable contains one value to

play06:39

it and the first example of that would

play06:41

be we could create a variable that is

play06:43

called something like string

play06:46

and a string we did already talk about

play06:48

what kind of data that is so in this

play06:49

case here we could say uh Daniel so as

play06:52

we know already a string is a piece of

play06:54

text now we do also have numbers so I

play06:56

can go below here and I can create a

play06:58

integer or a int and I'm going to go

play07:00

ahead and name this one a number so we

play07:02

can just write some sort of random

play07:04

number here and this is going to be a

play07:05

value for a integer notice that we're

play07:07

not using double quotes around the

play07:09

number because if I were to do that then

play07:10

all of a sudden this is going to be a

play07:12

string so it's not considered a number

play07:14

inside the PHP language anymore it is

play07:16

actually considered to be a piece of

play07:18

text just like a string up here and you

play07:20

can actually tell by the color that it

play07:21

actually changed it to text now we do

play07:24

also have something called a float which

play07:26

is something that we see happen a lot in

play07:27

many other programming languages which

play07:29

is essentially when you have a number

play07:30

that has decimal points so if you were

play07:33

to write something like

play07:36

2.5678 then this is going to be

play07:38

considered a float because it has

play07:40

decimal points and then we do also have

play07:41

one more scalar type which is called a

play07:43

Boolean now I'm just going to write bull

play07:46

even though it is spelled Boolean we're

play07:48

just gonna write bull for short and this

play07:51

one is going to be a true or false

play07:52

statement so essentially is something

play07:55

true or is it false and the values for

play07:57

that is going to be false or it is going

play08:00

to be true now it is also important to

play08:03

mention here that if you were to have

play08:04

numbers in here and you were to run this

play08:06

as a Boolean then one is also going to

play08:09

return as true and 0 is going to return

play08:11

as false but in most cases we do just

play08:14

use true or false when it comes to this

play08:16

type of data here and just because I

play08:18

talked about it in the last video let's

play08:19

go and create a comment for this section

play08:21

here so know exactly what this is so

play08:23

we're going to create a one line comment

play08:24

and I'm going to call this one scalar

play08:26

types now the next one we have is

play08:29

actually one that we're not going to do

play08:30

too much with here in the beginning but

play08:32

we do also have a array type so we can

play08:35

actually go and create a comment and

play08:36

call this one array type and essentially

play08:38

a array is when we have a variable that

play08:40

has multiple pieces of data inside of it

play08:42

so just like scalar types which means

play08:45

contains one value in the case of an

play08:49

array we have multiple values inside one

play08:51

variable so could for example create a

play08:54

variable called array and I can name

play08:56

this one equal to multiple pieces of

play08:58

data now we do have two different ways

play09:00

you can create an array one is by

play09:01

writing array parentheses semicolon and

play09:05

then you can add multiple pieces of data

play09:06

in here which could for example be a

play09:08

bunch of strings so I can go in here and

play09:10

say we have one string which is called

play09:11

Daniel and then I can create a comma and

play09:15

then add a second string which could be

play09:17

Bella

play09:19

and then I can add a third piece of data

play09:21

and I can just keep piling data on

play09:22

inside this array here so we can say

play09:24

Theta

play09:26

which is also a string and then we could

play09:28

of course rename the array to something

play09:29

like names if that makes a little bit

play09:31

more sense to what exactly this piece of

play09:33

data has inside of it but essentially

play09:35

this is going to be a bunch of data

play09:37

inside one variable and just to mention

play09:40

it since I did mention there was another

play09:41

way to do this instead of creating array

play09:43

parentheses you can also do a square

play09:45

bracket and then close it off using a

play09:48

square bracket as well just like this

play09:50

now if you just started following this

play09:52

course here you should have a newer

play09:53

version of PHP but if you do run PHP 5.4

play09:56

or lower then these square brackets are

play09:59

not going to work when it comes to

play10:00

writing a PHP code so if you run a older

play10:03

version of PHP then you do have to do it

play10:05

the way by creating this array around it

play10:08

but just like I said if you're a little

play10:09

bit confused about why we use arrays

play10:11

inside our code you may have questions

play10:13

about it don't worry too much about it

play10:14

because you will get to talk more about

play10:16

arrays in the future for now like I said

play10:18

you're only going to have to worry about

play10:19

these different scalar types here I do

play10:22

want to mention one more data type

play10:23

though even though it's not one you

play10:25

should concern yourself with here at the

play10:27

beginning since this is something that's

play10:28

a little bit further ahead in this

play10:29

course here but we do also have

play10:30

something called an object type so we

play10:33

can say object type and an object type

play10:36

is essentially just when we have a

play10:37

object that is equal to a variable

play10:40

objects is something we create based on

play10:42

classes which is something we again are

play10:44

not talking about right now but when we

play10:46

do instantiate a class we do actually

play10:48

create a variable that is equal to an

play10:50

object based off of that class so I

play10:52

could for example say that we have a

play10:54

variable called object and then we set

play10:56

it equal to a new object which is going

play11:00

to be the name of the new object so we

play11:01

could for example say car parentheses

play11:04

and semicolon and this would instantiate

play11:06

a new car object which of course right

play11:08

now can't find because we don't have it

play11:10

but once we do have a class called card

play11:12

that we can instantiate then this is not

play11:14

going to throw an error message but like

play11:15

I said we're not really going to worry

play11:17

too much about arrays and Optics right

play11:18

now this early on in this course here so

play11:21

don't worry if you get confused about

play11:22

them because that is perfectly normal

play11:24

even though we're not really supposed to

play11:25

talk about raising Optics yet I still

play11:27

thought it was important to just kind of

play11:28

like mention them because it is

play11:30

something that is very used inside PHP

play11:32

so knowing about them so you have kind

play11:34

of like a a little bell inside your head

play11:36

when we talk about objects in a future

play11:38

episode and you think to yourself oh

play11:40

wait I heard about that at some point

play11:42

inside this course here and then you

play11:44

might go back to this lesson here and

play11:45

think oh yeah we talked about objects in

play11:47

that early lesson oh now we get to talk

play11:49

about what exactly it is so don't worry

play11:51

too much about it right now it is

play11:53

something that you just need to have

play11:54

kind of like in the back of your head

play11:55

it's not important right now the only

play11:58

thing you need to worry about right now

play11:59

is that we have these scalar types and

play12:01

when we actually create the variable we

play12:03

declare the variable and then we

play12:05

initialize it by assigning a value to it

play12:08

the reason I mention that is because it

play12:10

is possible to go down and create a

play12:13

variable and let's just go and call it

play12:15

names and not assign anything to it and

play12:19

when you do this depending on the

play12:20

context of when you use this variable it

play12:23

is going to default to a certain value

play12:25

type so if you were to use this one in a

play12:27

string context then it will just go and

play12:29

say oh okay so this is supposed to be a

play12:31

string automatically and then it's just

play12:33

going to assign a empty string to it and

play12:35

it will actually do something that looks

play12:37

like this so we just have an empty

play12:39

string that doesn't have any sort of

play12:40

value inside of it and this is what it's

play12:42

going to default to and the same thing

play12:43

goes for integers floats and booleans

play12:46

and arrays and objects they all default

play12:48

to something so in this case if I were

play12:51

to go ahead let's just go and use these

play12:52

up here so as a default a string

play12:54

defaults to nothing so just an empty

play12:56

string with double quote by the way an

play12:59

integer is going to default to zero and

play13:01

the same thing goes for floats they also

play13:03

default to zero and when it comes to

play13:05

booleans they default to false and just

play13:08

to mention it here when it comes to an

play13:10

array so if we were to create a array

play13:12

because we did talk about them in this

play13:13

video so we do also need to just kind of

play13:15

mention this it is going to default to a

play13:17

empty pair of square brackets and when

play13:20

it comes to an object

play13:22

they default to null which means nothing

play13:26

null is not a concept that we're going

play13:28

to talk about right now but it just

play13:29

basically means nothing however when it

play13:32

comes to declaring a variable you should

play13:34

always initialize it the reason I say

play13:36

this is because sometimes when we do

play13:37

create variables we don't know what

play13:39

should be inside the variable just quite

play13:41

yet and in those cases we just declare a

play13:43

variable but we wait with assigning any

play13:46

sort of value to it and when those

play13:48

moments happen you should not do it this

play13:50

way because you do risk getting error

play13:52

messages inside your code so make sure

play13:54

that you always assign something to it

play13:56

by initializing the variable so with

play13:58

strings you put empty double quotes with

play14:00

a integer you put a zero the same thing

play14:02

with floats if you put a Boolean always

play14:04

set it to false arrays are just going to

play14:06

have these square brackets here and

play14:07

objects are just going to be null so

play14:08

these are going to be the default values

play14:10

you should always put inside a variable

play14:12

if you don't know what kind of data you

play14:14

should put inside of it just quite yet

play14:16

otherwise your interpreter is just going

play14:18

to throw you a warning in a lot of cases

play14:20

so so just go ahead and make that into a

play14:22

habit and here at the end before we end

play14:24

up the episode I just want to say that

play14:25

it is perfectly normal to be completely

play14:27

overwhelmed with all this information

play14:28

here because this is a lot of

play14:30

information I'm dumping on people

play14:31

especially if this is your first

play14:33

programmer language this is going to be

play14:34

completely new and this is going to be a

play14:36

lot of information I just want to say

play14:38

that it's perfectly normal to be

play14:40

overwhelmed and in the future we will

play14:42

get to do many more practical examples

play14:44

so we do actually use variables for

play14:46

something and we use many different data

play14:48

types and it is something that is just

play14:50

going to be a bit more natural when you

play14:52

actually start seeing how these are used

play14:53

in Practical examples and when you get

play14:55

that little oh okay Epiphany moment

play14:58

where okay so this is how we use these

play15:00

different things we've learned up until

play15:01

now then at that point you will remember

play15:03

things a lot easier okay so don't be

play15:05

worried about if you can't memorize all

play15:07

these things because no one expects you

play15:08

to memorize all these things in one

play15:10

sitting it is something that sticks with

play15:12

you as you start practicing PHP along

play15:15

the way just to give a short example

play15:17

here at the end just so people know

play15:18

exactly how we can use variables inside

play15:20

our code if I were to go back inside my

play15:22

body sex here and at the very top I'm

play15:24

going to go ahead and declare a variable

play15:25

called name I'm going to set this one

play15:27

equal to a string called Danny Crossing

play15:30

and what we can do here is we can go

play15:32

below and create a paragraph inside HTML

play15:35

then I can just go and say hi

play15:38

my name is and then we're going to open

play15:41

up PHP tags close it off again comma and

play15:45

I'm learning

play15:49

PHP then what you're going to do is

play15:50

you're going to go up and grab your

play15:51

variable name and you're going to go

play15:53

inside your PHP tags and you're going to

play15:54

Echo out your variable name semicolon

play15:58

and when you do this and go inside the

play16:00

browser and refresh it you can see that

play16:02

now it says hi my name is Danny Crossing

play16:04

and I'm learning PHP so in this sort of

play16:06

way we can take data or variables and we

play16:09

can use them inside our code or inside

play16:11

our HTML if you want to do that to

play16:13

Output data in this sort of sense like

play16:14

this is a very basic example but just to

play16:16

kind of show that we can use variables

play16:18

to reference to data that we assign to

play16:21

variables and just to mention one more

play16:23

thing because something we can also do

play16:24

is we can go down and say I want to

play16:26

create a new variable and I just go

play16:28

ahead and call this one something like

play16:29

test just to give it some kind of name I

play16:32

can assign equal to name so in this case

play16:35

here we have a variable that is assigned

play16:37

equal to a variable and it would take

play16:40

this name and copy it down instead of

play16:41

the echo you'll now notice that we still

play16:43

do get hi my name is Danny Crossing and

play16:46

I'm learning PHP and that is because

play16:48

this this first variable here has a

play16:50

piece of data assigned to it and then

play16:52

the second variable down here has that

play16:54

same variable which has data inside of

play16:57

it assigned to itself so we can also

play16:59

assign variables equal to variables in

play17:01

this sort of way here and with that we

play17:03

now know a lot about variables so I hope

play17:05

you enjoyed this episode here and I'll

play17:07

see you in the next video

play17:14

[Music]

play17:20

[Music]

play17:27

thank you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
PHP BasicsVariablesData TypesSyntaxWeb DevelopmentCoding TutorialXAMPPHTMLPHP TagsProgramming