PHP: Introduction

What is PHP?

PHP (Hypertext Preprocessor) is a widely-used, open-source scripting language especially suited for web development. It can be embedded into HTML and is particularly strong in server-side programming.

PHP is known for its simplicity, flexibility, and ability to interact with databases, making it a popular choice for creating dynamic web pages and applications.

Key Features of PHP

  • Server-side scripting
  • Command-line scripting
  • Writing desktop applications
  • Cross-platform compatibility
  • Database integration
  • Large community and extensive documentation

Basic PHP Syntax

PHP code is executed on the server, and the result is returned to the browser as plain HTML. PHP files have a file extension of ".php".

A simple PHP script:


<?php
    echo "Hello, World!";
?>
            

Explanation:

  • PHP code starts with <?php and ends with ?>
  • The echo statement is used to output text
  • Each PHP statement ends with a semicolon (;)

PHP in HTML

PHP can be embedded directly into HTML. This allows you to mix static HTML with dynamic PHP code.

Example:


<!DOCTYPE html>
<html>
<body>

<h1>My First PHP Page</h1>

<?php
echo "The current date is " . date("Y-m-d");
?>

</body>
</html>
            

In this example, PHP is used to dynamically insert the current date into an otherwise static HTML page.

PHP Variables

Variables in PHP start with a $ sign, followed by the name of the variable.

Example:


<?php
$txt = "Hello, World!";
$x = 5;
$y = 10.5;

echo $txt;
echo "<br>";
echo $x + $y;
?>
            

This script demonstrates string and numeric variables, and shows how to perform operations with them.

Conclusion

PHP is a powerful language for web development, offering a wide range of features and capabilities. As you progress, you'll learn about more advanced concepts like functions, arrays, object-oriented programming, and database integration.