Mostrando postagens com marcador PHP Basics. Mostrar todas as postagens
Mostrando postagens com marcador PHP Basics. Mostrar todas as postagens

PHP Data Types Explained: Integers, Floats, Strings, and Booleans

In this guide of our Progressive PHP Course, we will explore the fundamental Data Types: numbers, text, logical states, and the complex structures you will use daily in your backend projects.

Numeric Types: Integers and Floats

Math is the foundation of computing. Practically everything your script does under the hood involves high-performance numerical processing. In PHP, we work with two main groups of numbers:

1. Integers

As the name implies, these are whole numbers without a decimal point. They are perfect for counting items, user IDs, and array indexes.

  • Positive: 10, 500, 12345
  • Negative: -15, -100
  • Zero: 0
<?php
    echo 2026; // This is an Integer
?>

2. Floats (Floating Point Numbers / Doubles)

These are decimal numbers, often referred to as "fractions" or "broken numbers." Crucial rule: In programming, we follow the international standard. We use a dot (.) for decimals, never a comma.

  • Examples: 1.99 (prices), 4.5 (ratings), 0.007 (scientific precision)
⚙️ Technical Note: PHP also allows you to represent numbers in alternative bases, such as Octal (base 8, starts with 0), Hexadecimal (base 16, starts with 0x), and Scientific Notation (e.g., 6.0e+23).

Logical Type: Booleans

The heart of programming logic is binary. The Boolean type represents the smallest unit of decision-making in your code. It accepts only two possible states:

  • TRUE
  • FALSE

In PHP, certain values are automatically evaluated as false in logical tests (we call these "falsy" values): the integer 0, the float 0.0, an empty string "", or the NULL value. Practically everything else evaluates to TRUE. Mastering this concept is the key to creating smart, conditional algorithms.

Text Type: Strings

Strings are sequences of characters used to represent names, messages, or alphanumeric data. They must always be wrapped in quotes (single ' ' or double " ").

  • Flexibility: A string can be a single letter ('a'), a word ("PHP"), or a massive paragraph of text.
  • The Crucial Difference: Remember that 10 is a number (it can be mathematically processed), while "10" is a string (it is processed as a text character).
<?php
    echo "Welcome to the Progressive PHP Course!"; // This is a String
?>

Advanced & Compound Data Types

As we progress in our course, you will encounter types that group information in much more complex ways. Here is a sneak peek:

  • Array: An organized list that holds multiple values in a single structure.
  • Object: The foundation of Object-Oriented Programming (OOP), representing real-world entities.
  • NULL: A special type that explicitly represents a variable with absolutely no value.
  • Resource: Special variables that hold references to external resources, like database connections or open files.

📌 Lesson Takeaway:

In this introduction, you learned that PHP categorizes information to ensure performance and security. Knowing that a price must be a Float and a username must be a String will prevent critical bugs in your future systems.

Quick question for you: What data type would you use to store a user's Age? Let me know in the comments below!


What's Next? Declaring Variables!

Now that you know the different types of data, it's time to learn how to store them in your computer's memory. In the next tutorial, we will finally create Variables, the ultimate building blocks of any PHP application.

Go to the Next Lesson in the Syllabus →

PHP Practice Exercises: Mastering Echo, Output, and HTML Integration

Below, I have prepared a list of 10 practical challenges to train your syntax, logic, and HTML integration in PHP. Try to solve all of them without copying and pasting! Open your text editor, start your XAMPP server, and let's get to work.


The Exercise List: PHP Output & Syntax

Challenge 1: The Classic Phrase
Create a PHP script that outputs the following phrase to an HTML document: "The first program we write, we never forget!".
Challenge 2: Digital Business Card
Develop a script that prints your full name on the first line, your address on the second line, and your ZIP code and phone number on the third line. Use HTML tags inside your PHP strings to organize the line breaks.
Challenge 3: The Playlist
Write a script that outputs the chorus of your favorite song to the screen.
(Hint: You will need multiple `<br />` tags to separate the verses properly!)
Challenge 4: Surprise Message
Implement a PHP page that prints a funny or special message. Take a screenshot of the browser output and send it to a friend saying you built a "creative virus" that infected your localhost!
Challenge 5: Developer Goals
Create a program that displays a bulleted list (using HTML `<ul>` and `<li>` tags inside your `echo`) of what web apps or systems you intend to build once you master PHP.
Challenge 6: ASCII Art (The Square)
Write a program that produces the following geometric figure in the browser using uppercase 'X':
XXXXX
X   X
X   X
X   X
XXXXX
Challenge 7: The Grade Report
You were hired by a school. As your first task, develop a script that generates exactly the following text-based table:
STUDENT           GRADE
=========         =====
ALICE             9.0  
MARIO             TEN
SERGIO            4.5   
SHIRLEY           7.0
Challenge 8: Stylized Initial
Create a script to print the letter "P" (for PHP Progressive) on the screen using characters. Use the letter "L" below as an inspiration for the format:
L
L
L
LLLLL
Challenge 9: CLI-Style Menu
Simulate a terminal-based customer registration menu with the following visual structure:
Customer Management System
0 - Exit
1 - Create
2 - Update
3 - Delete
4 - Read
Select an option: _
Challenge 10: The Christmas Tree
Implement a program that draws a "pine tree" on the screen. Try using different characters (like `*`, `O`, `+`) to simulate ornaments on the tree!
        X
       XXX
      XXXXX
     XXXXXXX
    XXXXXXXXX
   XXXXXXXXXXX
  XXXXXXXXXXXXX
 XXXXXXXXXXXXXXX
        XX
        XX
       XXXX

💡 Master Tips for ASCII Art:

  • To keep your drawings (like the pine tree and the table) perfectly aligned in the browser, wrap your PHP output inside an HTML <pre> (Preformatted Text) tag.
  • Practice using both single quotes (') and double quotes (") to feel the difference.
  • Never forget the mandatory semicolon (;) at the end of each statement!

I'm waiting for your source code in the comments! Which exercise was the most challenging for you?


What's Next? Variables and Data Types!

Printing hardcoded text is fun, but real applications need to store, manipulate, and process dynamic information. In our next tutorial, you will learn the most fundamental concept in programming: Variables. This is where your code truly starts to "think."

Go to the Next Lesson in the Syllabus →

Veja também: