HOME HTML EDITOR C JAVA PHP

PHP Variables

Variables are "containers" for storing information. In PHP, a variable starts with the $ sign, followed by the name of the variable.

1. Creating (Declaring) Variables

PHP has no command for declaring a variable; it is created the moment you first assign a value to it.

<?php
  $txt = "Hello World!";
  $x = 5;
  $y = 10.5;

  echo $txt;
  echo $x + $y; // Outputs: 15.5
?>

2. Rules for PHP Variables

3. Outputting Variables

The PHP echo statement is often used to output data to the screen. You can combine text and variables using the dot (.) operator.

<?php
  $name = "Gemini";
  echo "I am " . $name . "!";
?>

4. Loosely Typed Language

PHP is a Loosely Typed language. This means you do not have to tell PHP which data type the variable is (like Integer or String). PHP automatically associates a data type to the variable, depending on its value.

Remember: Variable names should be descriptive so that you (and other developers) know what data they are holding. Use $user_email instead of just $e.