Beginner - Your First PHP Code

By: Array99      Posted On: 31 Dec, 2017      Category: PHP

PHP is a server scripting language for making dynamic and interactive Web pages.  Before writing, your first PHP script, you must know about Server Side Language. Server Side Languages like PHP runs on web server, before sending web page to the browser. It lets you building customized, creative and dynamic web pages. Once web server has executed the PHP code which is embedded in a web page, the output takes the place of the PHP code in a web page and it is viewed on the browser as we see normal HTML view. So, you need to install on your computer web server. Mostly, used web server as below:

  • Apache Web Server
  • WampServer
  • Microsoft IIS

After installing web server on your computer and it’s running perfectly. Now, it’s time to write your first PHP script.Let’s take a simple example to display a text. Create a file and put the below code. Save it with test.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>First PHP Script</title>
    </head>
    <body>
        <p>
             <?php
            echo "Welcome !!";
            ?>
        </p>
    </body>
</html>

In the above HTML code, PHP code has embedded.  <?php marks the start of an embedded PHP script and ?> marks its end. The web server interprets everything between these two delimiters and converts it to regular HTML code before it sends the web page to the requesting browser. If you view HTML source code on the browser using View Source option (generally, this option comes when do right-click inside the browser). The source code as below:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title> First PHP Script </title>
    </head>
    <body>
        <p> Welcome !! </p>
    </body>
</html>

You will see, there is no PHP code displaying. In its place the output of the script has appeared, and it looks just like standard HTML. 

A PHP script consists of a series of commands or statements. Each statement is an instruction that must be followed by the web server before it can proceed to the next instruction. PHP statements are always terminated by a semicolon  .

A simple and typical PHP statement:

<?php
echo "Hello <strong>World</strong> !!";
?>

In the above PHP code, echo is a statement which is used to send output to the browser. This output may be simple string, values or HTML code. Suppose, we have to display value of a variable, only needs to do as below:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>First PHP Script::Display Value of Variable</title>
    </head>
    <body>
        <p>
             <?php
                    $name = “Array99”;
                    echo $name; 
            ?>
        </p>
    </body>
</html>

If you place this file on your web server and request it using a web browser, your browser will receive this HTML code:
 

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>First PHP Script::Display Value of Variable</title>
    </head>
    <body>
        <p> Array99</p>
    </body>
</html>

In above examples, we are printing string value directly and a variable with assigning a string value. These string values are incorporated with double quote " or may be single quote ' .

Now, create a file with name sqrt-test.php and put below code:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>First PHP Script::Functions</title>
    </head>
    <body>
        <p> <?php echo sqrt(9); ?></p>
    </body>
</html>

The above code will first run the built-in function sqrt() to generate a square root and then pass the result to the echo command. You may see function sqrt has parenthesis () with an argument. This parenthesis has two purposes. First, they indicate that sqrt is a function and second, they mark the beginning and end of a list of arguments.

PHP has many built-in functions that let you do everything, from sending email to working with information stored in various types of databases.

You may like: