PHP Basics: Echo, Variables, Comments, Data Types & Operators
Master the fundamentals of PHP with easy explanations and practical examples.
Introduction
PHP is one of the most popular server-side programming languages used to create dynamic websites and web applications. Before learning advanced topics, every beginner should understand echo statements, variables, comments, data types, and operators.
1. Echo in PHP
The echo statement prints output to the browser.
<?php
echo "Hello World!";
echo "<br>";
echo "Welcome to PHP!";
?>
2. Variables
Variables store information and always start with the $ symbol.
<?php
$name = "Harsh";
$age = 22;
echo $name;
echo "<br>";
echo $age;
?>
- Starts with $
- Must begin with a letter or underscore
- Cannot start with a number
- Case-sensitive
3. Comments
Comments help explain code and are ignored during execution.
// Single-line comment
# Another single-line comment
/*
Multi-line
Comment
*/
4. Data Types
| Data Type | Description | Example |
|---|---|---|
| String | Stores text | "Harsh" |
| Integer | Stores whole numbers | 22 |
| Float | Stores decimal numbers | 99.99 |
| Boolean | Stores true or false | true / false |
String Example
<?php
$name = "Harsh";
echo $name;
?>
Integer Example
<?php
$age = 22;
echo $age;
?>
Float Example
<?php
$price = 99.95;
echo $price;
?>
Boolean Example
<?php
$isLoggedIn = true;
if($isLoggedIn){
echo "User Logged In";
}
?>
5. Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 |
| - | Subtraction | 10 - 5 |
| * | Multiplication | 10 * 5 |
| / | Division | 10 / 5 |
| % | Modulus | 10 % 3 |
<?php
$a = 10;
$b = 3;
echo $a + $b;
echo "<br>";
echo $a - $b;
echo "<br>";
echo $a * $b;
echo "<br>";
echo $a / $b;
echo "<br>";
echo $a % $b;
?>
6. Comparison Operators
| Operator | Meaning |
|---|---|
| == | Equal To |
| === | Equal Value & Type |
| != | Not Equal |
| !== | Not Equal Value or Type |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
<?php
$x = 10;
$y = 20;
var_dump($x < $y);
var_dump($x == $y);
var_dump($x != $y);
?>
7. Logical Operators
| Operator | Meaning |
|---|---|
| && | AND - Both conditions must be true |
| || | OR - At least one condition must be true |
| ! | NOT - Reverses the result |
<?php
$age = 20;
$citizen = true;
if($age >= 18 && $citizen){
echo "Eligible";
}
$isAdmin = false;
if(!$isAdmin){
echo " Access Denied";
}
?>
8. Complete PHP Example
<?php
$name = "Harsh";
$age = 22;
$salary = 50000.50;
$isStudent = true;
echo "Name: " . $name;
echo "<br>";
echo "Age: " . $age;
echo "<br>";
echo "Salary: " . $salary;
echo "<br>";
echo "Student: " . ($isStudent ? "Yes" : "No");
?>
Conclusion
Echo, variables, comments, data types, and operators are the building blocks of PHP programming. Once you understand these concepts, you can easily move on to conditions, loops, functions, arrays, forms, sessions, and database development.