PHP Syntax

PHP Syntax

A PHP script is executed on the server, and the plain HTML result is sent back to the browser. The following below are some basic facts about PHP.

  • A PHP script can be placed anywhere in the document.
  • A PHP script starts with < php and ends with >
  • The default file extension for PHP files is ".php".
  • A PHP file normally contains HTML tags, and some PHP scripting code.
  • PHP statements end with a semicolon (;).
  •       
                            <?php echo "Hello World!";>
                        

    PHP Case Sensitivity

    In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

    However; all key words are not case-sensitive!

  •       
                            <?php
    ECHO "Hello World!
    "; echo "Hello World!
    "; EcHo "Hello World!
    "; ?>

    However; all variable names are case-sensitive!

  •       
                            <?php
    $color = "red";
    echo "My car is " . $color . "
    "; echo "My house is " . $COLOR . "
    "; echo "My boat is " . $coLOR . "
    "; ?>

    Escaping to PHP

    The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as 'escaping to PHP'. There are four ways to do this

    Canonical PHP tags The most universally effective PHP tag style is >?php...?>. If you use this style, you can be positive that your tags will always be correctly interpreted.

    Short-open (SGML-style) tags Short or short-open tags look like this Short tags are, as one might expect, the shortest option You must do one of two things to enable PHP to recognize the tags −

    Choose the --enable-short-tags configuration option when you're building PHP.

    Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.

    ASP-style tags ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style tags look like this <%...%> To use ASP-style tags, you will need to set the configuration option in your php.ini file.

    HTML script tags

    HTML script tags look like this <script anguage = "PHP"><l></script>

    Comments in PHP

    A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code. Comments can be used to:

    Single-line comments − They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments.

  •       
                            <?php
    // This is a single-line comment
    
    # This is also a single-line comment
    ?>
                        

    Multi-lines comments − They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments.

  •       
                            <!DOCTYPE html>
    <html>
    <body>
    
        >?
       /* This is a comment with multiline
          Author : Mohammad Mohtashim
          Purpose: Multiline Comments Demo
          Subject: PHP
       */
       
       print "An example with multi line comments";
    ?>
    
    </body>
    </html>
                        

    PHP is whitespace insensitive

    Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters). PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row.one whitespace character is the same as many such characters.

  •       
                            $four = 2 + 2; // single spaces
    $four <tab>=  2   +      2 ; // spaces and tabs
    $four =
    2+
    2; // multiple lines
                        

    PHP is case sensitive

    Yeah it is true that PHP is a case sensitive language

  •       
                            <?php
        $capital = 67;
        print("Variable capital is $capital
    "); print("Variable CaPiTaL is $CaPiTaL
    "); ?>

    Running PHP Script from Command Prompt

    Yes you can run your PHP script on your command prompt.

  •       
                            <?php
        echo "Hello PHP!!!!!";
     ?>
                        

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1