EZ

Eduzan

Learning Hub

Eduzan
Eduzan / PHP

PHP Basic Concepts

PHP Overview: A Beginner’s Guide to Syntax

PHP is a versatile server-side language widely used in web development. Its straightforward syntax makes it suitable for both beginners and advanced developers. In this guide, we will explore the basic syntax of PHP. PHP scripts can be embedded within HTML using special PHP tags.

Basic PHP Syntax

PHP code is executed between PHP tags. The most commonly used tags are <?php ... ?>, which mark the beginning and end of PHP code. This is known as Escaping to PHP.

<?php
    // PHP code goes here
?>

These tags, also known as Canonical PHP tags, indicate to the PHP parser which parts of the document to process. Everything outside these tags is treated as plain HTML. Each PHP command must end with a semicolon (;).

PHP Syntax Example

<?php
    // Using echo to display a message
    echo "Greetings from PHP!";
?>

Output:

Greetings from PHP!

Embedding PHP in HTML

You can embed PHP code within an HTML document using PHP tags. In this example, the <?php echo "Welcome to PHP!"; ?> dynamically adds content into the HTML.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Example</title>
</head>
<body>
    <h1><?php echo "Welcome to PHP!"; ?></h1>
</body>
</html>

Output:

Welcome to PHP!

Short PHP Tags

Short tags allow for a more compact syntax, starting with <? and ending with ?>. This feature works only if the short_open_tag setting in the php.ini configuration file is enabled.

<?
    echo "Short tag in action!";
?>

Output:

Short tag in action!

Case Sensitivity in PHP

PHP is partly case-sensitive:

  • Keywords (such as ifelsewhileecho) are not case-sensitive.
  • Variables are case-sensitive.

Example:

<?php
    $Message = "Hello, Developer!";

    // Outputs: Hello, Developer!
    echo $Message;

    // Error: Undefined variable $message
    echo $message;
?>

Output:

Hello, Developer!
Notice: Undefined variable: message

Comments in PHP

Comments help make the code easier to understand and maintain. They are ignored by the PHP engine.

  • Single-Line Comments: Single-line comments are brief explanations added with // or #.
<?php
    // This is a single-line comment
    echo "Single-line comment example!";

    # This is another single-line comment
?>

Output:

Single-line comment example!
  • Multi-Line Comments: Multi-line comments span several lines, beginning with /* and ending with */.
<?php
    /* This is a multi-line comment.
       PHP variables start with a $ sign.
    */

    $message = "Learning PHP!";
    echo $message;
?>

Output:

Learning PHP!

Variables and Data Types

Variables store data and are defined with the $ symbol followed by the variable name.

Declaring Variables: Variables are created by assigning a value using the assignment operator (=).

$name = "Alice";   // String
$age = 25;         // Integer
$isStudent = false; // Boolean

Data Types in PHP

PHP supports multiple data types:

  • String: A sequence of characters.
  • Integer: Whole numbers.
  • Float: Numbers with decimal points.
  • Boolean: True or false values.
  • Array: A collection of values.
  • Object: An instance of a class.
  • NULL: A variable with no value.
  • Resource: A reference to external resources like database connections.

Code Blocks in PHP

PHP allows grouping of multiple statements into a block using curly braces ({}). A block is typically used with conditions or loops.

<?php
    $num = 10;

    if ($num > 0) {
        echo "Number is positive.\n";
        echo "It is greater than zero.";
    }
?>

Output:

Number is positive.
It is greater than zero.
End of lesson.