Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (5)

1 answer

Last reply by: Kitt Parker
Mon Dec 29, 2014 8:15 PM

Post by Chad Buie on December 25, 2012

I am still wondering why would you need to use this function in a program or script?

Maybe I am ahead here, but I am trying a little of a "Howard Hughes" type..can you give a dynamic example to illustrate when and how to know when to use this debugging function?

0 answers

Post by Vamsy Reddy on April 28, 2012

Hi Matthew Machaj

All these videos are helping me a lot.
Thank You
Your teaching is simply super.

1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 2:23 AM

Post by Jacob/Benja Share on November 27, 2011

For the var_dump could you output multiple variables with one function?

Variables & Numeric Data Types

  • PHP has eight pre-defined data types: four 'scalar' types (integer, float, string, boolean), two 'composite' types (array, object), and two 'special' types (resource, null).
  • An identifier is a name created in PHP code to refer to a particular piece of data.
  • The ‘assignment’ of data to an identifier is the process by which the reference, or association, between an identifier and its data value is made.
  • A variable is an identifier for which the data value assigned to it can change, or ‘vary’.
  • Variable identifiers begin with a $ followed by the variable’s name.
  • Variable names are case-sensitive and must start with a letter or underscore followed by any number and combination of letters, underscores, and numbers.
  • Static, or 'hard-coded', values assigned to variables are known as literals .
  • The integer data type is for representing integral, or whole, numbers.
  • Integer literals can be specified in decimal, hexadecimal, or octal notation.
  • The float data type is for representing floating-point numbers, or numbers with fractional parts.
  • Float literals can be specified as decimal numbers in regular or exponential notation.
  • var_dump() is a built-in PHP function that ouptuts the type and value of a variable.
  • Additional resources:

Variables & Numeric Data Types

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:10
    • Lesson Overview
  • Working With Data 0:48
    • 8 Types of Data for PHP
  • Identifiers 1:40
    • Identifiers: Definition and Example
  • Variables 2:47
    • Variables Definition
    • Variables Syntax
  • Integer Data Type 4:44
    • Integer Data Type
    • Integer Literals
    • Examples
  • Float Data Type 6:26
    • Float Data Type
    • Float Literals
    • Example
  • Example: PHP Code Declaring Variables 8:06
    • Example: PHP Code Declaring Variables
  • var_dump() Function 9:59
    • var_dump()
    • Example: Code and Output
    • Example: var_dump() Function
  • Coding Conventions: Variables 12:32
    • Lower Camel Case Notation
    • Variable Name Lengths
  • Homework Challenge 14:54
    • Homework Challenge

Transcription: Variables & Numeric Data Types

Hello, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be introducing you to the concept of variables, as well as numeric data types in PHP.0004

What, specifically, are we going to cover in this lesson?0014

Well, we are going to talk about working with data in PHP.0016

We are going to talk about what are known as identifiers and variables; we are going to talk about two kinds of data 0020

that you can use within your PHP code--the integer data type and the float data type.0027

We are going to learn about another function that is built into PHP, called var_dump,0034

that is used to output information about variables, which you are going to learn about.0040

And then, we are also going to talk about coding conventions as they relate to variables.0043

Working with data: PHP, like all programming languages, essentially boils down to manipulating and processing pieces of data.0049

As such, each programming language has types of data that it allows you to use within your programs.0058

And PHP is no exception: it actually has 8 data types which it allows.0064

There are four data types known as the scalar data types, which refers to simple data types.0071

There is an integer and a float data type (as I had mentioned); there is also a string data type and a boolean data type.0077

There are also two composite data types, known as Object and Array, and two special data types, called Resource and Null.0085

As we progress through this course, between here and the advanced course, you are going to get exposure to all of these different data types.0093

We know that, in our code, we work with data; how do we work with data in our code?0103

One of the ways is through the use of identifiers.0108

Basically, what an identifier is, is a name you create in your code to refer to a particular piece of data.0111

What you do is: you assign data values to a name.0119

For example, you might have the name var, and you assign it the value, let's say, of the integer 1.0122

As you will see, variables actually begin with a dollar sign.0134

What this is, is actually an identifier; and you could assign it different pieces of data.0137

And that is actually what a variable is: it is an identifier that you can assign different pieces of data at different points throughout your code.0146

We might assign it 1 here, and then later in the code assign it a value of 2.0152

Basically, the assignment, again, refers to associating a particular piece of data with a name in your code that you can use to refer to that piece of data.0158

Variables--variable identifiers: we know that, as mentioned, variables are identifiers that can have different data values throughout your code.0169

They can have a value of 1 in the beginning, 2 in the end; and you can do that as often as needed--that is the purpose of variables.0180

What is the syntax for defining variables in our code?0187

Basically, variable identifiers begin with a dollar sign, followed by a variable name.0190

In PHP, names are case-sensitive, and they can be followed by an underscore and a letter, and then any other combination of letters, underscores, and numbers.0198

This is an example, right here: a dollar sign, followed by the variable name total--that is a variable identifier.0210

Additionally, when we assign static values to variables or hard-coded values such as 0, those are known as literals.0218

You will hear me refer to the term "an integer literal" or "a float literal," and basically, those are (in this case)0228

numbers that are hard-coded into your code.0233

Variables are declared using an assignment statement; and this right here is an assignment statement.0237

What it does is: you have the variable identifier; you have an equals sign, which is known as the assignment operator; and you have a literal value.0245

What this assignment statement does is: it is declaring to PHP that you are creating a variable named total.0264

And through this operator, you are assigning it the value of 0.0270

Now, if you were to use total in other parts of your code, when you reference it, it would also be referencing this value 0.0275

We mentioned that PHP has 8 different data types that are built in; and in today's lesson, 0286

we are going to talk about the two numeric data types that it has.0291

The first is an integer data type, and it is a data type that is used for representing integral or whole numbers, both positive and negative.0294

It is often referred to as the int data type; so you might hear somebody refer to a variable as being an int.0302

Now, integer literals can be specified in a number of different formats: they can be specified in decimal, hexadecimal, or octal notation.0309

In this course, we are only going to be using the decimal and hexadecimal notation.0320

The way you specify a decimal notation is simply: you just write the numbers as is.0325

For example, down here, we have a variable holding an integer in decimal notation, and it's simply the number 5.0330

To specify an integer literal in hexadecimal notation, you have to precede it with the character 0x or 0X.0341

For example, here we have declared a variable called hexInt, and we have set it to the hexadecimal value 5,0354

which actually, in decimal and hexadecimal, is the same; these both equal the decimal number 5.0361

Now, I am not mentioning octal notation because it's not very common and it's a little bit complicated,0367

but there will be a link in the Quick Notes which you can refer to to learn more about octal notation, 0373

if that is something that you have experience with and want to learn about how it works in PHP.0379

The other numeric data type that we have in PHP is the float data type; and that is used for representing floating-point numbers,0388

which are essentially numbers with fractional parts, like 1.25 (where this is the fraction).0395

You will sometimes hear these data types referred to as a double or a real 0403

(real as in real number, and double is sort of a legacy term that was used for floating-point numbers in the past).0409

You may actually come across those in literature and when you are reading about PHP.0416

Now, float literals, like integer literals, have a couple of different ways in which you can express them.0422

They are always expressed as decimal numbers, and you can express them either in simply a regular format or what is known as exponential notation.0430

Down here is what exponential notation looks like--these two variables.0442

We have declared a variable x and a variable y, and assigned it the value 1.5e3.0445

And for those of you that need a quick refresher on that, what this means is: 1.5x103, which equals 1,500.0453

So, this is an exponential way of declaring the number 1,500; and you can use, in PHP, either the lowercase e or the uppercase E.0466

In this course, we are only going to be using the lowercase e; that is just a convention we will use.0476

And whatever convention you use, be sure to stick with it, as it gives consistency to your code.0480

Let's take a look at some actual PHP code declaring some variables.0486

Here we have a PHP file, and it declares two integer variables--one called decInt, which we are going to have hold the value of -100,0492

and we're going to specify the -100 literal in decimal format, which is why we called the variable decInt.0505

We also have a variable called hexInt; and as you can see, it is preceded with a dollar sign, as all variable names are.0513

We have set it to the value of, in hexadecimal, -64; and you notice that it's preceded by a 0 and an x.0521

In hexadecimal notation, 64 actually is the same as 100; so this value right here is, in decimal, -100, as well.0530

Down here, we have declared two float variables in two different formats for the number 0.75.0542

Here is a regular floating-point notation, where you just write out the number in decimal format, as is.0549

And here is the same number, 0.75, written in exponential notation.0555

You can see, we have used a negative exponent--which...0561

those of you that have used this before know that that means to move the decimal one place back, so it's going to represent 0.75.0564

This is how you declare a variable; you have the variable name, 0573

the assignment operator (as it is called--the = sign), and a literal value which you assign to the variable.0577

This shows how you can do integers in decimal or hexadecimal notation and floats in regular or exponential notation.0584

There is a function built into PHP--it's similar in that it's like the phpinfo function that we talked about in the last lesson, in that0601

it's built in, and it's a function you can use in your code, and it does something for you.0610

In this case, the function we are going to be using is called var_dump, and what it does is: 0616

you specify a variable to this function, and then it outputs the type and value of that particular variable.0624

The way that it works is: let's say that you declare a variable here called testInt that you set equal to 100.0630

In order to output what the type and value of that variable is (and this is useful mostly for debugging purposes), you use this function call,0637

which is a statement, so you notice the semicolon here.0649

It's var_dump with parentheses, and within parentheses, you put the identifier for the variable you want to know the type and value of (in this case, testInt).0652

So actually, if you run this code (and let's say this was the only code in your file), it would output this right here.0665

What it says is that testInt is an int variable (or integer variable), and it currently has a value of 100.0670

If we go back to our file where we declared our integer and float variables, we are going to practice using the var_dump function0682

to confirm that, in fact, both the decInt and hexInt variables have the value of -100,0690

and also that the regFloat and expFloat variables have the same value of 0.75.0698

What we have here is a var_dump function call for each of the different variables, just followed by some HTML, so that 0706

they end up on different lines, so that they are easier to read.0717

And if we actually go and view this file in our web browser, we can see the output of var_dump, and it confirms what we had hoped and expected.0719

It was that the two integer values, decInt and hexInt, both are integer data type and have the value -100.0731

Additionally, the variables regFloat and expFloat are both float variables (as you can see here) and both have the value 0.75.0739

I'm going to turn, again, to the concept of coding conventions, which we introduced in the last lesson.0754

In this lesson, we are going to talk about coding conventions as they apply to variables.0758

Basically, it applies to variable names: we are going to use a naming convention called lower camel case notation.0764

The variables we defined in our file that we were working with already actually follow lower camel case notation.0775

What that notation states is that the first letter of the variable is always a lowercase letter (hence the "lower" part of lower camel case).0782

And then, each subsequent word in the variable name has its first letter, and only its first letter, capitalized.0790

As you can see here, we have hex with the lowercase letter; the next word in the variable name is Int, so we capitalize the first letter.0799

We could maybe create another variable that has a couple of words to demonstrate this more: let's call it daysOfWeek.0807

So here, we have a three-word variable name, daysOfWeek, and we can see that the O is capitalized, the W is capitalized, and d is lowercase.0814

The reason it gets that name, CamelCase notation, is because the variable sort of has humps in it when you look at the variable name,0825

because of the capitalization of each of the words.0832

Additionally, in addition to using lower camel case notation, because in PHP there is no practical limit to variable name lengths,0837

we want to pick meaningful names for our variables.0846

For example, in our code file, we declared this variable called regFloat; 0849

and that tells us that we are declaring a float value that is going to be in regular notation.0855

Up here, this variable I just created a second ago, daysOfWeek--that is a meaningful name.0862

It should let you know, as a programmer, that this variable refers to the number of days in a week.0867

Now, we actually could just call it y and maybe set it equal to 7, and it would represent days of the week.0872

But daysOfWeek is a lot more meaningful and useful to you as a programmer.0878

And because it is a convention, it is something you have to do that is not enforced by the program, 0884

but it's going to make your code easier to read and understand.0889

The homework challenge for today: for today's homework challenge, I want to have you create a new PHP script.0896

I want you to declare two integer variables in it: one using decimal notation and one using hexadecimal notation, both equal to the literal value 10.0902

I want to also have you declare two float variables and set them equal to the literal 1234.56, and do it once in regular notation and once in exponential notation.0916

And then, use the var_dump function to output the type and value of all four variables you declared.0929

You should be able, if it was done correctly, to confirm that both of your integer variables have the value of 10 and are of the int data type,0938

and both of your float variables have the value 1234.56 and are also of the float data type.0947

When you do this, be sure to follow the lower camel case notation.0954

And also, begin the practice of using meaningful names.0958

Again, it is going to be a simple program, so you might remember what x or y refers to; but in the name of good practice,0961

maybe give your variable a name--for example, the floating-point name--you could call it subTotal.0969

That would refer to this value right here; and so, subTotal is a little more meaningful than if we just set this value equal to a variable called y.0978

That ends today's lecture; thank you again for watching, and we look forward to seeing you next time.0993