ADVERTISEMENT
ADVERTISEMENT

What is the Basic Syntax of PHP?

This tutorial will introduce you to the basic syntax of PHP, explain the common file extensions used for PHP files, and discuss how PHP handles case sensitivity. We will provide simple examples along the way to help you understand these concepts.

PHP scripts are usually embedded in HTML or used to generate HTML. The basic PHP code is written between PHP opening and closing tags.

PHP Tags

A PHP file starts with the PHP opening tag <?php and ends with the closing tag ?>. Everything between these tags is interpreted as PHP code.

Example

<?php
// Your PHP code goes here
echo "Learn PHP -Techskillguru!";
?>

Note: In files that contain only PHP code, the closing tag ?> is optional and often omitted to prevent accidental output.

Basic Output

The echo and print statements are used to output text to the browser. The most commonly used is echo.

Example

<?php
echo "Welcome to PHP programming!";
?>

You can also mix HTML with PHP:

<!DOCTYPE html>
<html>
<head>
    <title>PHP Output Example</title>
</head>
<body>
    <?php echo "<h1>Hello, World!</h1>"; ?>
</body>
</html>

PHP File Extensions

PHP files typically use the .php extension. This tells the web server that the file contains PHP code that should be processed by the PHP engine.

  • .php: The standard file extension.

Example:

If you create a file named index.php with the following content:

<?php
echo "This is a PHP file with the .php extension.";
?>

When accessed via a web server (e.g., http://localhost/index.php), the PHP code will be executed, and the output will be displayed in your browser.

PHP Case Sensitivity

PHP’s case sensitivity rules can be summarized as follows:

Variable Names

Variable names in PHP are case sensitive. This means that $variable and $Variable are considered different.

Example

<?php
$greeting = "Hello!";
echo $greeting;    // Outputs: Hello!
echo $Greeting;    // Error or no output because $Greeting is not defined
?>

Function Names

Function names in PHP are not case sensitive. For example, echo, ECHO, and EcHo all refer to the same function. However, it is recommended to use the standard lowercase to maintain consistency.

<?php
ECHO "Hello PHP!<br>";
echo "Hello PHP!<br>";
EcHo "Hello PHP!<br>";
?>
  • Keywords: Not case-sensitive. You can write if, ELSE, While, etc., in any case.
  • Functions (Built-in and User-Defined): Not case-sensitive. Calling myFunction(), MYFUNCTION(), or any variation will work the same.
  • Classes: The class names are case-insensitive when declaring and instantiating. However, remember that class properties (variables) are case-sensitive.

ADVERTISEMENT

ADVERTISEMENT