How to Create the First PHP Application?

PHP is originally derived from Personal Home Page Tools, now known as HyperText Processor.

It runs on the server and is a scripting language. This tool creates dynamic and interactive web pages that power 80 percent of the web.

It is a widely-used free tool that can make dynamic web pages. It is a tutorial to learn how to write and run a simple PHP application.

Here, you will learn the basic syntax, integrate it with HTML, and run the application in a web browser.

How to define a PHP script?

To start writing your first PHP application, create a file named myFirstWebPage.php.

You can keep an alternative name as well, remember to give it an extension of .php.

This way, the server will identify it as a PHP script. Open your IDE and write the code for a basic HTML page as follows:

<html>
    <head>
    	<title>Hello</title>
    </head>
    <body>
        HELLO WORLD 
    </body>
</html>

Save this code and upload it to any PHP-supporting host. You will see the words “HELLO WORLD” on the page.

What are open & close tags?

The open & close tags in PHP indicate a dedicated space between them for PHP code. These are written as:

<?php
?>

HTML is also allowed within the PHP document.

Therefore, these open and close tags tell the interpreter about the PHP code. Without these tags, the interpreter will misinterpret and may break the application.

What is echo content?

The echo command in PHP displays the content written after it on the web page. You put the specific content to be echoed within single quotes and end the statement with a semi-colon.

Example-

<html>
    <head>
    	<title>Hello</title>
    </head>
    <body>
    	Hello <?php echo "there!"; ?>
    </body>
</html>

When you see it on the web host, “Hello there!” will appear on the screen.

How to use the $_GET command?

PHP allows the developer to create dynamic web content and interact with incoming visitors. You can collect data using URLs or forms by using the $_GET command.

<html>
    <head>
    	<title>Hello</title>
    </head>
    <body>
    	Hello<?php echo $_GET['name']; ?>
    </body>
</html>

When you run this on the server, the screen will display “Hello your name”.

How to define variables and use logic?

PHP commands like the if, elseif, else, and date() function allows you to display conditional output. The following code is an example:

<html>
    <head>
    	<title>Hello</title>
    </head>
    <body>
	<?php
		If(date("G") > 18) {
			$time = "evening";
		} elseif (date("G") > 12) {
			$time = "afternoon";
		} else { 
			$time = "morning";
		} echo "Good" .$time. " " .$_GET['name'];
        ?>
    </body>
</html>

This code will display Good Morning, Good Afternoon, or Good Evening depending on the time of the server, followed by your name.

End Takeaway

For executing the PHP code, the document extension must be .php.

The interpreter will interpret it only after seeing the open & closed tags by combining HTML with PHP. It will create your first PHP application.