Now that you've set up your PHP development environment, it's time to dive into the basics of PHP programming. In this lesson, we'll cover variables, data types, and operators, which are fundamental building blocks for writing PHP code.

Variables

In PHP, variables are used to store data that can be used and manipulated later in your code. Variables in PHP start with a dollar sign ($) followed by the variable name. Variable names are case-sensitive and must start with a letter or an underscore.

To assign a value to a variable, use the equal sign (=):

$name = "John Doe";
$age = 30;
$score = 85.5;

Data Types

PHP supports several data types:

  1. String: A sequence of characters, used to store text. Strings can be enclosed in single or double quotes.
$string1 = 'Hello, World!';
$string2 = "Hello, World!";
  1. Integer: A whole number without a decimal point, used to store numeric values.
$int1 = 42;
$int2 = -10;
  1. Float (or double): A number with a decimal point, used to store decimal values.
$float1 = 3.14;
$float2 = -0.5;
  1. Boolean: Represents either true or false.
$bool1 = true;
$bool2 = false;
  1. Array: An ordered collection of values, which can be of any data type.
$array1 = array(1, 2, 3, 4, 5);
$array2 = array("apple", "banana", "cherry");
  1. Object: An instance of a class, used to represent complex data structures and behaviors.
class Person {
  public $name;
  public $age;
}

$person = new Person();
$person->name = "John Doe";
$person->age = 30;
  1. NULL: Represents a variable with no value.