Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

3. Data Types & Output

Lesson 3 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Output Functions in PHP

Output functions are used to display data on the browser.

1. echo

  • Most commonly used
  • Faster than print
  • Can display multiple values
<?php
echo "Hello World";
?>

2. print

  • Similar to echo
  • Returns value (1)
  • Slightly slower
<?php
print "Welcome to PHP";
?>

Difference Between echo and print

Featureechoprint
SpeedFasterSlower
Return ValueNoYes
Multiple ValuesYesNo
Exam Tip: echo is preferred for output because it is faster.

---

Datatypes in PHP

A datatype defines what type of data a variable can store.

PHP Supports 8 Main Datatypes

  1. Integer: Stores whole numbers.
    $x = 10;
  1. Float (Double): Stores decimal values.
    $price = 99.99;
  1. String: Stores text.
    $name = "PHP";
  1. Boolean: Stores true or false.
    $isActive = true;
  1. Array: Stores multiple values in one variable.
    $colors = array("Red", "Green", "Blue");
  1. Object: Stores class instances.
    class Test {}
    $obj = new Test();
  1. NULL: Represents no value.
    $x = NULL;
  1. Resource: Stores external resources like database connections.
Exam Point: PHP is a loosely typed language.

---

Type Casting in PHP

Type casting is used to convert a variable from one data type to another.

Syntax

(type) $variable;

Common Casts

  • (int) - Cast to Integer
  • (bool) - Cast to Boolean
  • (float) - Cast to Float
  • (string) - Cast to String
  • (array) - Cast to Array

Example: Integer to Float Conversion

<?php
$x = 10;
$f = (float)$x;

echo "Value: " . $f; // Outputs: 10
echo "\n";
var_dump($f);        // Outputs: float(10)
?>
Why does echo show 10 instead of 10.0? PHP's echo statement converts numbers to strings for display. If a float has no fractional part (like 10.0), echo displays it as 10 to keep it clean. To verify the type, always use var_dump().