fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← PHP

PHP / 3 MIN READ

PHP Basics

Learn the fundamental concepts and syntax of PHP programming.

From the original Fervor library. Examples may use older package versions.

Introduction to PHP

PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language designed for web development. It is especially suited for creating dynamic web pages and can be embedded into HTML. PHP is known for its simplicity, speed, and flexibility, making it a popular choice among web developers.

Key Features of PHP

  1. Open Source: PHP is free to use and distribute.
  2. Cross-Platform: PHP runs on various platforms, including Windows, Linux, Unix, and macOS.
  3. Embedded in HTML: PHP code can be easily integrated with HTML, making it simple to add dynamic content to web pages.
  4. Server-Side Execution: PHP scripts are executed on the server, and the result is sent to the client’s web browser.
  5. Database Support: PHP supports numerous databases, such as MySQL, PostgreSQL, SQLite, and more.
  6. Large Community: PHP has a vast and active community, providing extensive documentation and a wealth of third-party libraries and frameworks.

Basic PHP Syntax

PHP scripts are typically embedded in HTML files. The PHP code is enclosed in <?php ?> tags. Here’s an example of a basic PHP script:

<!DOCTYPE html>
<html>
<head>
    <title>My First PHP Page</title>
</head>
<body>

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

</body>
</html>

In this example, the PHP code within the <?php ?> tags will output “Hello, World!” to the web page.

Variables and Data Types

PHP supports various data types, including integers, floats, strings, arrays, and objects. Variables in PHP are declared with a dollar sign ($) followed by the variable name.

<?php
$integer = 10;
$float = 20.5;
$string = "Hello, PHP!";
$array = array(1, 2, 3, 4, 5);
?>

Control Structures

PHP provides standard control structures, such as if-else statements, switch statements, and loops (for, while, do-while).

If-Else Statement

<?php
$number = 10;

if ($number > 0) {
    echo "$number is positive.";
} else {
    echo "$number is not positive.";
}
?>

For Loop

<?php
for ($i = 0; $i < 5; $i++) {
    echo "The number is: $i <br>";
}
?>

While Loop

<?php
$i = 0;

while ($i < 5) {
    echo "The number is: $i <br>";
    $i++;
}
?>

Functions

Functions in PHP are declared using the function keyword. Functions help organize and reuse code.

<?php
function greet($name) {
    return "Hello, $name!";
}

echo greet("Alice");
?>

Working with Forms

PHP is commonly used to handle form data submitted via HTTP POST or GET methods. Here’s an example of a simple form and its handling script.

HTML Form

<!DOCTYPE html>
<html>
<head>
    <title>Form Example</title>
</head>
<body>

<form method="post" action="process_form.php">
    Name: <input type="text" name="name">
    <input type="submit" value="Submit">
</form>

</body>
</html>

PHP Script (process_form.php)

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars($_POST['name']);
    echo "Hello, $name!";
}
?>

In this example, when the form is submitted, the data is sent to process_form.php, where it is processed and displayed.

Conclusion

This basic overview provides a glimpse into PHP’s capabilities and syntax. PHP is a powerful tool for web development, offering extensive features and a supportive community. To delve deeper, explore PHP’s official documentation and experiment with writing and running PHP scripts.

Keep your curiosity going.Explore more PHP →
287 TUTORIALS · 22 TOPICSREADY