w1coding.comAll tutorials

PHP Variables

PHP lessons
  1. Environment Setup
  2. Introduction
  3. Variables
  4. Basics
  5. Operators
  6. Control
  7. Loops
  8. Variables
  9. Functions
  10. Arrays
  11. Superglobal
  12. File
  13. Loops
  14. Date
  15. Loops
  16. Strings
  17. Error
  18. Class
  19. File
  20. Include
  21. Sessions
  22. Mysql
  23. Cookies
  24. Forms
  25. Cookies
  26. Regular
  27. Authenthication
  28. Fileupload
  29. Mvc
  30. Responsive

Variables and Data Types in PHP

Variables in PHP are used to store data values. PHP automatically determines the type of variable based on its value (dynamic typing).

Declaring Variables

To declare a variable, use the $ sign followed by the variable name:

      
        <?php
          $name = "John"; // String
          $age = 30;      // Integer
          $height = 5.9;  // Float
        ?>
      
    

Common Data Types

Some common data types in PHP include:

  • String: A sequence of characters
  • Integer: A whole number
  • Float: A number with a decimal point
  • Boolean: True or false
  • Array: A collection of values
  • Object: An instance of a class

Activity

Try It Yourself!

Declare a variable for your favorite color and echo it to the page.

Quiz

Quick Quiz

  1. What symbol is used to declare a variable in PHP?
  2. What are the basic data types in PHP?

Answers: The $ symbol; String, Integer, Float, Boolean, Array, Object.