How to declare a variable in PHP?

Introduction:
Variables in PHP are used to store data that can be accessed and modified throughout the script. PHP variables start with a $ symbol and do not require explicit type declaration since PHP is loosely typed.

Example:

<?php
$name = "John Doe"; // String
$age = 25;          // Integer
$isStudent = true;  // Boolean

echo "Name: $name, Age: $age, Student: $isStudent";
?>

Explanation:

  • $name is assigned a string value "John Doe".
  • $age is assigned an integer value 25.
  • $isStudent is assigned a boolean value true.
  • The echo statement outputs the concatenated string along with variable values. PHP automatically converts the variables to strings where required.

Leave a Reply

Your email address will not be published. Required fields are marked *