techmore.in

PHP Syntax

PHP (Hypertext Preprocessor) is a widely-used open source scripting language especially suited for web development and can be embedded into HTML. Here are some basics of PHP syntax to get you started:

Basic PHP Syntax

  1. PHP Tags:

    PHP tags are used to delineate PHP code within a file. These tags tell the server to interpret the enclosed code as PHP. Here are the different types of PHP tags you can use.

    Standard PHP Tags:

    The most common way to open and close PHP code:

    PHP

    <?php

    // Your PHP code goes here

    ?>

    Short-Open Tags:

    These tags are a shorter form but are not always enabled by default on all servers due to potential security issues. They can be enabled with the short_open_tag directive in the php.ini file:

    PHP

    <?

    // Your PHP code goes here

    ?>

    Echo Tags:

    A shorthand for '<?php echo' that directly outputs content:

    PHP

    <?= "Hello, World!"; ?>
    ASP Tags:

    These tags are similar to ASP style and are rarely used. They require the asp_tags directive to be enabled in the php.ini file (which is deprecated and removed in recent PHP versions):

    PHP

    <%

    // Your PHP code goes here

    %>
    Script Tags:

    Another rarely used method, similar to embedding JavaScript, and requires asp_tags to be enabled:

    PHP

    <script language="php">
    // PHP code goes here
    </script>
  2. Echo Statement: Used to output text or variables.

    PHP

    <?php

    echo "Hello, World!" ;

    ?>

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

    PHP

    <?php

    $greeting = "Hello,World!";

    echo $greeting ;

    ?>

  4. Comments.
    Single-line comments:

    PHP

    <?php

    // This is a single-line comment
    echo "Hello, World!"; // This is also a single-line comment

    ?>

    Multi-line comments:

    PHP

    <?php

    /*
    This is a multi-line comment
    */
    echo "Hello, World!";

    ?>

  5. Data Types:
    String:

    PHP

    <?php

    $String= "Hello,World!";

    echo $String ;

    ?>

    Integer:

    PHP

    <?php

    $int = 42;

    echo $int ;

    ?>

    Float:

    PHP

    <?php

    $float = 3.14;

    echo $float ;

    ?>

    Boolean:

    PHP

    <?php

    $bool = true;

    echo $bool ;

    ?>

  6. Arrays:
    Indexed array:

    PHP

    <?php

    $colors = array("Red", "Green", "Blue");

    echo $colors[0]; // Outputs "Red"

    ?>

    Associative array:

    PHP

    <?php

    $ages= array("Peter" => 35, "Ben" => 37, "Joe" => 43);

    echo $ages["Peter" ]; // Outputs 35

    ?>

  7. Conditional Statements:
    If statement:

    PHP

    <?php

    $x = 10 ;

    if ($x > 5) {

    echo "x is greater than 5";

    }

    ?>

    If-else statement:

    PHP

    <?php

    $x = 10 ;

    if ($x > 5) {

    echo "x is greater than 5";

    } else{

    echo "x is not greater than 5";

    }

    ?>

    Elseif statement:

    PHP

    <?php

    $x = 10 ;

    if ($x > 5) {

    echo "x is greater than 5";

    } elseif ($x == 5){

    echo "x is equal to 5";

    } else{

    echo "x is not greater than 5";

    }

    ?>

  8. Loops:
    While loop:

    PHP

    <?php

    $x = 1 ;

    while ($x <=5) {

    echo "The number is: $x <br>";

    }

    ?>

    For loop:

    PHP

    <?php

    for( $x = 0; $x <= 10; $x++) {

    echo "The number is: $x <br>";

    }

    ?>

    Foreach loop:

    PHP

    <?php

    $colors = array( "Red", "Green", "Blue");

    foreach ($colors as $value) {

    echo "$value <br>";

    }

    ?>

  9. Functions:

    PHP

    <?php

    function writeMessage() {

    echo "Hello, World!";

    }

    writeMessage(); // Calls the function

    ?>

  10. Superglobals:
    $_GET, $_POST, $_SERVER, $_SESSION, $_COOKIE, $_FILES, $_REQUEST, $_ENV

    PHP

    <?php

    echo $_SERVER['PHP_SELF']; // Outputs the filename of the currently executing script

    ?>

Example of a Simple PHP Script

Here's a small example combining several of these elements:

php
<?php // Define variables $name = "John Doe"; $age = 30; $is_student = true; // Function to display greeting function displayGreeting($name) { echo "Hello, $name!"; } // Check age and display appropriate message if ($age > 18) { echo "You are an adult.<br>"; } else { echo "You are a minor.<br>"; } // Loop through an array of colors $colors = array("Red", "Green", "Blue"); foreach ($colors as $color) { echo "Color: $color <br>"; } // Call the function displayGreeting($name); ?>