PHP Tutorial



VARIABLES IN PHP


๐Ÿ’ก PHP Variables

Variables in PHP are used to store data like text, numbers, or other values. They are declared using the $ sign, followed by the variable name.

Syntax: $variable_name = value;

๐Ÿ”น Example:

<?php
  $name = "Technorank";
  $age = 20;
  $is_online = true;

  echo "Name: " . $name;
  echo "<br>";
  echo "Age: " . $age;
?>
  

๐Ÿ“Œ Rules for Variable Names

  • Must start with $ followed by a letter or underscore
  • Cannot start with a number
  • Can only contain letters, numbers, and underscores (A-z, 0-9, _)
  • Are case-sensitive ($var and $Var are different)

๐Ÿงช Example: Multiple Variables

<?php
  $x = 5;
  $y = 10;
  $sum = $x + $y;

  echo "Sum is: " . $sum;
?>
  

๐Ÿ”„ Variable Output

You can use echo or print to display variables:

<?php
  $greeting = "Hello";
  $name = "Technorank";

  echo $greeting . " " . $name;
?>
  
โš ๏ธ Note: PHP uses concatenation (.) to join text and variables.

๐Ÿ› ๏ธ Quick Tips

  • Always start variable names with $
  • Use meaningful names like $username or $price
  • Use comments to describe complex logic
โœ… Summary: PHP variables store values for later use. Use $ followed by a name, and assign using =. Output them using echo or print.

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review