2. PHP Basics

🔹 1. Variables & Data Types

✅ Variables

Variables are used to store data.

👉 In PHP, variables start with $

$name = "Aditya";
$age = 25;

✅ Rules:

  • Must start with $
  • Cannot start with a number
  • Case-sensitive ($name$Name)

✅ Data Types in PHP

TypeExampleDescription
String"Hello"Text
Integer10Whole numbers
Float10.5Decimal numbers
Booleantrue/falseTrue or false
Array["a", "b"]Multiple values
Objectnew ClassName()Complex data
$name = "Aditya";   // String
$age = 25; // Integer
$price = 99.99; // Float
$isActive = true; // Boolean

🔹 2. Constants

Constants are values that cannot be changed once defined.

define("SITE_NAME", "SOA Technology");
echo SITE_NAME;

✅ Rules:

  • No $ symbol
  • Always global
  • Value cannot be modified

🔹 3. Operators

Operators are used to perform operations on variables


➤ Arithmetic Operators

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
$a = 10;
$b = 5;
echo $a + $b; // 15

➤ Comparison Operators

OperatorMeaning
==Equal
===Equal (value + type)
!=Not equal
>Greater
<Less
$a = 10;
$b = "10";var_dump($a == $b); // true
var_dump($a === $b); // false

➤ Logical Operators

OperatorMeaning
&&AND
`
!NOT
$x = true;
$y = false;var_dump($x && $y); // false

🔹 4. Echo vs Print

Both are used to display output


✅ echo

  • Faster
  • Can print multiple values
echo "Hello ", "World";

✅ print

  • Slower than echo
  • Returns value (1)
print "Hello World";

🔥 Key Difference

Featureechoprint
SpeedFasterSlower
Multiple argumentsYesNo
Return valueNoYes (1)

🔹 5. Comments

Comments are used to explain code (ignored by PHP)


➤ Single-line comment

// This is a comment
# This is also a comment

➤ Multi-line comment

/*
This is a
multi-line comment
*/

✅ Summary

  • Variables store data ($name)
  • Data types define the kind of data
  • Constants are fixed values
  • Operators perform operations
  • echo & print show output
  • Comments explain code