Data types

Understanding data types

In PHP

In the last section, we discussed how Variables can be used to store various types of data in PHP. Let’s learn about the various types of data that can be stored. 

If you are familiar with other programming languages, you would know that you normally have to tell the computer what type of data a variable can store. In PHP, you don’t have to! But we still need to know what data types we can use.

In this video you will learn about different Data Types. Click on play to learn more.

Let’s Recap the video. Data types can be scripted as follows:

(click on the blue arrows for detail on each data type)

This data type can only have values of either “True” or “False”.

This data type can have the value of a signed (positive or negative) integer.

Let’s practice writing some PHP code. Click on the button below to visit W3Schools coding playground and change the integer from 5985 to something else. Notice how the PHP code sits inside the HTML < body > code in the W3 schools compiler. 

 

Be creative and play around. Click run to see what happens.

This data type can have the value of any rational number. It allows us to store numbers with decimals unlike the Integer data type.

Let’s practice writing some PHP code. Click on the button below to visit the W3 Schools coding playground and change the float from 10.365 to something else. 

Be creative and play around. Click run to see what happens.

This data type can have the value of a letter, word, sentence, or symbol. Basically it can contain any ordered collection of characters. It can also contain numbers and punctuation marks. Note that all String values are placed in double quotation marks. “The messages that are sent by the dronesare an example of a String Data Type.

Let’s practice writing some PHP code. We will be writing PHP in our own coding playground or php compiler below. 

Change the values of the variables. Be creative and play around. Click run to see what happens.

Remember – your task is to help Agent Gail to build the drone software that will help locate spaceship Galaxy X using location coordinates. 

The Galactic Coordinate system locates planets and objects in the Milky Way on lines of latitude and longitude – similar to how we do it on Earth. Do you know what “coordinates” are? 

Coordinates are what GPSs use to locate exactly where you are on the map. We use lines of latitude and longitude to pinpoint an exact location. For instance – Sandton City Mall in Johannesburg is located at: 

  • Latitude: 28.0527° E
  • Longitude: 26.1088° S, 

(We also write latitude and then longitude – that’s the global standard)

If you’d like to read up more about how to locate objects in the Milky Way galaxy –

For the drones to be able to send location coordinates to you and Agent Gail, the drone must be able to first (1) save the coordinates and then (2) send the coordinates. Your first task is ensure that the coordinates are saved then sent.

Here are some steps to guide you:

  1. Create two variable named “latitude ” and “longitude”
  2. Assign the following coordinates to your variables
    • Latitude: -0.97377, 
    • Longitude: -45.64867
  3. Remember the data type of variables is float  (needs to be removed )
  4. Use the echo statement to print the coordinates 
Click on the “fork me” below to edit the PHP code directly.

What data type is the above coordinate?

Click here to find out...

This is a float data type

In this section, we talked about variables and data types and  how they help us store different values. We introduced the following 

  • Variables
  • Data types
  • the different types of data types: 
    • integer: int
    • text: string 
    • numbers with decimals: float
    • true/false: boolean

You built a storage system for the drones to store coordinates.

Next up: Let's learn about arrays