BCA / B.Tech 9 min read

Whitespace in PHP

Whitespace in PHP in Hindi | Whitespace in PHP in Hindi: 


  • Whitespace in PHP does not affect the functionality of the code, but its correct use makes the code organized, professional, and easy to understand.
  • By using whitespace, you not only become a good programmer, but it also becomes simple for your team to understand the code.
  • Whitespace means empty spaces that are not visible in programming code. This includes spaces, tabs, and new lines.
  • Whitespace in PHP helps in reading and understanding the code, but it does not affect the functionality of the PHP code.
Types of Whitespace in PHP in Hindi | Types of Whitespace:

Space: An empty space created by the space button.

Example:

$a = 10; // This line and "$a=10;" will both work the same.

Tab: Indenting the code with the help of a tab. This makes the code organized and easy to read.

Example:

if ($a > 5) {
    echo "Value is greater than 5"; // A tab has been used here.
}

New Line: When we press Enter, the code goes to a new line.

Example:

echo "Hello";
echo "World"; // This will output on two different lines.

Importance of Whitespace in PHP in Hindi | Importance of Whitespace in PHP:

1. Making the code easy to read (Readability): Whitespace makes the code organized and easy to understand.

// Without whitespace
if($a>5){echo"Value is greater than 5";}

// With whitespace
if ($a > 5) {
    echo "Value is greater than 5";
}

2. Help in code formatting: The correct use of whitespace makes the code look clean and professional.

function add($a, $b) {
    return $a + $b;
}

3. No effect on the PHP interpreter: Whitespace does not matter to PHP. The PHP interpreter ignores it.

echo "Hello World"; // Correct
echo     "Hello World"; // Correct

Where is whitespace used?

1. In conditions and loops:

if ($x > 10) {
    echo "X is greater than 10";
}

for ($i = 0; $i < 10; $i++) {
    echo $i;
}

2. In function declaration and call:

function greet($name) {
    return "Hello, " . $name;
}

echo greet("Rahul");

3. In PHP with HTML:

<!DOCTYPE html>
<html>
<head>
    <title>PHP and Whitespace</title>
</head>
<body>
    <?php
        echo "This is PHP code";
    ?>
</body>
</html>

4. In writing SQL queries:

$sql = "SELECT * 
        FROM users 
        WHERE age > 18";

Rules and precautions related to whitespace:

  • Use in the right place: Use whitespace to organize the code, not to make the code unnecessarily large.
  • Pay attention to indentation: Use Tab or Spaces consistently to indent the code.

if ($a > 10) {
    echo "Value is greater than 10"; // Correct
}
  • Effect of whitespace in HTML: Whitespace in HTML can be visible in the browser.
<p>This     is     an     example.</p>

  • No effect of whitespace inside PHP: Whitespace does not affect the output in PHP.
echo    "Hello World"; // Correct

Example: Correct use of whitespace

<?php
// Well-formatted code
$number = 15;

if ($number > 10) {
    echo "Number is greater than 10";
} else {
    echo "Number is 10 or less";
}
?>

Example: Incorrect use of whitespace

<?php
// Code without formatting
$number=15;if($number>10){echo"Number is greater than 10";}else{echo"Number is 10 or less";}
?>