Course Content
About Lesson

How do you write PHP code?

PHP code is written between <?php and ?> tags. Example:

PHP
<?php
  echo "PHP code runs here";
?>

PHP statements are executed one by one. Each PHP statement ends with a semicolon (;). Example:

PHP
<?php
  echo "Hello";
  echo "World";
?>

Variables in PHP are used to store data. A variable starts with the $ symbol followed by the name of the variable. Rules:

  • Start with $
  • Can’t start with a number
  • Are case-sensitive

Example:

PHP
<?php
  $name = "John";
  $age = 25;
  echo $name;
?>
  • String: Text
  • Integer: Whole numbers
  • Float (Double): Decimal numbers
  • Boolean: true or false
  • Array: Multiple values
  • Object: Instance of a class
  • NULL: No value

Example:

PHP
<?php
  $text = "Hello";        // String
  $num = 123;              // Integer
  $price = 45.67;          // Float
  $is_valid = true;        // Boolean
  $colors = array("Red", "Blue"); // Array
  $x = null;               // NULL
?>

Use echo or print to display variables. Example:

PHP
<?php
  $name = "Alice";
  echo "My name is " . $name;
?>